Enable PowerShell Remoting on Linux using OpenSSH

Linux has always been an important part of the server infrastructure world and SSH has been the primary method of connecting to it. PowerShell can now use same underlying SSH Protocol to perform SSH and manage Linux servers centrally. This functionality allows us to seamlessly incorporate Unix servers using the known PSRemoting commands into the PowerShell scripts. PowerShell based SSH remoting creates a PowerShell host process on the target computer as an SSH subsystem. In this blog post, we’ll learn how we can enable and use the same on one of the popular Linux distributions, Ubuntu. The same set of steps can be used for other Linux distributions as well.

Configure Ubuntu for PSRemoting

We have used Latest LTS flavor, ubuntu 20.04 lts, but same set of steps are needed for older versions.

Installing PowerShell Core

You can install PowerShell Core by following this article.

Install OpenSSH Server

To install OpenSSH, we can run below set of commands:

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

Edit SSH Server Configuration

Edit the sshd_config file at location /etc/ssh by enabling following configuration:

PasswordAuthentication yes
PubkeyAuthentication yes
Subsystem powershell /usr/bin/pwsh -sshs -NoLogo

Save and restart sshd daemon/service.

That’s it. We can now go to powershell client and initiate remoting session.

Performing PSRemoting to Ubuntu

As we mentioned, we can use PSRemoting commands to do SSH based remoting to Linux Server. For example, we can create a PSSession by supplying the target computer IP address or dns name along with the username. Supply password when prompted:

We can now enter PSSession using Enter-PSSession cmdlet. Once connected to terminal, we can run usual linux commands or PowerShell core commands:

Gotchas

sudo command does not work in a PSRemoting session, as evident from below. So the commands needing to be initiated as sudo, cannot be run:

Also, PSRemoting over SSH does not support Profiles and does not have access to $PROFILE. Once in a session, you can load a profile by dot sourcing the profile with the full filepath.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s