47

Setup PowerShell SSH Remoting in PowerShell 6

 5 years ago
source link: https://www.tuicool.com/articles/hit/fyeENjZ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

With PowerShell version 6, Microsoft introduced PowerShell Remoting over SSH, which allows true multiplatform PowerShell remoting between Linux, macOS and Windows. PowerShell SSH Remoting creates a PowerShell host process on the target machine as an SSH subsystem. Normally, PowerShell remoting uses WinRM for connection negotiation and data transport, however WinRM is only available on Windows based machines.

There are also some downsides to it. SSH-based remoting doesn’t currently support remote endpoint configuration and JEA ( Just Enough Administration ). It is also important to understand, that this is not just another PowerShell SSH client .

Use SSH Transport with PowerShell Remoting

To use PowerShell remoting with SSH you can use the same cmdlets, you know from PowerShell remoting with WinRM.

  • New-PSSession
  • Enter-PSSession
  • Invoke-Command

There are 3 new parameters for these cmdlets, if you are using PowerShell SSH remoting.

  • -HostName (Instead of -Computername, you define the SSH target)
  • -UserName (Instead of -Credentials you use the -UserName parameter)
  • -KeyFilePath (If you are using SSH key authentication you can use the -KeyFilePath parameter to point to the key file)
New-PSSession -HostName tomsssh.server.com -UserName thomas

Setup PowerShell SSH Remoting

To work with PowerShell SSH, you will need to complete the following steps on all systems and machines.

  • Install OpenSSH Server and Client
    • OpenSSH for Windows is available directly inWindows 10 (1809) andWindows Server 2019 as an optional feature.
    • On Linux you install OpenSSH depending on your platform
  • Install PowerShell 6 on all systems
  • Configure the SSH subsystem to host a PowerShell process on the remote machine
  • Configure password or key-based authentication

Step by step setup SSH remoting on Windows

Step 1: First you will need to install PowerShell 6 on Windows. You can follow my blog post to easilyinstall PowerShell 6.

Install-PowerShell-6-768x361.jpg

Step 2: Install OpenSSH Client and Open SSH Server. To install client and server and initially configure the OpenSSH server, you can use the following command.

# Install the OpenSSH Client and Server
 
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
 
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
 
# Initial Configuration of SSH Server
 
Start-Service sshd
 
Set-Service -Name sshd -StartupType 'Automatic'
 
# Confirm the Firewall rule is configured. It should be created automatically by setup.
 
Get-NetFirewallRule -Name *ssh*
 
# There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled

Step 2.1:Since there is currently a bug in OpenSSH for Windows, the path do not accept spaces. You need to create a symlink to a directory without spaces. Run the following command on the target system.

mklink /D c:\pwsh "C:\Program Files\PowerShell\6"

Step 3: Configure and edit the sshd_config file located at $env:ProgramData\ssh on the target machine.

Edit-sshd_config-768x565.jpg

Check that password authentication is enabled

PasswordAuthentication yes

Add the Subsystem for PowerShell

Subsystem    powershell c:\pwsh\pwsh.exe -sshs -NoLogo -NoProfile

Optional enable key authentication

PubkeyAuthentication yes

If you are interested in setting up key based authentication with OpenSSH on Windows Server , check out the blog post on ITOpstalk.com by Orin Thomas (Microsoft Cloud Advocate).

Step 4: Restart the sshd service

Restart-Service sshd

Step by step Setup SSH remoting on Linux (Ubuntu 18.04)

Step 1: First you will need to install PowerShell 6 on Linux. You can follow my blog post to easilyinstall PowerShell 6.

Install-PowerShell-6-Linux-768x487.jpg

Step 2: Install OpenSSH Client and Open SSH Server. To install client and server and initially configure the OpenSSH server, you can use the following commands.

sudo apt install openssh-client
sudo apt install openssh-server

Step 3: Configure and edit the sshd_config file at location /etc/ssh on the target machine.

Check that password authentication is enabled

PasswordAuthentication yes

Add the Subsystem for PowerShell

Subsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile

Optional enable key authentication

PubkeyAuthentication yes

Step 4: Restart the sshd service

sudo service sshd restart

SSH Remoting with PowerShell 6

Now you can start using PowerShell SSH remoting to connect even from different operating systems like Windows to Linux or Linux to Windows. PowerShell-6-SSH-Remoting-768x358.jpg

As mentioned before, you can use the same commands as you are familiar with like New-PSSession, Enter-PSSession or Invoke-Command.

PowerShell-SSH-Remoting-Linux-to-Windows-768x351.jpg

As a client you can also use theAzure Cloud Shell, which runs PowerShell 6

Of course, you can also use the PowerShell SSH remoting to connect from Linux to Linux and Windows to Windows. You can find more information about PowerShell SSH remoting on the Microsoft Docs pages. If you have any questions, please let me know in the comments.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK