透過 SSH 的 PowerShell 遠端

概觀

PowerShell 遠端通常會使用 WinRM 進行連線交涉和資料傳輸。 SSH 目前適用於 Linux 與 Windows 平台,而且能夠執行真正的多平台 PowerShell 遠端。

WinRM 提供一個健全裝載模型以供 PowerShell 遠端工作階段使用。 SSH 型遠端目前不支援遠端端點設定和 Just Enough Administration (JEA)。

SSH 遠端可讓您在 Windows 和 Linux 電腦之間執行基本的 PowerShell 會話遠端處理。 SSH 遠端會在目標電腦上建立 PowerShell 主機處理序作為 SSH 子系統。 最後,我們將實作類似於 WinRM 的一般裝載模型,以支援端點設定和 JEA。

New-PSSessionEnter-PSSessionInvoke-Command Cmdlet 現在已設定新的參數,以支援這個新的遠端連線。

[-HostName <string>]  [-UserName <string>]  [-KeyFilePath <string>]

若要建立遠端會話,您可以使用 HostName 參數指定目標電腦,並使用 UserName 提供使用者名稱。 以互動方式執行 Cmdlet 時,系統會提示您輸入密碼。 您也可以使用私鑰檔案搭配 KeyFilePath 參數使用 SSH 金鑰驗證。 建立 SSH 驗證的金鑰會因平臺而異。

一般設定資訊

PowerShell 6 或更新版本,且 SSH 必須安裝在所有電腦上。 同時安裝 SSH 用戶端 (ssh.exe) 和伺服器 (sshd.exe),讓您可以從電腦進行遠端存取。 Windows 10 組建 1809 和 Windows Server 2019 現已提供 OpenSSH for Windows。 如需詳細資訊,請參閱 使用 OpenSSH 管理 Windows。 針對 Linux,請安裝 SSH,包括適用於您平臺的 sshd 伺服器。 您也需要從 GitHub 安裝 PowerShell,以取得 SSH 遠端功能。 SSH 伺服器必須設定為建立 SSH 子系統,以在遠端電腦上裝載 PowerShell 進程。 而且,您必須啟用 密碼金鑰型 驗證。

在 Windows 電腦上安裝 SSH 服務

  1. 安裝最新版的PowerShell。 如需詳細資訊,請參閱 在 Windows 上安裝 PowerShell。

    您可以列出 New-PSSession 參數集,確認 PowerShell 具有 SSH 遠端支援。 您會發現有以 SSH 開頭的參數集名稱。 這些參數集包含 SSH 參數。

    (Get-Command New-PSSession).ParameterSets.Name
    
    Name
    ----
    SSHHost
    SSHHostHashParam
    
  2. 安裝最新的 Win32 OpenSSH。 如需安裝指示,請參閱 開始使用OpenSSH

    注意

    如果您想要將PowerShell設定為OpenSSH的預設殼層,請參閱 設定OpenSSH的 Windows。

  3. sshd_config編輯位於的$env:ProgramData\ssh檔案。

    請確定已啟用密碼驗證:

    PasswordAuthentication yes
    

    在遠端電腦上建立載入 PowerShell 行程的 SSH 子系統:

    Subsystem powershell c:/progra~1/powershell/7/pwsh.exe -sshs -nologo
    

    注意

    從 PowerShell 7.4 開始,您不再需要在 SSH 伺服器模式中執行 PowerShell 時使用 -nologo 參數。

    注意

    PowerShell 可執行檔案的預設位置是 c:/progra~1/powershell/7/pwsh.exe。 位置可能會因您安裝PowerShell的方式而有所不同。

    您必須針對包含空格的任何檔案路徑使用8.3簡短名稱。 適用於 Windows 的 OpenSSH 有一個 Bug,可防止空格在子系統可執行文件路徑中運作。 如需詳細資訊,請參閱這個 GitHub 問題。

    Windows 中資料夾的 Program Files 8.3 簡短名稱通常是 Progra~1。 不過,您可以使用下列命令來確定:

    Get-CimInstance Win32_Directory -Filter 'Name="C:\\Program Files"' |
      Select-Object EightDotThreeFileName
    
    EightDotThreeFileName
    ---------------------
    c:\progra~1
    

    選擇性地啟用金鑰驗證:

    PubkeyAuthentication yes
    

    如需詳細資訊,請參閱 管理 OpenSSH 金鑰

  4. 重新啟動 sshd 服務。

    Restart-Service sshd
    
  5. 將 OpenSSH 安裝所在的路徑新增至您的 Path 環境變數。 例如: C:\Program Files\OpenSSH\ 。 這個項目允許 ssh.exe 找到 。

