找回密码
 立即注册
首页 业界区 业界 告别图形界面:Windows系统OpenSSH服务部署 ...

告别图形界面:Windows系统OpenSSH服务部署

荦绅诵 2025-6-12 21:13:26
前言

士别三日当刮目相待
没想到这么多年过去了,Windows 也不再是以前那个离开了图形界面啥也不是的系统
Windows 10/11 和 Server 2019+ 已内置 OpenSSH Server,可以作为跳板连接到其他 Linux 服务器。
这种场景一般是办公室有长期开机的 Windows 电脑,然后通过虚拟组网的方式来远程操作
操作步骤

先说明一点,网上找到的大部分资料都是使用图形界面操作,然后 Windows 那个设置真的很垃圾,本身很卡,点多几下就卡死或者闪退。
在设置的可选功能里面添加 OpenSSH Server 组件,总是跑到一半就卡死不动了
在啃了官方文档之后,我完全用命令行的方式搞定了。不得不说这个 powershell 确实有点东西的
安装 OpenSSH Server
  1. Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
复制代码
这个命令比在设置界面添加快多了
启动服务并设置为开机自启
  1. Start-Service sshd
  2. Set-Service -Name sshd -StartupType 'Automatic'
复制代码
开放防火墙端口(22)

为了安全起见,后续建议修改一下其他端口
  1. New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
复制代码
连接

这时候就已经搞定了,直接用用本地账号或 Microsoft 账户登录就行了,建议使用本地账户
使用本地用户 + 密码登录
  1. ssh your_user@10.x.x.x
复制代码
使用密钥登录

在 Linux 上生成密钥对(或使用已有)
  1. ssh-keygen -t rsa -b 4096
复制代码
将公钥内容添加到 Windows 的用户 .ssh\authorized_keys 里
  1. C:\Users\<your_user>\.ssh\authorized_keys
复制代码
如果是 Linux 的话,这里就完事了,可以直接用密钥免密码登录,但 Windows 有点小坑,接下来介绍一下。
权限

SSH 对配置文件的权限要求比较严格,在 Linux 下很简单,用 chmod 和 chown 命令就行了
但在 Windows 上就蒙圈了,似乎可以在图形界面配置权限,但好像很复杂
不过我还是找到了用命令行配置权限的方法
  1. # 设置目录权限:仅用户本身可读写
  2. icacls .ssh /inheritance:r
  3. icacls .ssh /grant:r 用户名:F
  4. icacls .ssh /remove "Authenticated Users" "Users" "Administrators" "System"
  5. icacls .ssh /setowner 用户名
  6. # 设置 authorized_keys 权限
  7. cd .ssh
  8. icacls authorized_keys /inheritance:r
  9. icacls authorized_keys /grant:r 用户名:F
  10. icacls authorized_keys /remove "Authenticated Users" "Users" "Administrators" "System"
  11. icacls authorized_keys /setowner 用户名
复制代码
配置

