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. However, I also have two Linux systems (Ubuntu VPS and a Raspberry Pi4 running Raspian Bullseye) on which PowerShell is also installed. However, 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 your client uses PowerShell 6 (Core) and higher (7 is recommended). There is 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. (Check here for supported Ubuntu versions; 24.10 is not yet supported) However, they could have different package managers (Yum instead of Apt, for example). To enable it for PowerShell remoting, you can follow the steps below.

Installing PowerShell

  • Log into the system using SSH. If SSH is unavailable, connect using the (VMWare) console, run sudo apt-get install OpenSSH-server, and follow the prompts.
  • Check if PowerShell is installed using “sudo apt list powershell“. If it’s not installed, you can install it by running the command lines below:
###################################
# Prerequisites

# Update the list of packages
sudo apt-get update

# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common

# Get the version of Ubuntu
source /etc/os-release

# Download the Microsoft repository keys
wget -q https://packages.microsoft.com/config/ubuntu/$VERSION_ID/packages-microsoft-prod.deb

# Register the Microsoft repository keys
sudo dpkg -i packages-microsoft-prod.deb

# Delete the Microsoft repository keys file
rm packages-microsoft-prod.deb

# Update the list of packages after we added packages.microsoft.com
sudo apt-get update

###################################
# Install PowerShell
sudo apt-get install -y powershell

Enable PowerShell Remoting

  • Start a PowerShell session on Linux by running “sudo pwsh” in a terminal window or an 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“. (This will 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 systemctl restart ssh” to restart the OpenSSH Server service with the new configuration setting.

Preparing your Windows system

Installing the OpenSSH Client

You can install OpenSSH components on Windows Server 2022, Windows Server 2019, and Windows 10 (build 1809 and later). 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 an 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 (in that case, Start a Terminal prompt).
  • 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
  • Now, it should log in without asking for a password. 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

After connecting and running “ls” to list the files in your home directory, it should look like this: It looks/works the same as 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, 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.