在Ubuntu Linux電腦上安裝SSH服務

  1. 安裝最新版本的 PowerShell,請參閱 在 Ubuntu 上安裝 PowerShell。

  2. 安裝 Ubuntu OpenSSH 伺服器

    sudo apt install openssh-client
    sudo apt install openssh-server
    
  3. sshd_config 位置 /etc/ssh編輯檔案。

    請確定已啟用密碼驗證:

    PasswordAuthentication yes
    

    選擇性地啟用金鑰驗證:

    PubkeyAuthentication yes
    

    如需在Ubuntu上建立 SSH 金鑰的詳細資訊,請參閱 ssh-keygenmanpage。

    新增 PowerShell 子系統專案:

    Subsystem powershell /usr/bin/pwsh -sshs -nologo
    

    注意

    PowerShell 可執行檔案的預設位置是 /usr/bin/pwsh。 位置可能會因您安裝PowerShell的方式而有所不同。

    注意

    從 PowerShell 7.4 開始,您不再需要在 SSH 伺服器模式中執行 PowerShell 時使用 -nologo 參數。

  4. 重新啟動 ssh 服務。

    sudo systemctl restart sshd.service
    

在 macOS 電腦上安裝 SSH 服務

  1. 安裝最新版的PowerShell。 如需詳細資訊, 請在macOS上安裝PowerShell。

    請依照下列步驟確定已啟用 SSH 遠端處理:

    1. 開啟 [System Settings]。
    2. 按一下 General
    3. 按一下 Sharing
    4. 檢查 Remote Login 以設定 Remote Login: On
    5. 允許存取適當的使用者。
  2. sshd_config 位置 /private/etc/ssh/sshd_config編輯檔案。

    使用文字編輯器,例如 nano

    sudo nano /private/etc/ssh/sshd_config
    

    請確定已啟用密碼驗證:

    PasswordAuthentication yes
    

    新增 PowerShell 子系統專案:

    Subsystem powershell /usr/local/bin/pwsh -sshs -nologo
    

    注意

    PowerShell 可執行檔案的預設位置是 /usr/local/bin/pwsh。 位置可能會因您安裝PowerShell的方式而有所不同。

    注意

    從 PowerShell 7.4 開始,您不再需要在 SSH 伺服器模式中執行 PowerShell 時使用 -nologo 參數。

    選擇性地啟用金鑰驗證:

    PubkeyAuthentication yes
    
  3. 重新啟動 sshd 服務。

    sudo launchctl stop com.openssh.sshd
    sudo launchctl start com.openssh.sshd
    

注意

當您升級作業系統時,可能會覆寫 SSH 組態檔。 請務必在升級之後檢查組態檔。

驗證

透過 SSH 的 PowerShell 遠端處理依賴 SSH 用戶端與 SSH 服務之間的驗證交換,而且不會實作任何驗證配置本身。 結果是任何已設定的驗證配置,包括多重要素驗證都是由 SSH 處理,且與 PowerShell 無關。 例如,您可以設定 SSH 服務以要求公鑰驗證和一次性密碼,以增加安全性。 多重要素驗證的設定超出本文件的範圍。 請參閱 SSH 檔,以瞭解如何正確設定多重要素驗證,並驗證它在 PowerShell 外部運作,然後再嘗試搭配 PowerShell 遠端處理使用。

注意

使用者會在遠程會話中保留相同的許可權。 也就是說,管理員 istrators 可以存取提升許可權的殼層,而一般使用者則無法存取。

PowerShell 遠程範例

