How to use SSH in Powershell

Who can imagine 10 years ago that today SSH would work from Windows native program?

Most of us when we want to connect to a Linux Server use the SSH with an application like Putty to establish the connection.

Today we can use SSH in PowerShell to create an SSH connection between Windows and Linux ,Linux and Windows or Windows to Windows.

I will explain step by step how can use OpenSSH or SSH in PowerShell Remoting to create an SSH connection from Windows to  Linux or Linux to Windows.

 

Prerequisites to use SSH in Powershell

Make sure that meet  the prerequisites before trying to use ssh in PowerShell

  • Windows 10 v1809 and later
  • PowerShell v6+
  • Open the Port 22 in your Firewall

 

How to use SSH with PowerShell in Windows

Th faster way to use SSH in PowerShell is to install the OpenSSH Client or Server in Windows/Linux.

Then you can establish an SSH connection from Windows to Linux and vice versa.

If you have Windows 10 v1809 and prior then you can read my article that explains step by step how can install the SSH Client in Windows 10.

How to install Open SSH in Windows 10 as Optional Feature

So let's take it from the beginning and follow the steps

#Install the OpenSSH Server and OpenSSH Client in Centos

  • Run the following command to install the open SSH server and the open SSH client.
    yum –y install OpenSSH-server OpenSSH-clients

install openssh in Centos

 

  • Now run the following commands to allow the sshd service to run automatically after a restart, and start the sshd service.
    systemctl enable sshd
    systemctl start sshd
start sshd service in Centos

 

By default after the installation finish successfully you can establish the connection with the password. But we can change it to use a public/private key which is more secure.

  • For now let’s use the SSH with the password to test that all works fine.
  • From the Windows machine open the PowerShell and write the command as ssh <username>@<ipaddress>
    ssh root@192.168.37.8
  • Type the password of the user and the connection will be established
use ssh in windows

 

If we don’t use the <username> then the local account of the machine trying to do the connection will be used by default.

 

#Create SSH Connection from Linux to Windows

If you would like to connect with SSH  from a Linux machine to Windows you can read my article which describes step by step all the settings that need to be done in Windows.

How to install and configure Open SSH Server in Windows 10

 

 

How to use SSH over PowerShell Remoting

From PowerShell 6 and later SSH can be supported over PowerShell remoting which can use it on Windows or Linux as well. 

SSH remoting will do basic PowerShell session between Windows and Linux.

In order to create an SSH connection over a PowerShell remoting you must use the following commands including the new parameter -Hostname instead of Computername.

  • New-PSsession
  • Enter-Pssession
  • Invoke-command

The prerequisites to establish an SSH Connection over a Powershell Remoting is to install the Powershell 6 or higher and OpenSSH Client and Server to allow remote connection to and from the computer.

Note that if you want only to establish SSH connections from a specific machine you can install only the OpenSSH Client.

But if you want remote connection to and from the machines then you should install both(Server/client).

Let's do a little practice

 

#How to Install PowerShell in Windows 

You can use this single command in Windows Powershell to install the Powershell 7.

iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"

or you can download the msi package to install Manual.

You can find the links from Microsoft Docs Installing PowerShell on Windows

 

Hornet Security in-content Banner

 

#How to Install PowerShell in Linux

You can use the following command to install the PowerShell 7 in Linux.

yum install -y powershell

 

Are you finish the PowerShell installation in your machines ?

Then continue with the following steps to establish the connection

  • First of all you must configure the sshd_config in all machines that you would like to use SSH over Powershell remoting.
  • So in linux type vi /etc/ssh/sshd_config
  • Go at the end of the file and find the override default of no subsystems
  • Add the following line

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

 

  • Restart the sshd service with the command
    sysemctl restart sshd 
  • Now login on the Windows machines that you have installed the powershell.
  • Open the sshd_config. You will find it in C:\Programmdata\ssh
  • Go at the end of the file and find the line #override default of no subsystems
  • Add the line 

    Subsystem powershell pwsh.exe -sshs -NoLogo -NoProfile

 

 

  • Restart the sshd service with the following command in Powershell
    Restart-Service -Name sshd
  • Now that you have finished with the configuration it's time to test the connectivity.
  • From the Powershell 6 or 7 depends what you have install type the following command
    New-PSsession -hostname <ip or computername>
  • As you can see with the parameter -Hostname the Transport is SSH

 

 

PowerShell is a multiplatform tool that you can use it with Windows and Linux.

You don't need to use different tools to connect with SSH in Linux.

 

I hope my article helps you or explore something new or resolve a problem.

Have a nice weekend !!

I invite you to follow me on Twitter or Facebook. If you have any questions, send me an email at info@askme4tech.com