How to Connect Windows to Linux and Linux to Windows using PowerShell 7 SSH Remoting ( PS Remoting Over SSH)

PowerShell Remoting has various benefits. It started with Windows when Windows administrators use to work remotely work with tons of windows machine over WinRM protocol. With Automation and Unix distribution spreading across the world and require by every single IT engineer, PowerShell introduced PSRemoting over SSH in PowerShell 7 to connect Windows to Linux and Linux to Windows remotely .

In this tutorial we will learn how to setup PS Remoting on windows machine and on Linux machine using PS remoting over SSH ( PowerShell 7 supported) . Finally we will connect both Windows to Linux and Linux to Windows machine. Lets get started.

Table of Content

  1. What is PSRemoting or PowerShell Remoting Over WinRM?
  2. What is PSRemoting or PowerShell Remoting Over SSH?
  3. Prerequisites
  4. Step by step set up SSH remoting on Windows
  5. Step by step set up SSH remoting on Ubuntu
  6. Test the OpenSSH connectivity from Windows machine to Linux using PSRemoting
  7. Test the OpenSSH connectivity from Linux to Windows machine using PSRemoting
  8. Conclusion

What is PSRemoting or PowerShell Remoting?

PowerShell Remoting is a feature of PowerShell. With PowerShell Remoting you can connect with a single or tons of servers at a single time.

PS Remoting Over SSH (Windows to Linux and Windows to Windows)

WS-Management or Web services management or WS-Man provides a common way for systems to access and exchange management information across the IT infrastructure.

Microsoft implemented WS-Management or Web services management or WS-Man in WinRM that is Windows Remote Management that allows hardware and operating systems, from different vendors to connect to each other. For WinRM to obtain data from remote computers, you must configure a WinRM listener. WinRM listener can work on both HTTP or HTTPS Protocols.

PS Remoting Over WinRM (Linux to Windows)

When PowerShell Remoting takes place between two servers that is one server try to run commands remotely on other server, the source server connects to destination server on WinRM Listener. To configure PSRemoting on local machine or remote machine please visit the link

What is PSRemoting or PowerShell Remoting Over SSH?

Microsoft introduced PowerShell 7 Remoting over SSH, which allows true multiplatform PowerShell remoting between Linux, macOS, and Windows. PowerShell SSH Remoting creates a PowerShell host process on the target machine as an SSH subsystem. Normally, Windows PowerShell remoting uses WinRM for connection negotiation and data transport. However, WinRM is only available on Windows-based machines. That means Linux machines can connect with windows or windows can connect to Windows over WinRM but Windows cannot connect to Linux.

With PowerShell 7 Remoting over SSH Now its possible to remote between Linux, macOS, and Windows.

PS Remoting Over SSH ( Windows to Linux , Linux to Windows)

Prerequisites

  • Microsoft Windows Server 2019 standard . This machine should also have PowerShell 7 installed. If you don’t have PowerShell installed please follow here to install.
  • Make sure you have local account setup in Windows server 2019. We will be using “automate” user.
  • Make sure you set the password for ubuntu user on ubuntu machine or if you have it then ignore.
  • Ubuntu machine with PowerShell 7 installed.

Step by step set up SSH remoting on Windows

Here we will discuss about how to setup SSH remoting on Windows Machine and run the PSRemoting commands.

  • Assuming you are on Windows 2019 standard machine with PowerShell 7 installed. Lets verify it once.
  • Before we setup SSH on windows machine & if you try to make a SSH session with Linux machine you will received an error message like this.
  • Next step is to install Open SSH client and server on Windows 2019 standard server. Lets use the PowerShell utility Add-WindowsCapabillity and run the commands.
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
 
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
  • Once Open SSH is installed successfully , we need to start the OpenSSH Services.
Start-Service sshd
Set-Service sshd -StartupType Automatic
  • Now, Edit the OpenSSH configuration sshd_config located in C:\Windows\System32\OpenSSH or you can find it in C:\ProgramData\ssh\sshd_config by adding Subsystem for PowerShell.
Subsystem powershell c:/progra~1/powershell/7/pwsh.exe -sshs -NoLogo -NoProfile
  • Also make sure OpenSSH configuration file sshd_config has PasswordAuthentication set to yes
  • Restart the service
Restart-Service sshd
  • SSH remoting is now properly set on Windows Machine

Step by step set up SSH remoting on Ubuntu

Previously we configured SSH remoting on Windows Machine , now we need to perform similar steps in ubuntu machines with various commands.

  • PowerShell 7 must be installed on ubuntu machine
  • Install OpenSSH client and server on ubuntu machine
sudo apt install openssh-client
sudo apt install openssh-server
  • Similarly Edit the Sshd_config file in ubuntu machine
vi /etc/ssh/sshd_config
  • Paste the below content (Add the Subsystem for PowerShell) and make sure PasswordAuthentication set to yes
Subsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile
  • Restart the service
sudo service sshd restart

Test the OpenSSH connectivity from Windows machine to Linux using PSRemoting

Here now we are set with windows and ubuntu SSH remoting steps , now lets verify the SSH connectivity between from windows to ubuntu machine

Verification Method 1

  • Create a session and then enter into session and run commands from windows PowerShell to Linux PowerShell
New-PSSession -Hostname  54.221.35.44 -UserName ubuntu # Windows to Linux Create Session

