荦绅诵 发表于 2025-6-12 21:13:26

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

前言

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

先说明一点,网上找到的大部分资料都是使用图形界面操作,然后 Windows 那个设置真的很垃圾,本身很卡,点多几下就卡死或者闪退。
在设置的可选功能里面添加 OpenSSH Server 组件,总是跑到一半就卡死不动了
在啃了官方文档之后,我完全用命令行的方式搞定了。不得不说这个 powershell 确实有点东西的
安装 OpenSSH Server

Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0这个命令比在设置界面添加快多了
启动服务并设置为开机自启

Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'开放防火墙端口(22)

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

这时候就已经搞定了,直接用用本地账号或 Microsoft 账户登录就行了,建议使用本地账户
使用本地用户 + 密码登录

ssh your_user@10.x.x.x使用密钥登录

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

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

# 设置 authorized_keys 权限
cd .ssh
icacls authorized_keys /inheritance:r
icacls authorized_keys /grant:r 用户名:F
icacls authorized_keys /remove "Authenticated Users" "Users" "Administrators" "System"
icacls authorized_keys /setowner 用户名配置

Windows 上的 sshd_config 路径在 C:\ProgramData\ssh\sshd_config
刚才说 Windows 上的 SSH 密钥登录有点小坑,这里看下这个配置文件
# This is the sshd server system-wide configuration file.See
# sshd_config(5) for more information.

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.Uncommented options override the
# default value.

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey __PROGRAMDATA__/ssh/ssh_host_rsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_dsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ecdsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile        .ssh/authorized_keys

#AuthorizedPrincipalsFile none

# For this to work you will also need host keys in %programData%/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no

# GSSAPI options
#GSSAPIAuthentication no

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# override default of no subsystems
Subsystem        sftp        sftp-server.exe

# Example of overriding settings on a per-user basis
#Match User anoncvs
#        AllowTcpForwarding no
#        PermitTTY no
#        ForceCommand cvs server

Match Group administrators
       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
最好在配置下权限
cd 'C:\ProgramData\ssh'
New-Item -ItemType File -Path .\administrators_authorized_keys -Force
icacls .\administrators_authorized_keys /inheritance:r
icacls .\administrators_authorized_keys /grant:r "Administrators:F"修改默认端口

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


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

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

修改完记得添加规则
New-NetFirewallRule -Name "OpenSSH Custom Port" `
-DisplayName "OpenSSH Custom Port (60222)" `
-Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 60222重启 SSH 服务

Restart-Service sshd使用体验

Windows 的命令行体验还是比较有限的
启用了 SSH Server 之后,连上去居然是 CMD
不过要改成 PowerShell 也不难
这里贴一下网上找到的方法(未测试)
方法一,修改注册表

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell `
-PropertyType String -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Force重启服务 Restart-Service sshd
方法二,为单个用户设置登录 shell

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

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

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


[*]https://zhuanlan.zhihu.com/p/391373172

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 告别图形界面:Windows系统OpenSSH服务部署