诸暨城乡与建设局网站长沙推广公司
服务器操作系统 Window Server 2016
1、Windows服务器安装OpenSSH
有多种方式,本文介绍一种方式
下载页:
https://github.com/PowerShell/Win32-OpenSSH/releases
在下载页下载文件OpenSSH-Win64.zip
本次实验解压至 D:\OpenSSH-Win64\OpenSSH-Win64
# 打开powershell
# 安装
powershell.exe -ExecutionPolicy Bypass -File D:\OpenSSH-Win64\OpenSSH-Win64\install-sshd.ps1
修改配置文件
编辑配置文件 C:\ProgramData\ssh\sshd_config,不是解压目录里的配置文件
注释掉配置文件尾部的这两行配置#Match Group administrators
# AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
启动命令
# 开启防火墙
netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22# 启动
net start sshd# 停止
net stop sshd# 重启
Restart-Service sshd
2、SSH免密配置
在客户端生成ssh密钥
在终端(windows在cmd执行)执行ssh-keygen -t rsa,一直回车即可
生成的密钥文件存在在用户目录的.ssh文件夹中,例如
C:\Users\Administrator.ssh
或者
/root/.ssh
生成了两个文件id_rsa是私钥,id_rsa.pub是公钥
将客户端的公钥
文件内容,追加
到服务器端的authorized_keys
文件中即可实现免密登录。
Windows服务器:C:\Users\Administrator.ssh\authorized_keys
Linux服务器:/root/.ssh/authorized_keys
服务器端的authorized_keys文件中每一行都是一条免密配置,如果文件不存在,新建即可。
# SSH登录
# ssh 用户名@服务器IP或者域名
C:\Users\Administrator> ssh Administrator@dev.test.com
# 第一次登录会出现提示,输入yes
The authenticity of host 'dev.test.com (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:ujSN12kWaMtVwDVG5Ak0ZQ2flhKDYGnorIyzGAoT3+Q.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'dev.test.com' (ECDSA) to the list of known hosts.# 无需输入密码就能登陆成功
3、使用代码SSH免密登录并执行脚本
pom依赖
<dependencies>
<!--<dependency><groupId>com.jcraft</groupId><artifactId>jsch</artifactId><version>0.1.55</version></dependency>
--><!-- com.jcraft.jsch长时间没有更新,windows服务器免密登录会出现错误,使用com.github.mwiede.jsch替代 --><dependency><groupId>com.github.mwiede</groupId><artifactId>jsch</artifactId><version>0.2.8</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency></dependencies>
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.apache.commons.io.IOUtils;import java.io.IOException;
import java.io.InputStream;public class Test {public static void main(String[] args) throws JSchException, IOException {String username = "Administrator";String hostIp = "dev.test.com";int sshPort = 22;// String password = "xxx";String privateKeyPath = "C:\\Users\\Administrator\\.ssh\\id_rsa";JSch jsch = new JSch();// 免密登录jsch.addIdentity(privateKeyPath);Session session = jsch.getSession(username, hostIp, sshPort);// 密码登录// session.setPassword(password);session.setConfig("StrictHostKeyChecking", "no");System.out.println("connecting...");session.connect();System.out.println("connect success");// 执行脚本 ipconfigString command = "ipconfig";ChannelExec channelExec = (ChannelExec) session.openChannel("exec");InputStream in = channelExec.getInputStream();channelExec.setCommand(command);channelExec.setErrStream(System.err);channelExec.connect();String result = IOUtils.toString(in, "GBK");// 输出执行结果System.out.println(result);channelExec.disconnect();session.disconnect();System.out.println("connect disconnect");}
}
常见问题
异常:
com.jcraft.jsch.JSchException: invalid privatekey: [B@42bbf4
1.SSH免密登录没有配置成功,将客户端的公钥
文件内容,追加
到服务器端的authorized_keys
文件中即可实现免密登录。
2.如果在终端中可以免密登录,用代码免密登录报错。
使用ssh-keygen -t rsa -m PEM
重新生成密钥
-m 参数指定密钥的格式,PEM(也就是RSA格式)是之前使用的旧格式
在终端中可以免密登录,用代码免密登录出现异常:
com.jcraft.jsch.JSchException: Auth fail
com.jcraft.jsch.JSchException: Auth fail
at com.jcraft.jsch.Session.connect(Session.java:519)
at com.jcraft.jsch.Session.connect(Session.java:183)
com.jcraft.jsch长时间没有更新,windows服务器免密登录会出现错误,使用com.github.mwiede.jsch替代
<!-- 替换掉com.jcraft.jsch --><!-- <dependency><groupId>com.jcraft</groupId><artifactId>jsch</artifactId><version>0.1.55</version></dependency>--><!-- 使用com.github.mwiede.jsch替代 --><dependency><groupId>com.github.mwiede</groupId><artifactId>jsch</artifactId><version>0.2.8</version></dependency>