Enter-PSSession -Hostname 54.221.35.44 -UserName ubuntu # Windows to Linux Enter Session

Verification Method 2

  • Create the session and then test the connectivity from Windows machine to Linux using Invoke-Command command
$SessionParams = @{
     HostName = "54.221.35.44"
     UserName = "ubuntu"
     SSHTransport = $true
 }
Invoke-Command @SessionParams -ScriptBlock {Get-Process}

Test the OpenSSH connectivity from Linux to Windows machine using PSRemoting

Lets verify the SSH connectivity between from ubuntu machine to windows Machine.

  • Open PowerShell on ubuntu machine with following command
pwsh
  • Although you are on ubuntu machine lets verify the ubuntu version [Optional Step]
  • Now SSH into Windows machine using following command
ssh automate@3.143.233.234
  • Here we go , You can clearly see that we are have SSH into Windows machine successfully

Conclusion

PowerShell Remoting has various benefits. It started with Windows when Windows administrators use to work remotely work with tons of windows machine over WinRM protocol. With Automation and Unix distribution spreading across the world and require by every single IT engineer. To resolve problem of connecting windows to Linux and Linux to windows PowerShell introduced PSRemoting over SSH to connect Windows to Linux and Linux to Windows remotely with easy setups .

Hope you find this tutorial helpful. If you like please share it with your friends.

What is PSRemoting or PowerShell Remoting and how to Enable PS Remoting

PSRemoting or PowerShell Remoting is a PowerShell based remoting which allows you to connect to one or thousands of remote computers and execute commands. PSRemoting allows you to sit at one place and execute commands on remote machine as if you are executing physically on the servers.

In this tutorial you will learn what is PS Remoting that is PowerShell Remoting and how to enable PowerShell Remoting locally and on remote machines.

Table of Content

  1. What is PSRemoting or PowerShell Remoting?
  2. Prerequisites
  3. How to Enable PS Remoting Locally on system?
  4. How to Enable PS Remoting on remote system?
  5. Conclusion

What is PSRemoting or PowerShell Remoting?

PowerShell Remoting is a feature of PowerShell. With PowerShell Remoting you can connect with a single or tons of servers at a single time.

WS-Management or Web services management or WS-Man provides a common way for systems to access and exchange management information across the IT infrastructure.

Microsoft implemented WS-Management or Web services management or WS-Man in WinRM that is Windows Remote Management that allows hardware and operating systems, from different vendors to connect to each other. For WinRM to obtain data from remote computers, you must configure a WinRM listener. WinRM listener can work on both HTTP or HTTPS Protocols.

When PowerShell Remoting takes place between two servers that is one server try to run commands remotely on other server, the source server connects to destination server on WinRM Listener.

How to check WinRM listeners on Windows Host?

To check the WinRM listeners on windows host use the following command

 winrm e winrm/config/listener

Prerequisites

  • Make sure you windows machine with PowerShell 7 installed . If you don’t have, Install it from here.

How to Enable PS Remoting Locally on system?

There are two ways in which you can enable PSRemoting on the local machine.

Use Enable-PSRemoting to Enable PS Remoting Locally on system

  • Invoke the command Enable-PSRemoting and this performs the following function
    • WinRM service is started
    • Creates listener on 5985 for HTTP
    • Registers and Enable PowerShell sessions
    • Set PowerShell sessions to allow remote sessions.
    • Restarts WinRM server

Enable-PSRemoting  # By Default its enabled in Windows
  • On a Server OS, like Windows Server 2019, the firewall rule for Public networks allows on remote connections from other devices on the same network. On a client OS, like Windows 10, you will receive an error stating that you are a public network.
Command Ran on Windows 2019 server
Command Ran on Windows 10 Machine
  • If you want to ignore the Error message because of Network Profile on client like windows 10 use the following command
Enable-PSRemoting -SkipNetworkProfileCheck

Use WinRM to Enable PS Remoting Locally on system

  • We can use WinRM quickconfig command as well to enable PS Remoting on local machine
winrm quickconfig

How to Enable PS Remoting on remote system?

There are two ways in which you can enable PSRemoting on the remote machine.

Use PS exec to Enable PS Remoting on remote system

  • Using PS exec you can run command on remote machine after connecting to remote machine. When you run PS exec command , it initialize the PowerShell session on remote machine and then run the command.
.\psexec.exe \\3.143.113.23 -h -s powershell.exe Enable-PSRemoting -Force # 3.143.113.23 is remote machine's IP address

Use WMI to Enable PS Remoting on remote system

Using PowerShell and the Invoke-CimMethod cmdlet. Using the Invoke-CimMethod cmdlet, you can instruct PowerShell to connect to the remote computer over DCOM and invoke methods.

$SessionArgs = @{
     ComputerName  = 'WIN-U22NTASS3O7'
     Credential    = Get-Credential
     SessionOption = New-CimSessionOption -Protocol Dcom
 }
 $MethodArgs = @{
     ClassName     = 'Win32_Process'
     MethodName    = 'Create'
     CimSession    = New-CimSession @SessionArgs
     Arguments     = @{
         CommandLine = "powershell Start-Process powershell -ArgumentList 'Enable-PSRemoting -Force'"
     }
 }
 Invoke-CimMethod @MethodArgs

Conclusion

In this tutorial, you have learned what is PSRemoting and how to enable PSRemoting with various methods locally on the machine as well as remotely on the machine. This will give you great opportunity to automate with various remote machines together.