PowerShell remoting to Linux Servers

Connecting from a Windows Client to a Windows Server using PowerShell is easy, Enter-PSSession -Computername xxxx and you’re in when connecting with enough credentials. But I also have two Linux systems (Ubuntu VPS and a Raspberry Pi4 running Raspian Bullseye) on which PowerShell is also installed. But PowerShell remoting into those systems is not standard and requires a few steps, this blog post will show you how to connect to Linux using PowerShell.

Compatibility

PowerShell remoting into Linux systems only works when using PowerShell 6 (Core) and higher on your client, there’s no support for PowerShell 5.

Preparing the Linux server

For this blog post I used a VMware Workstation Ubuntu 22.04 installation from Linux VM Images (Link), it will also work on other Linux distributions but they could have other package managers, etc. (Yum instead of Apt for example) To enable it for PowerShell remoting you can follow these steps: (I Used this Microsoft Link, you can find more details about other Linux distributions there too on the left side)

Installing PowerShell

  • Log in to the system using SSH or from the (VMware) console (Start a Terminal prompt in that case)
  • Check if PowerShell is already installed by using “sudo apt list powershell”, if it’s not installed you can install it running the command lines below:
# Update the list of packages
sudo apt update
# Install pre-requisite packages.
sudo apt install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of packages after we added packages.microsoft.com
sudo apt update
# Install PowerShell
sudo apt install -y powershell

Enable PowerShell Remoting

  • Start a PowerShell session on Linux by running “sudo pwsh” in a terminal window or a SSH session
  • Type “Install-Module -Name Microsoft.PowerShell.Remotingtools” and press “Y” and “Enter” to install it from the PSGallary
  • Type “Enable-SSHRemoting” and press “Y” and “Enter” to update the sshd_config file with a new line containing “Subsystem powershell /opt/microsoft/powershell/7/pwsh -sshs -nologo -noprofile
  • Type “exit” to close the session
  • Run “sudo service sshd restart” to restart the OpenSSH Server service with the new configuration setting

Disable Message Of The Day (MOTD)

I ran into an issue when connecting my Ubuntu Linux server using PowerShell, the MOTD banner somehow stops me from connecting. If you see a message like this “Enter-PSSession: The background process reported an error with the following message: The SSH client session has ended with error message“, then you can prevent that by the following steps:

  • Log in to the system using SSH or from the (VMware) console (Start a Terminal prompt in that case)
  • Run “sudo nano /etc/ssh/sshd_config
  • Scroll all the way down and change “Banner /etc/motd” to “#Banner /etc/motd“, use CTRL-X, Y and Enter to Save the file and Quit
  • Run “sudo service sshd restart” to restart the OpenSSH Server service with the new configuration setting

Preparing your Windows system

Installing the OpenSSH Client

On Windows Server 2022, Windows Server 2019, and Windows 10 (build 1809 and later) you can install OpenSSH components. In this case, we only need the OpenSSH client, if you don’t already have it installed you can install it on your system using the command below in an Administrative PowerShell session:

Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

Generating an SSH key

To create the SSH key which you will need to connect without entering a password, you can follow these steps:

  • Start a command prompt (Start, Run, cmd)
  • Run “ssh-keygen -t ed25519” and press Enter to keep the default file location (c:\users\accountname\.ssh\id_ed25519), press Enter two times again to use a empty passphrase.
  • The output should look like this:
  • Copy the id_ed25519.pub file to your Linux server by running the command below (change the username from ubuntu to your username and change the IP-address to the IP-address/hostname of your Linux server):
cd .ssh
scp id_ed25519.pub ubuntu@192.168.80.130:~/.ssh
  • After entering your password, the file should be copied to your Linux Server.
  • Log in to the Linux Server using SSH or from the (VMware) console (Start a Terminal prompt in that case)
  • Use the commands below to add the contents from your id_ed25519.pub file to the authorized_keys and remove it from the system afterward:
cd .ssh
cat id_ed25519.pub >> authorized_keys
rm id_ed25519.pub
  • Test if the connection works by running this command on your Windows system (change the username from ubuntu to your username and change the IP-address to the IP-address/hostname of your Linux server):
ssh ubuntu@192.168.80.130
  • It should log in without asking for a password now, and exit the session by typing “exit

Using PowerShell to run commands in Linux remotely

Now that everything is configured, you can run your first test by starting an interactive session with the Linux server. Run this command to get a session with the server: (change the username from ubuntu to your username and change the IP-address to the IP-address/hostname of your Linux server)

Enter-PSSession -HostName ubuntu@192.168.80.130

It should look like this after connecting and running “ls” to list the files in your home directory, it looks/works the same just like connecting a Windows Client or Server.

You can now also use Invoke-Command to run the same “ls” command on the Linux server and return to your PowerShell prompt on your client:

Invoke-Command -HostName ubuntu@192.168.80.130 -ScriptBlock {ls}

And that should look like this:

So… Now you can start using your Linux machine for certain tasks running remote PowerShell commands on it (Or triggering Linux commands too of course). Enjoy! 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.