- How to use SSH in Powershell
- Prerequisites to use SSH in Powershell
- How to use SSH with PowerShell in Windows
- #Install the OpenSSH Server and OpenSSH Client in Centos
- #Create SSH Connection from Linux to Windows
- How to use SSH over PowerShell Remoting
- #How to Install PowerShell in Windows
- #How to Install PowerShell in Linux
- Accessing network applications with WSL
- Accessing Linux networking apps from Windows (localhost)
- Accessing Windows networking apps from Linux (host IP)
- Connecting via remote IP addresses
- Accessing a WSL 2 distribution from your local area network (LAN)
- IPv6 access
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
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.
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
- 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
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 @
ssh root@192.168.37.8 - Type the password of the user and the connection will be established
If we don’t use the 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 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.
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.
or you can download the msi package to install Manual.
You can find the links from Microsoft Docs Installing PowerShell on Windows
#How to Install PowerShell in Linux
You can use the following command to install the PowerShell 7 in Linux.
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 - 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.
I invite you to follow me on Twitter or Facebook. If you have any questions, send me an email at info@askme4tech.com
Accessing network applications with WSL
There are a few considerations to be aware of when working with networking apps, whether you are accessing a Linux networking app from a Windows app or accessing a Windows networking app from a Linux app, you may need to identify the IP address of the virtual machine you are working with, which will be different than the IP address of your local physical machine.
Accessing Linux networking apps from Windows (localhost)
If you are building a networking app (for example an app running on a NodeJS or SQL server) in your Linux distribution, you can access it from a Windows app (like your Edge or Chrome internet browser) using localhost (just like you normally would).
Accessing Windows networking apps from Linux (host IP)
If you want to access a networking app running on Windows (for example an app running on a NodeJS or SQL server) from your Linux distribution (ie Ubuntu), then you need to use the IP address of your host machine. While this is not a common scenario, you can follow these steps to make it work.
- Obtain the IP address of your host machine by running this command from your Linux distribution: cat /etc/resolv.conf
- Copy the IP address following the term: nameserver .
- Connect to any Windows server using the copied IP address.
The picture below shows an example of this by connecting to a Node.js server running in Windows via curl.
Connecting via remote IP addresses
When using remote IP addresses to connect to your applications, they will be treated as connections from the Local Area Network (LAN). This means that you will need to make sure your application can accept LAN connections.
For example, you may need to bind your application to 0.0.0.0 instead of 127.0.0.1 . In the example of a Python app using Flask, this can be done with the command: app.run(host=’0.0.0.0′) . Please keep security in mind when making these changes as this will allow connections from your LAN.
Accessing a WSL 2 distribution from your local area network (LAN)
When using a WSL 1 distribution, if your computer was set up to be accessed by your LAN, then applications run in WSL could be accessed on your LAN as well.
This isn’t the default case in WSL 2. WSL 2 has a virtualized ethernet adapter with its own unique IP address. Currently, to enable this workflow you will need to go through the same steps as you would for a regular virtual machine. (We are looking into ways to improve this experience.)
Here’s an example Windows command to add a port proxy that listens on port 4000 on the host and connects it to port 4000 to the WSL 2 VM with IP address 192.168.101.100.
netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=192.168.101.100
IPv6 access
WSL 2 distributions currently cannot reach IPv6-only addresses. We are working on adding this feature.