告別圖形界面:Windows系統(tǒng)OpenSSH服務(wù)部署
前言
士別三日當(dāng)刮目相待
沒(méi)想到這么多年過(guò)去了,Windows 也不再是以前那個(gè)離開(kāi)了圖形界面啥也不是的系統(tǒng)
Windows 10/11 和 Server 2019+ 已內(nèi)置 OpenSSH Server,可以作為跳板連接到其他 Linux 服務(wù)器。
這種場(chǎng)景一般是辦公室有長(zhǎng)期開(kāi)機(jī)的 Windows 電腦,然后通過(guò)虛擬組網(wǎng)的方式來(lái)遠(yuǎn)程操作
操作步驟
先說(shuō)明一點(diǎn),網(wǎng)上找到的大部分資料都是使用圖形界面操作,然后 Windows 那個(gè)設(shè)置真的很垃圾,本身很卡,點(diǎn)多幾下就卡死或者閃退。
在設(shè)置的可選功能里面添加 OpenSSH Server 組件,總是跑到一半就卡死不動(dòng)了
在啃了官方文檔之后,我完全用命令行的方式搞定了。不得不說(shuō)這個(gè) powershell 確實(shí)有點(diǎn)東西的
安裝 OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
這個(gè)命令比在設(shè)置界面添加快多了
啟動(dòng)服務(wù)并設(shè)置為開(kāi)機(jī)自啟
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
開(kāi)放防火墻端口(22)
為了安全起見(jiàn),后續(xù)建議修改一下其他端口
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
連接
這時(shí)候就已經(jīng)搞定了,直接用用本地賬號(hào)或 Microsoft 賬戶(hù)登錄就行了,建議使用本地賬戶(hù)
使用本地用戶(hù) + 密碼登錄
ssh your_user@10.x.x.x
使用密鑰登錄
在 Linux 上生成密鑰對(duì)(或使用已有)
ssh-keygen -t rsa -b 4096
將公鑰內(nèi)容添加到 Windows 的用戶(hù) .ssh\authorized_keys 里
C:\Users\<your_user>\.ssh\authorized_keys
如果是 Linux 的話(huà),這里就完事了,可以直接用密鑰免密碼登錄,但 Windows 有點(diǎn)小坑,接下來(lái)介紹一下。
權(quán)限
SSH 對(duì)配置文件的權(quán)限要求比較嚴(yán)格,在 Linux 下很簡(jiǎn)單,用 chmod 和 chown 命令就行了
但在 Windows 上就蒙圈了,似乎可以在圖形界面配置權(quán)限,但好像很復(fù)雜
不過(guò)我還是找到了用命令行配置權(quán)限的方法
# 設(shè)置目錄權(quán)限:僅用戶(hù)本身可讀寫(xiě)
icacls .ssh /inheritance:r
icacls .ssh /grant:r 用戶(hù)名:F
icacls .ssh /remove "Authenticated Users" "Users" "Administrators" "System"
icacls .ssh /setowner 用戶(hù)名
# 設(shè)置 authorized_keys 權(quán)限
cd .ssh
icacls authorized_keys /inheritance:r
icacls authorized_keys /grant:r 用戶(hù)名:F
icacls authorized_keys /remove "Authenticated Users" "Users" "Administrators" "System"
icacls authorized_keys /setowner 用戶(hù)名
配置
Windows 上的 sshd_config 路徑在 C:\ProgramData\ssh\sshd_config
剛才說(shuō) Windows 上的 SSH 密鑰登錄有點(diǎn)小坑,這里看下這個(gè)配置文件
# 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 的注釋去掉,雖然默認(rèn)是啟用 PubkeyAuthentication,但為了明確起見(jiàn),建議取消注釋?zhuān)悦庀到y(tǒng)某些默認(rèn)值變動(dòng)導(dǎo)致失效。
然后還有個(gè)大坑 Match Group administrators
如果登錄用戶(hù)屬于 administrators 組,就不會(huì)使用 ~/.ssh/authorized_keys,而是會(huì)強(qiáng)制用 C:\ProgramData\ssh\administrators_authorized_keys
一般 Windows 用戶(hù)都是管理員,所以寫(xiě)入到 C:\Users\用戶(hù)名\.ssh\authorized_keys 的配置是無(wú)效的
正確的方式是把公鑰寫(xiě)入到 C:\ProgramData\ssh\administrators_authorized_keys
最好在配置下權(quán)限
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"
修改默認(rèn)端口
默認(rèn)是 22 端口,不太安全,別人一掃就出來(lái)了
端口范圍科普
1025~49151是注冊(cè)端口段(很多服務(wù)注冊(cè)使用)49152~65535是動(dòng)態(tài)/私有端口段(推薦選這里)
查看端口占用
在私有端口段隨便選一個(gè)就行
不過(guò)可以先查看占用,比如:
netstat -aon | findstr ":60222"
添加防火墻規(guī)則
修改完記得添加規(guī)則
New-NetFirewallRule -Name "OpenSSH Custom Port" `
-DisplayName "OpenSSH Custom Port (60222)" `
-Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 60222
重啟 SSH 服務(wù)
Restart-Service sshd
使用體驗(yàn)
Windows 的命令行體驗(yàn)還是比較有限的
啟用了 SSH Server 之后,連上去居然是 CMD
不過(guò)要改成 PowerShell 也不難
這里貼一下網(wǎng)上找到的方法(未測(cè)試)
方法一,修改注冊(cè)表
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell `
-PropertyType String -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Force
重啟服務(wù) Restart-Service sshd
方法二,為單個(gè)用戶(hù)設(shè)置登錄 shell
如果不想影響系統(tǒng)中其他用戶(hù)(比如安全隔離),可以編輯 sshd_config,增加如下內(nèi)容:
Match User 用戶(hù)名
ForceCommand powershell.exe
方法三,臨時(shí)切換
連上去之后直接輸入 powershell 或 pwsh (如果有安裝 PowerShell 7 的話(huà))
小結(jié)
搞定了,雖然是連上了 SSH,不過(guò) Windows 的價(jià)值也就在于當(dāng)跳板連 Linux 了…

浙公網(wǎng)安備 33010602011771號(hào)