PowerShell-Remoting über SSHPowerShell remoting over SSH
ÜbersichtOverview
In der Regel wird beim PowerShell-Remoting für die Aushandlung der Verbindung und den Datentransport WinRM verwendet.PowerShell remoting normally uses WinRM for connection negotiation and data transport. SSH ist jetzt für Linux- und Windows-Plattformen verfügbar und ermöglicht echtes PowerShell-Remoting für mehrere Plattformen.SSH is now available for Linux and Windows platforms and allows true multiplatform PowerShell remoting.
WinRM bietet ein stabiles Hostingmodell für PowerShell-Remotesitzungen.WinRM provides a robust hosting model for PowerShell remote sessions. SSH-basiertes Remoting unterstützt derzeit nicht die Remotekonfiguration von Endpunkten und JEA (Just Enough Administration, minimale Verwaltung).SSH-based remoting doesn't currently support remote endpoint configuration and Just Enough Administration (JEA).
Mithilfe von SSH-Remoting können Sie grundlegendes PowerShell-Remoting von Sitzungen zwischen Windows- und Linux-Computern ausführen.SSH remoting lets you do basic PowerShell session remoting between Windows and Linux computers. SSH-Remoting erstellt einen PowerShell-Hostprozess als SSH-Subsystem auf dem Zielcomputer.SSH remoting creates a PowerShell host process on the target computer as an SSH subsystem. Schließlich implementieren wir ein allgemeines mit WinRM vergleichbares Hostingmodell, um die Endpunktkonfiguration und JEA zu unterstützen.Eventually we'll implement a general hosting model, similar to WinRM, to support endpoint configuration and JEA.
Die Cmdlets New-PSSession
, Enter-PSSession
und Invoke-Command
verfügen nun über einen neuen Parameter, der diese neue Remotingverbindung unterstützt.The New-PSSession
, Enter-PSSession
, and Invoke-Command
cmdlets now have a new parameter set to support this new remoting connection.
[-HostName <string>] [-UserName <string>] [-KeyFilePath <string>]
Um eine Remotesitzung zu erstellen, geben Sie den Zielcomputer mit dem Parameter HostName an und fügen mit UserName den Benutzernamen hinzu.To create a remote session, you specify the target computer with the HostName parameter and provide the user name with UserName . Wenn Sie die Cmdlets interaktiv ausführen, werden Sie zur Kennworteingabe aufgefordert.When running the cmdlets interactively, you're prompted for a password. Sie können die SSH-Schlüsselauthentifizierung auch mithilfe einer privaten Schlüsseldatei und des Parameters KeyFilePath einrichten.You can also use SSH key authentication using a private key file with the KeyFilePath parameter. Das Erstellen von Schlüsseln für die SSH-Authentifizierung unterscheidet sich je nach Plattform.Creating keys for SSH authentication varies by platform.
Allgemeine SetupinformationenGeneral setup information
PowerShell 6 oder höher und SSH müssen auf allen Computern installiert sein.PowerShell 6 or higher, and SSH must be installed on all computers. Installieren Sie den SSH-Client (ssh.exe
) und -Server (sshd.exe
), damit Sie Remoting zwischen den Computern nutzen können.Install both the SSH client (ssh.exe
) and server (sshd.exe
) so that you can remote to and from the computers. OpenSSH für Windows ist jetzt im Windows 10-Build 1809 sowie in Windows Server 2019 verfügbar.OpenSSH for Windows is now available in Windows 10 build 1809 and Windows Server 2019. Weitere Informationen finden Sie unter Installation von OpenSSH für Windows Server 2019 und Windows 10.For more information, see Manage Windows with OpenSSH. Installieren Sie für Linux SSH, einschließlich SSHD-Server, entsprechend Ihrer Plattform.For Linux, install SSH, including sshd server, that's appropriate for your platform. Sie müssen auch PowerShell über GitHub installieren, um das SSH-Remotingfeature zu erhalten.You also need to install PowerShell from GitHub to get the SSH remoting feature. Der SSH-Server muss konfiguriert werden, um ein SSH-Subsystem zum Hosten eines PowerShell-Prozesses auf dem Remotecomputer zu erstellen.The SSH server must be configured to create an SSH subsystem to host a PowerShell process on the remote computer. Sie müssen auch eine kennwort- oder schlüsselbasierte Authentifizierung konfigurieren und aktivieren.And, you must enable password or key-based authentication.
Einrichten auf einem Windows-ComputerSet up on a Windows computer
Installieren Sie die neueste Version von PowerShell.Install the latest version of PowerShell. Weitere Informationen finden Sie unter Installieren von PowerShell Core unter Windows.For more information, see Installing PowerShell Core on Windows.
Sie können bestätigen, dass PowerShell SSH-Remoting unterstützt, indem Sie die
New-PSSession
-Parametersätze auflisten.You can confirm that PowerShell has SSH remoting support by listing theNew-PSSession
parameter sets. Sie werden sehen, dass die Namen einiger Parametersätze mit SSH beginnen.You'll notice there are parameter set names that begin with SSH . Diese Parametersätze enthalten SSH -Parameter.Those parameter sets include SSH parameters.(Get-Command New-PSSession).ParameterSets.Name
Name ---- SSHHost SSHHostHashParam
Installieren Sie die neueste Win32-OpenSSH.Install the latest Win32 OpenSSH. Weitere Installationsanweisungen finden Sie unter Getting started with OpenSSH (Erste Schritte mit OpenSSH).For installation instructions, see Getting started with OpenSSH.
Hinweis
Wenn Sie PowerShell als Standardshell für OpenSSH festlegen möchten, finden Sie weitere Informationen unter Konfigurieren der Standardshell für OpenSSH in Windows.If you want to set PowerShell as the default shell for OpenSSH, see Configuring Windows for OpenSSH.
Bearbeiten Sie die
sshd_config
-Datei, die sich hier befindet:$env:ProgramData\ssh
.Edit thesshd_config
file located at$env:ProgramData\ssh
.Vergewissern Sie sich, dass die Kennwortauthentifizierung aktiviert ist:Make sure password authentication is enabled:
PasswordAuthentication yes
Erstellen Sie das SSH-Subsystem, das einen PowerShell-Prozess auf dem Remotecomputer hostet:Create the SSH subsystem that hosts a PowerShell process on the remote computer:
Subsystem powershell c:/progra~1/powershell/7/pwsh.exe -sshs -NoLogo
Hinweis
Der Standardspeicherort der ausführbaren PowerShell-Datei ist
c:/progra~1/powershell/7/pwsh.exe
.The default location of the PowerShell executable isc:/progra~1/powershell/7/pwsh.exe
. Der Speicherort kann abhängig von der installierten PowerShell-Instanz variieren.The location can vary depending on how you installed PowerShell.Sie müssen den kurzen Namen von Version 8.3 für alle Dateipfade verwenden, die Leerzeichen enthalten.You must use the 8.3 short name for any file paths that contain spaces. Es besteht ein Fehler in OpenSSH für Windows, der verhindert, dass Leerzeichen in ausführbaren Pfaden zu Subsystemen funktionieren.There's a bug in OpenSSH for Windows that prevents spaces from working in subsystem executable paths. Weitere Informationen finden Sie in diesem GitHub-Problem.For more information, see this GitHub issue.
Der kurze Name von Version 8.3 für den Ordner
Program Files
unter Windows lautet üblicherweiseProgra~1
.The 8.3 short name for theProgram Files
folder in Windows is usuallyProgra~1
. Sie können jedoch den folgenden Befehl verwenden, um dies sicherzustellen:However, you can use the following command to make sure:Get-CimInstance Win32_Directory -Filter 'Name="C:\\Program Files"' | Select-Object EightDotThreeFileName
EightDotThreeFileName --------------------- c:\progra~1
Aktivieren Sie ggf. die Schlüsselauthentifizierung:Optionally, enable key authentication:
PubkeyAuthentication yes
Weitere Informationen finden Sie unter OpenSSH-Schlüsselverwaltung.For more information, see Managing OpenSSH Keys.
Starten Sie den sshd -Dienst neu.Restart the sshd service.
Restart-Service sshd
Fügen Sie den Pfad der OpenSSH-Installation der Umgebungsvariablen „Path“ hinzu.Add the path where OpenSSH is installed to your Path environment variable. Beispiel:
C:\Program Files\OpenSSH\
.For example,C:\Program Files\OpenSSH\
. Durch diesen Eintrag kann die Dateissh.exe
gefunden werden.This entry allows for thessh.exe
to be found.
Einrichten auf einem Linux-Computer unter Ubuntu 16.04Set up on an Ubuntu 16.04 Linux computer
Weitere Informationen zur Installation der neuesten Version von PowerShell finden Sie unter Installieren von PowerShell Core unter Linux.Install the latest version of PowerShell, see Installing PowerShell Core on Linux.
Installieren Sie Ubuntu OpenSSH Server.Install Ubuntu OpenSSH Server.
sudo apt install openssh-client sudo apt install openssh-server
Bearbeiten Sie die Datei
sshd_config
unter/etc/ssh
.Edit thesshd_config
file at location/etc/ssh
.Vergewissern Sie sich, dass die Kennwortauthentifizierung aktiviert ist:Make sure password authentication is enabled:
PasswordAuthentication yes
Aktivieren Sie ggf. die Schlüsselauthentifizierung:Optionally, enable key authentication:
PubkeyAuthentication yes
Weitere Informationen zum Erstellen von SSH-Schlüsseln unter Ubuntu finden Sie auf der manpage für ssh-keygen.For more information about creating SSH keys on Ubuntu, see the manpage for ssh-keygen.
Fügen Sie einen Eintrag für das PowerShell-Subsystem hinzu:Add a PowerShell subsystem entry:
Subsystem powershell /usr/bin/pwsh -sshs -NoLogo
Hinweis
Der Standardspeicherort der ausführbaren PowerShell-Datei ist
/usr/bin/pwsh
.The default location of the PowerShell executable is/usr/bin/pwsh
. Der Speicherort kann abhängig von der installierten PowerShell-Instanz variieren.The location can vary depending on how you installed PowerShell.Aktivieren Sie ggf. die Schlüsselauthentifizierung:Optionally, enable key authentication:
PubkeyAuthentication yes
Führen Sie einen Neustart des SSH -Diensts aus.Restart the ssh service.
sudo service ssh restart
Einrichten auf einem macOS-ComputerSet up on a macOS computer
Installieren Sie die neueste Version von PowerShell.Install the latest version of PowerShell. Weitere Informationen finden Sie unter Installieren von PowerShell Core unter macOS.For more information, Installing PowerShell Core on macOS.
Vergewissern Sie sich, dass SSH-Remoting aktiviert ist, indem Sie die folgenden Schritte ausführen:Make sure SSH Remoting is enabled by following these steps:
- Öffnen Sie
System Preferences
.OpenSystem Preferences
. - Klicken Sie auf
Sharing
.Click onSharing
. - Aktivieren Sie
Remote Login
, umRemote Login: On
festzulegen.CheckRemote Login
to setRemote Login: On
. - Erteilen Sie den entsprechenden Benutzern Zugriff.Allow access to the appropriate users.
- Öffnen Sie
Bearbeiten Sie die Datei
sshd_config
unter/private/etc/ssh/sshd_config
.Edit thesshd_config
file at location/private/etc/ssh/sshd_config
.Verwenden Sie einen Text-Editor, z.B. nano :Use a text editor such as nano :
sudo nano /private/etc/ssh/sshd_config
Vergewissern Sie sich, dass die Kennwortauthentifizierung aktiviert ist:Make sure password authentication is enabled:
PasswordAuthentication yes
Fügen Sie einen Eintrag für das PowerShell-Subsystem hinzu:Add a PowerShell subsystem entry:
Subsystem powershell /usr/local/bin/pwsh -sshs -NoLogo
Hinweis
Der Standardspeicherort der ausführbaren PowerShell-Datei ist
/usr/local/bin/pwsh
.The default location of the PowerShell executable is/usr/local/bin/pwsh
. Der Speicherort kann abhängig von der installierten PowerShell-Instanz variieren.The location can vary depending on how you installed PowerShell.Aktivieren Sie ggf. die Schlüsselauthentifizierung:Optionally, enable key authentication:
PubkeyAuthentication yes
Starten Sie den sshd -Dienst neu.Restart the sshd service.
sudo launchctl stop com.openssh.sshd sudo launchctl start com.openssh.sshd
AuthentifizierungAuthentication
PowerShell-Remoting über SSH basiert auf dem Authentifizierungsaustausch zwischen dem SSH-Client und dem SSH-Dienst und implementiert selbst keine Authentifizierungsschemas.PowerShell remoting over SSH relies on the authentication exchange between the SSH client and SSH service and doesn't implement any authentication schemes itself. Infolgedessen werden alle konfigurierten Authentifizierungsschemas, einschließlich der mehrstufigen Authentifizierung, von SSH und unabhängig von PowerShell verarbeitet.The result is that any configured authentication schemes including multi-factor authentication are handled by SSH and independent of PowerShell. Sie können den SSH-Dienst beispielsweise für die Authentifizierung mit öffentlichen Schlüsseln oder die Verwendung eines Einmalkennworts zur Erhöhung der Sicherheit konfigurieren.For example, you can configure the SSH service to require public key authentication and a one-time password for added security. Die Konfiguration einer mehrstufigen Authentifizierung wird in dieser Dokumentation nicht beschrieben.Configuration of multi-factor authentication is outside the scope of this documentation. Bevor Sie versuchen, eine mehrstufige Authentifizierung mit PowerShell-Remoting zu verwenden, informieren Sie sich in der Dokumentation für SSH, wie Sie eine mehrstufige Authentifizierung richtig konfigurieren, und testen Sie, ob diese außerhalb von PowerShell funktioniert.Refer to documentation for SSH on how to correctly configure multi-factor authentication and validate it works outside of PowerShell before attempting to use it with PowerShell remoting.
Hinweis
Benutzer behalten dieselben Berechtigungen in Remotesitzungen bei.Users retain the same privileges in remote sessions. Das bedeutet, dass Administratoren Zugriff auf eine Shell mit erhöhten Rechten haben und normale Benutzer nicht.Meaning, Administrators have access to an elevated shell, and normal users will not.
Beispiel für das PowerShell-RemotingPowerShell remoting example
Sie können das Remoting am einfachsten auf einem einzelnen Computer testen.The easiest way to test remoting is to try it on a single computer. In diesem Beispiel haben wir eine Remotesitzung mit demselben Linux-Computer erstellt.In this example, we create a remote session back to the same Linux computer. Wir verwenden PowerShell-Cmdlets interaktiv, damit wir Eingabeaufforderungen von SSH sehen, die zum Überprüfen des Hostcomputers und zur Kennworteingabe auffordern.We're using PowerShell cmdlets interactively so we see prompts from SSH asking to verify the host computer and prompting for a password. Sie können dies auch auf einem Windows-Computer durchführen, um sicherzustellen, dass das Remoting funktioniert.You can do the same thing on a Windows computer to ensure remoting is working. Verwenden Sie dann Remoting zwischen Computern, indem Sie den Hostnamen ändern.Then, remote between computers by changing the host name.
# Linux to Linux
#
$session = New-PSSession -HostName UbuntuVM1 -UserName TestUser
The authenticity of host 'UbuntuVM1 (9.129.17.107)' cannot 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 powershell }
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName PSComputerName
------- ------ ----- ----- ------ -- -- ----------- --------------
0 0 0 19 3.23 10635 635 powershell UbuntuVM1
0 0 0 21 4.92 11033 017 powershell UbuntuVM1
0 0 0 20 3.07 11076 076 powershell UbuntuVM1
#
# Linux to 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 to 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>
EinschränkungenLimitations
Der Befehl sudo funktioniert bei Remotesitzungen auf Linux-Computern nicht.The sudo command doesn't work in a remote session to a Linux computer.
PSRemoting über SSH unterstützt keine Profile und hat keinen Zugriff auf
$PROFILE
.PSRemoting over SSH does not support Profiles and does not have access to$PROFILE
. In einer aktiven Sitzung können Sie ein Profil laden, indem Sie das Profil mit dem vollständigen Dateipfad per „dot-source“ angeben.Once in a session, you can load a profile by dot sourcing the profile with the full filepath. Dies steht nicht im Zusammenhang mit SSH-Profilen.This is not related to SSH profiles. Sie können den SSH-Server so konfigurieren, dass PowerShell als Standardshell verwendet und ein Profil über SSH geladen wird.You can configure the SSH server to use PowerShell as the default shell and to load a profile through SSH. Weitere Informationen finden Sie in der SSH-Dokumentation.See the SSH documentation for more information.Vor PowerShell 7.1 unterstützte das Remoting über SSH keine Remotesitzungen über einen zweiten Hop.Prior to PowerShell 7.1, remoting over SSH did not support second-hop remote sessions. Diese Funktion war auf Sitzungen beschränkt, die WinRM verwendeten.This capability was limited to sessions using WinRM. PowerShell 7.1 ermöglicht, dass
Enter-PSSession
undEnter-PSHostProcess
in jeder interaktiven Remotesitzung funktionieren.PowerShell 7.1 allowsEnter-PSSession
andEnter-PSHostProcess
to work from within any interactive remote session.
Weitere InformationenSee also
Installieren von PowerShell Core unter LinuxInstalling PowerShell Core on Linux
Installieren von PowerShell Core unter macOSInstalling PowerShell Core on macOS
Installieren von PowerShell Core unter WindowsInstalling PowerShell Core on Windows
Installation von OpenSSH für Windows Server 2019 und Windows 10Manage Windows with OpenSSH