測試遠端的最簡單方式是在單一計算機上試用。 在此範例中,我們會建立遠程會話回到相同的 Linux 計算機。 我們以互動方式使用PowerShell Cmdlet,因此我們看到SSH中的提示要求驗證主計算機,並提示輸入密碼。 您可以在 Windows 電腦上執行相同動作,以確保遠端功能正常運作。 然後,藉由變更主機名在計算機之間遠端。

Linux 到 Linux

$session = New-PSSession -HostName UbuntuVM1 -UserName TestUser
The authenticity of host 'UbuntuVM1 (9.129.17.107)' can't be established.
ECDSA key fingerprint is SHA256:2kCbnhT2dUE6WCGgVJ8Hyfu1z2wE4lifaJXLO7QJy0Y.
Are you sure you want to continue connecting (yes/no)?
TestUser@UbuntuVM1s password:
$session
 Id Name   ComputerName    ComputerType    State    ConfigurationName     Availability
 -- ----   ------------    ------------    -----    -----------------     ------------
  1 SSH1   UbuntuVM1       RemoteMachine   Opened   DefaultShell             Available
Enter-PSSession $session
[UbuntuVM1]: PS /home/TestUser> uname -a
Linux TestUser-UbuntuVM1 4.2.0-42-generic 49~16.04.1-Ubuntu SMP Wed Jun 29 20:22:11 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

[UbuntuVM1]: PS /home/TestUser> Exit-PSSession
Invoke-Command $session -ScriptBlock { Get-Process pwsh }
Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName    PSComputerName
-------  ------    -----      -----     ------     --  -- -----------    --------------
      0       0        0         19       3.23  10635 635 pwsh           UbuntuVM1
      0       0        0         21       4.92  11033 017 pwsh           UbuntuVM1
      0       0        0         20       3.07  11076 076 pwsh           UbuntuVM1

Linux 到 Windows

Enter-PSSession -HostName WinVM1 -UserName PTestName
PTestName@WinVM1s password:
[WinVM1]: PS C:\Users\PTestName\Documents> cmd /c ver
Microsoft Windows [Version 10.0.10586]

Windows 到 Windows

C:\Users\PSUser\Documents>pwsh.exe
PowerShell
Copyright (c) Microsoft Corporation. All rights reserved.
$session = New-PSSession -HostName WinVM2 -UserName PSRemoteUser
The authenticity of host 'WinVM2 (10.13.37.3)' can't be established.
ECDSA key fingerprint is SHA256:kSU6slAROyQVMEynVIXAdxSiZpwDBigpAF/TXjjWjmw.
Are you sure you want to continue connecting (yes/no)?
Warning: Permanently added 'WinVM2,10.13.37.3' (ECDSA) to the list of known hosts.
PSRemoteUser@WinVM2's password:
$session
 Id Name            ComputerName    ComputerType    State         ConfigurationName     Availability
 -- ----            ------------    ------------    -----         -----------------     ------------
  1 SSH1            WinVM2          RemoteMachine   Opened        DefaultShell             Available
Enter-PSSession -Session $session
[WinVM2]: PS C:\Users\PSRemoteUser\Documents> $PSVersionTable

Name                           Value
----                           -----
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
SerializationVersion           1.1.0.1
BuildVersion                   3.0.0.0
CLRVersion
PSVersion                      6.0.0-alpha
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
GitCommitId                    v6.0.0-alpha.17


[WinVM2]: PS C:\Users\PSRemoteUser\Documents>

限制

  • sudo 命令無法在Linux電腦的遠端會話中運作。

  • 透過 SSH 的 PSRemoting 不支援設定檔,而且無法存取 $PROFILE。 在工作階段中之後,您可以透過點來源配置檔與完整檔案路徑來載入設定檔。 這與 SSH 配置檔無關。 您可以將 SSH 伺服器設定為使用 PowerShell 作為預設殼層,並透過 SSH 載入設定檔。 如需詳細資訊,請參閱 SSH 檔。

  • 在 PowerShell 7.1 之前,透過 SSH 進行遠端處理不支援第二個躍點遠端會話。 這項功能僅限於使用 WinRM 的會話。 PowerShell 7.1 允許 Enter-PSSessionEnter-PSHostProcess 從任何互動式遠端會話內運作。

另請參閱