適用於 Windows Server 和 Windows 的 OpenSSH 伺服器組態

適用於 Windows Server 2022、Windows Server 2019、Windows 10 (組建 1809 及更高版本)

本文涵蓋適用於 OpenSSH 伺服器 (sshd) 的 Windows 專屬組態。

OpenSSH 會在線上 (OpenSSH.com) 維護設定選項的詳細文件,在本文件集當中不會重複。

OpenSSH 組態檔

OpenSSH 同時具有伺服器和用戶端設定的組態檔。 OpenSSH 是開放原始碼,從 Windows Server 2019 和 Windows 10 (組建 1809) 開始,新增至 Windows Server 和 Windows 用戶端作業系統。 因此,這裡不會重複 OpenSSH 組態檔的開放原始碼文件。 您可以在 ssh_config 手冊頁面上找到用戶端組態檔,而 OpenSSH 伺服器組態檔可以在 sshd_config 手冊頁面上找到。

根據預設,Open SSH Server (sshd) 會從 %programdata%\ssh\sshd_config 讀取組態資料,或可透過使用 -f 參數啟動 sshd.exe 來指定不同的組態態。 如果檔案不存在,sshd 會在服務啟動時,以預設設定產生一個檔案。

在 Windows 中,OpenSSH 用戶端 (ssh) 會依下列順序從組態檔讀取組態資料:

  1. 藉由使用 -F 參數啟動 ssh.exe,指定組態檔的路徑,以及該檔案的項目名稱。
  2. 位於 %userprofile%\.ssh\config 的使用者組態檔
  3. 位於 %programdata%\ssh\ssh_config的系統範圍組態檔。

在 Windows 中設定 OpenSSH 的預設殼層

預設的命令殼層提供使用者使用 SSH 連線到伺服器時所看到的體驗。 初始預設視窗是 Windows 命令殼層 (cmd.exe)。 Windows 也包含 PowerShell,而第三方命令殼層也適用於 Windows,而且可以設定為伺服器的預設殼層。

若要設定預設的命令殼層,請先確認 OpenSSH 安裝資料夾位於系統路徑上。 若為 Windows,預設安裝資料夾是 %systemdrive%\Windows\System32\openssh。 下列命令會顯示目前的路徑設定,並在其中新增預設的 OpenSSH 安裝資料夾。

命令殼層 要使用的命令
Command path
PowerShell $env:path

在 Windows 登錄中,透過在字串值 DefaultShell 中新增的HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH殼層可執行檔的完整路徑來設定預設 ssh 殼層。

例如,下列提升權限的 Powershell 命令會將預設殼層設定為 PowerShell.exe:

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force

sshd_config 中的 Windows 設定

在 Windows 中,sshd 預設會從 %programdata%\ssh\sshd_config 讀取組態檔資料,或者藉由使用 -f 參數啟動 sshd.exe 來指定不同的組態檔。 如果檔案不存在,sshd 會在服務啟動時,以預設設定產生一個檔案。

以下列出的元素可透過 sshd_config 中的項目,提供 Windows 特有的設定。 還有其他可行的組態設定並未在這裡列出,它們在線上 Win32 OpenSSH 文件中有詳細說明。

提示

OpenSSH 伺服器 (sshd) 會在服務啟動時讀取組態檔。 組態檔的任何變更都需要重新啟動服務。

AllowGroups,AllowUsers,DenyGroups,DenyUsers

使用 AllowGroups、AllowUsers、DenyGroups 和 DenyUsers 指示詞來控制哪些使用者和群組可以連線到伺服器。 允許/拒絕指示詞會依下列順序處理:DenyUsers、AllowUsers、DenyGroups 和 Finally AllowGroups。 所有帳戶名稱都必須以小寫來指定。 如需 ssh_config 中 PATTERNS 和萬用字元的詳細資訊,請參閱 sshd_config OpenBSD 手冊頁面

以網域使用者或群組設定使用者/群組型規則時,請使用下列格式:user?domain*。 Windows 允許使用多種格式來指定網域主體,但是許多格式與標準 Linux 模式有衝突。 基於這個理由,會新增 * 來涵蓋 FQDN。 此外,這種方法會使用 "?",而不是 @,以避免與 username@host 格式發生衝突。

工作群組使用者/群組和網際網路連線帳戶一律會解析為其本機帳戶名稱 (沒有網域部分,類似於標準 Unix 名稱)。 網域使用者和群組會嚴格地解析為 NameSamCompatible 格式 - domain_short_name\user_name。 所有以使用者/群組為基礎的設定規則都必須遵守此格式。

下列範例拒絕主機 192.168.2.23 的 contoso\admin,並封鎖 contoso 網域的所有使用者。 範例中也允許屬於 contoso\sshusers 和 contoso\serveroperators 群組成員的使用者。

DenyUsers contoso\admin@192.168.2.23
DenyUsers contoso\*
AllowGroups contoso\sshusers contoso\serveroperators

