PowerShell over SSH
July 22, 2023 · 5 minute read
Introduction
sshd_config
. As a result, it becomes recognized by SSH and enables the remote execution of PowerShell over SSH. Microsoft has an official article on PowerShell remoting over SSH. This article includes in-depth information on configuration and installation. In this case I'll provide a brief overview of installation and configuration.
Windows Configuration
Step One: PowerShell 7 installation
$env:ProgramFiles\PowerShell\7
. The path can also be represented like this: c:/progra~1/powershell/7/pwsh.exe
Step Two: OpenSSH Installation
Check if OpenSSH is already installed
Get-WindowsCapability -Online | Where-Object { $_.Name -like 'OpenSSH*' }
Import-Module NetSecurity
Install OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client
Install OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server
Set the SSH server to start automatically
Set-Service -Name sshd -StartupType 'Automatic'
Lastly, start the SSH Server
Start-Service sshd
#Check Status
Get-Service sshd
Step Three: OpenSSH Configuration
(Get-Command New-PSSession).ParameterSets.Name
Output
Name
----
SSHHost
SSHHostHashParam
sshd_config
to configure the OpenSSH server. By default SSH configuration files are stored at C:\ProgramData\ssh\sshd_config
. To edit the file, make sure you open notepad as an administrator.
sshd_config
:
PasswordAuthentication yes
Subsystem powershell c:/progra~1/powershell/7/pwsh.exe -sshs -nologo
Restart-Service sshd
Linux Configuration
Step One: PowerShell 7 Installation
- Debian/Ubuntu:
sudo apt-get install powershell
- Fedora/CentOS:
sudo yum install powershell
- Arch Linux:
sudo pacman -S powershell
pwsh
in the terminal. To avoid potential issues with versions, ensure that both your target and client machine have the same major version.
Step Two: OpenSSH Installation
- Debian/Ubuntu:
sudo apt-get install openssh-server
- Fedora/CentOS:
sudo yum install openssh-server
- Arch Linux:
sudo pacman -S openssh
Step Three: OpenSSH Configuration
/etc/ssh/sshd_config
. Below are the lines to needed to properly configure the service:
PasswordAuthentication yes
Subsystem powershell /usr/bin/pwsh -sshs -nologo
sudo systemctl restart sshd
Practical Demonstration
Linux to Windows
#Enter powershell
[user@cocoec-linuxbox ~]$ pwsh
PowerShell 7.3.5
A new PowerShell stable release is available: v7.3.6
Upgrade now, or check out the release page at:
https://aka.ms/PowerShell-Release?tag=v7.3.6
#Create remote powershell SSH session
PS /home/user> $session = New-PSSession -Hostname windowsbox -UserName pcadmin
The authenticity of host 'windowsbox (192.168.1.5)' can't be established.
ECDSA key fingerprint is SHA256:2kCbn2kCbn...
Are you sure you want to continue connecting (yes/no)?
pcadmin@windowsbox's password:
PS /home/user>
PS /home/user> $session
Id Name Transport ComputerName ComputerType State ConfigurationName Availability
-- ---- --------- ------------ ------------ ----- ----------------- ------------
2 Runspace1 SSH windowsbox RemoteMachine Opened DefaultShell Available
PS /home/user> Invoke-Command -Session $session -ScriptBlock {hostname}
windowsbox
PS /home/user> Invoke-Command -Session $session -ScriptBlock {$PSVersionTable}
Name Value
---- -----
WSManStackVersion 3.0
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSEdition Core
SerializationVersion 1.1.0.1
Platform Win32NT
GitCommitId 7.3.5
PSVersion 7.3.5
PSRemotingProtocolVersion 2.3
OS Microsoft Windows 10.0.22621
Limitations
sudo
to elevate privileges. This is done intentionally for security reasons.