Windows开启SSH
Windows系统下,很方便可以开启SSH。
20220924更新:
先前的步骤,在系统更新后,出现无法连接的问题,问题在于服务端用户组发生变化,需将公共密匙放至C:\ProgramData\ssh\administrators_authorized_keys
,参照该链接和链接做法,操作如下:
- 服务端配置。卸载OpenSSH,下载最新压缩包,解压缩至
C:\Program Files\OpenSSH
本地安装。
cd C:\Program Files\OpenSSH
powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
net start sshd
Set-Service sshd -StartupType Automatic
- 客户端配置。
cd C:\Users\Martin\.ssh
ssh-keygen
#output:
#Your identification has been saved in C:\Users\Martin/.ssh/id_rsa
#Your public key has been saved in C:\Users\Martin/.ssh/id_rsa.pub
#The key fingerprint is:
#SHA256:xxx/xxx martin@Martin-X1Nano
# 以管理员身份启动ssh-agent服务并使用ssh-add来存储私钥
# Make sure you're running as an Administrator
Start-Service ssh-agent
# This should return a status of Running
Get-Service ssh-agent
# Now load your key files into ssh-agent
ssh-add ~\.ssh\id_rsa
这一步,可以进一步简化:
# 如果PowerShell当前目录是`PS C:\Users\User`
ssh-keygen
# 直接cat 本地的id_rsa.pub,复制内容到服务器的~/.ssh/authorized_keys中新增一行
# 客户端
cat ~/.ssh/id_rsa.pub
# 服务端
vim C:/ProgramData/ssh/administrators_authorized_keys
- 部署公钥。 上面提及到,更新系统后,账号归类到管理员用户组,区别于先前操作,具体如下:
# 将客户端的公钥 `~.ssh/id_rsa.pub`的内容移动到服务端上`C:\ProgramData\ssh`,重命名为`administrators_authorized_keys` (无扩展名)的文本文件中。
# 在服务端上启动Windows PowerShell,运行以下命令
$acl = Get-Acl C:\ProgramData\ssh\administrators_authorized_keys
$acl.SetAccessRuleProtection($true, $false)
$administratorsRule = New-Object system.security.accesscontrol.filesystemaccessrule("Administrators","FullControl","Allow")
$systemRule = New-Object system.security.accesscontrol.filesystemaccessrule("SYSTEM","FullControl","Allow")
$acl.SetAccessRule($administratorsRule)
$acl.SetAccessRule($systemRule)
$acl | Set-Acl
测试成功。
↓以下为旧版内容。↓
-
默认Windows 10 1809以上系统,自带open ssh功能,进入可选功能,添加open ssh服务端。如果添加功能失败,离线安装,去github下载最新压缩包,手动安装即可。
-
Get-Service *|where Name -Like '*ssh*'
查看ssh是否添加成功。 -
Start-Service sshd
启动ssh。// net start/stop sshd -
ssh 你的用户名@localhost
登录即可。如果没有设置密码,可能会提示错误。 -
The authenticity of host 'localhost (::1)' can't be established.
此报错,可以使用ssh -o StrictHostKeyChecking=no
登录即可。
在使用中,输入密码不是很方便,我们可以选择开启ssh免密登录。
-
客户端查看ssh-agent服务是否开启,OpenSSH Authentication Agent服务。
-
网上教程多以服务端为Linux系统为主,Windows服务器端配置有所差异。
-
客户端生成密匙,
ssh-keygen -t rsa
,将~/.ssh目录下的id_rsa.pub复制到Windows服务端的~/.ssh目录下。 -
新建文件authorized_keys(无后缀名),将id_rsa.pub的内容复制到该文件。
-
修改
C:\ProgramData\ssh
下的sshd_config文件,确认PubkeyAuthentication yes | PasswordAuthentication no |AuthorizedKeysFile .ssh/authorized_keys |
三个配置已经打开。确认#Match Group administrators |# AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
已注释。 -
重启sshd net stop/start sshd
-
vs code 或者 ssh直接连接成功,如果连接失败,报错如
permission denied (publickey keyboard-interactive)
,则客户端需要重新生成id_rsa密匙,重新复制到服务端,并复制其内容到authorized_keys中。 -
配置vs code时,需要增加一行
IdentityFile C:\Users\xxx\.ssh\id_rsa
私钥地址。
参考链接:
https://segmentfault.com/a/1190000022248357
https://www.cnblogs.com/charon2/p/10448889.html
https://my.oschina.net/u/3100167/blog/5220061
https://zhuanlan.zhihu.com/p/111812831
https://blog.arey.fun/archives/19/
https://www.freesion.com/article/8067237339/
https://zhuanlan.zhihu.com/p/222452460
https://juejin.cn/post/7023042621295558692
https://github.com/PowerShell/Win32-OpenSSH/releases