下列範例讓使用者本機使用者從主機 192.168.2.23 登入,並允許群組 sshusers 的成員。

AllowUsers localuser@192.168.2.23
AllowGroups sshusers

AuthenticationMethods

對於 Windows OpenSSH,唯一可用的驗證方法為 passwordpublickey

重要

目前不支援使用 Microsoft Entra 帳戶進行驗證。

AuthorizedKeysFile

預設值為 .ssh/authorized_keys。 如果路徑不是絕對路徑,則相對於使用者的主目錄 (或設定檔映像路徑) 獲取路徑,例如 C:\Users\username。 如果使用者屬於系統管理員群組,則會改用 %programdata%/ssh/administrators_authorized_keys

提示

administrators_authorized_keys 檔案只能有NT Authority\SYSTEM 帳戶和 BUILTIN\Administrators 安全群組的權限項目。 NT Authority\SYSTEM 帳戶必須獲授完整控制權。 系統管理員必須具有 BUILTIN\Administrators 安全群組權限來管理授權的金鑰,因此您可以選擇必要的存取權。 若要授與權限,您可以開啟已提升權限的 PowerShell 提示字元,並執行 命令 icacls.exe "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"

ChrootDirectory (在 v7.7.0.0 中新增支援)

只有 sftp 工作階段才支援這個指示詞。 cmd.exe 的遠端工作階段不接受 ChrootDirectory。 若要設定僅限 sftp 的 chroot 伺服器,請將 ForceCommand 設為 internal-sftp。 您也可以藉由實作只允許 scp 和 sftp 的自訂殼層,以使用 chroot 設定 scp。

GSSAPIAuthentication

GSSAPIAuthentication 組態引數會指定是否允許 GSSAPI 型使用者驗證。 GSSAPIAuthentication 的預設值為否。

使用 OpenSSH 用戶端時,GSSAPI 驗證也需要使用 -K 參數來指定主機名稱。 或者,您可以在 SSH 用戶端組態中建立對應的項目。 在 Windows 中,OpenSSH 用戶端預設會從 %userprofile%.ssh\config 讀取組態資料。

您可以在下方看到範例 GSSAPI OpenSSH 用戶端組態。

# Specify a set of configuration arguments for a host matching the pattern SERVER01.contoso.com
# Patterns are case sensitive
Host SERVER01.contoso.com
    # Enables GSSAPI authentication
    GSSAPIAuthentication yes
    # Forward (delegate) credentials to the server.
    GSSAPIDelegateCredentials yes

重要

GSSAPI 僅適用於在 Windows Server 2022、Windows 11 和 Windows 10 xxxx 中啟動。

HostKey

預設值是:

#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

如果預設值不存在,sshd 會在服務啟動時自動產生。

比對

使用一或多個準則比對條件。 比對時,會套用後續的組態引數。 比對會使用 AllowGroups、AllowUsers、DenyGroups、DenyUsers 區段中涵蓋的模式規則。 使用者和群組名稱應該是小寫。

PermitRootLogin

在 Windows 中不適用。 若要防止系統管理員登入,請使用具有 DenyGroups 指示詞的系統管理員。

SyslogFacility

如果您需要以檔案為基礎的記錄,請使用 LOCAL0。 記錄檔會在 %programdata%\ssh\logs 下產生。 其他任何值 (包括預設值 AUTH) 都會將記錄導向至 ETW。 如需詳細資訊,請參閱 Windows 中的「記錄設施」

組態引數

從 Windows Server 2022、Windows 11 和 Windows 10 xxxx 開始,可以使用下列組態引數:

  • GSSAPIAuthentication

在 Windows Server 和 Windows 用戶端隨附的 OpenSSH 版本中,無法使用下列組態引數:

  • AcceptEnv
  • AllowStreamLocalForwarding
  • AuthorizedKeysCommand
  • AuthorizedKeysCommandUser
  • AuthorizedPrincipalsCommand
  • AuthorizedPrincipalsCommandUser
  • ExposeAuthInfo
  • GSSAPICleanupCredentials
  • GSSAPIStrictAcceptorCheck
  • HostbasedAcceptedKeyTypes
  • HostbasedAuthentication
  • HostbasedUsesNameFromPacketOnly
  • IgnoreRhosts
  • IgnoreUserKnownHosts
  • KbdInteractiveAuthentication
  • KerberosAuthentication
  • KerberosGetAFSToken
  • KerberosOrLocalPasswd
  • KerberosTicketCleanup
  • PermitTunnel
  • PermitUserEnvironment
  • PermitUserRC
  • PidFile
  • PrintLastLog
  • PrintMotd
  • RDomain
  • StreamLocalBindMask
  • StreamLocalBindUnlink
  • StrictModes
  • X11DisplayOffset
  • X11Forwarding
  • X11UseLocalhost
  • XAuthLocation