Windows 上的 sshd_config 路径在 C:\ProgramData\ssh\sshd_config
刚才说 Windows 上的 SSH 密钥登录有点小坑,这里看下这个配置文件
  1. # This is the sshd server system-wide configuration file.  See
  2. # sshd_config(5) for more information.
  3. # The strategy used for options in the default sshd_config shipped with
  4. # OpenSSH is to specify options with their default value where
  5. # possible, but leave them commented.  Uncommented options override the
  6. # default value.
  7. #Port 22
  8. #AddressFamily any
  9. #ListenAddress 0.0.0.0
  10. #ListenAddress ::
  11. #HostKey __PROGRAMDATA__/ssh/ssh_host_rsa_key
  12. #HostKey __PROGRAMDATA__/ssh/ssh_host_dsa_key
  13. #HostKey __PROGRAMDATA__/ssh/ssh_host_ecdsa_key
  14. #HostKey __PROGRAMDATA__/ssh/ssh_host_ed25519_key
  15. # Ciphers and keying
  16. #RekeyLimit default none
  17. # Logging
  18. #SyslogFacility AUTH
  19. #LogLevel INFO
  20. # Authentication:
  21. #LoginGraceTime 2m
  22. #PermitRootLogin prohibit-password
  23. #StrictModes yes
  24. #MaxAuthTries 6
  25. #MaxSessions 10
  26. #PubkeyAuthentication yes
  27. # The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
  28. # but this is overridden so installations will only check .ssh/authorized_keys
  29. AuthorizedKeysFile        .ssh/authorized_keys
  30. #AuthorizedPrincipalsFile none
  31. # For this to work you will also need host keys in %programData%/ssh/ssh_known_hosts
  32. #HostbasedAuthentication no
  33. # Change to yes if you don't trust ~/.ssh/known_hosts for
  34. # HostbasedAuthentication
  35. #IgnoreUserKnownHosts no
  36. # Don't read the user's ~/.rhosts and ~/.shosts files
  37. #IgnoreRhosts yes
  38. # To disable tunneled clear text passwords, change to no here!
  39. #PasswordAuthentication yes
  40. #PermitEmptyPasswords no
  41. # GSSAPI options
  42. #GSSAPIAuthentication no
  43. #AllowAgentForwarding yes
  44. #AllowTcpForwarding yes
  45. #GatewayPorts no
  46. #PermitTTY yes
  47. #PrintMotd yes
  48. #PrintLastLog yes
  49. #TCPKeepAlive yes
  50. #UseLogin no
  51. #PermitUserEnvironment no
  52. #ClientAliveInterval 0
  53. #ClientAliveCountMax 3
  54. #UseDNS no
  55. #PidFile /var/run/sshd.pid
  56. #MaxStartups 10:30:100
  57. #PermitTunnel no
  58. #ChrootDirectory none
  59. #VersionAddendum none
  60. # no default banner path
  61. #Banner none
  62. # override default of no subsystems
  63. Subsystem        sftp        sftp-server.exe
  64. # Example of overriding settings on a per-user basis
  65. #Match User anoncvs
  66. #        AllowTcpForwarding no
  67. #        PermitTTY no
  68. #        ForceCommand cvs server
  69. Match Group administrators
  70.        AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
复制代码
首先把 PubkeyAuthentication yes 的注释去掉,虽然默认是启用 PubkeyAuthentication,但为了明确起见,建议取消注释,以免系统某些默认值变动导致失效。
然后还有个大坑 Match Group administrators
如果登录用户属于 administrators 组,就不会使用 ~/.ssh/authorized_keys,而是会强制用 C:\ProgramData\ssh\administrators_authorized_keys
一般 Windows 用户都是管理员,所以写入到 C:\Users\用户名\.ssh\authorized_keys 的配置是无效的
正确的方式是把公钥写入到 C:\ProgramData\ssh\administrators_authorized_keys
最好在配置下权限
  1. cd 'C:\ProgramData\ssh'
  2. New-Item -ItemType File -Path .\administrators_authorized_keys -Force
  3. icacls .\administrators_authorized_keys /inheritance:r
  4. icacls .\administrators_authorized_keys /grant:r "Administrators:F"
复制代码
修改默认端口

默认是 22 端口,不太安全,别人一扫就出来了
端口范围科普


  • 1025 ~ 49151 是注册端口段(很多服务注册使用)
  • 49152 ~ 65535 是动态/私有端口段(推荐选这里
查看端口占用

在私有端口段随便选一个就行
不过可以先查看占用,比如:
  1. netstat -aon | findstr ":60222"
复制代码
添加防火墙规则

修改完记得添加规则
  1. New-NetFirewallRule -Name "OpenSSH Custom Port" `
  2.   -DisplayName "OpenSSH Custom Port (60222)" `
  3.   -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 60222
复制代码
重启 SSH 服务
  1. Restart-Service sshd
复制代码
使用体验

Windows 的命令行体验还是比较有限的
启用了 SSH Server 之后,连上去居然是 CMD
不过要改成 PowerShell 也不难
这里贴一下网上找到的方法(未测试)
方法一,修改注册表
  1. New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell `
  2.   -PropertyType String -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Force
复制代码
重启服务 Restart-Service sshd
方法二,为单个用户设置登录 shell

如果不想影响系统中其他用户(比如安全隔离),可以编辑 sshd_config,增加如下内容:
  1. Match User 用户名
  2.     ForceCommand powershell.exe
复制代码
方法三,临时切换

连上去之后直接输入 powershell 或 pwsh (如果有安装 PowerShell 7 的话)
小结

搞定了,虽然是连上了 SSH,不过 Windows 的价值也就在于当跳板连 Linux 了…
参考资料


  • https://zhuanlan.zhihu.com/p/391373172

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册