PowerShell Open Source – Windows PSRemoting to Linux with OpenSSH

I have to admit that I was waiting for this for some time. Finally, PowerShell 6.0.0-Alpha_13 can do remoting between Windows to Linux. This is Awesome!

microsoft-loves-linux

I’m going to give you the top-to-bottom scope in order to avoid going back-and-forth between link. So, it’s all here and including tips and print-screens.

Instructions for Windows OpenSSH installation

[sourcecode language=”dos”]
cd ‘C:\Program Files\OpenSSH’
[/sourcecode]

  • Install sshd and ssh-agent services by using the following command line:

[sourcecode language=”powershell”]
powershell -executionpolicy bypass -file install-sshd.ps1
[/sourcecode]

  • Setup SSH host keys (this will generate all the ‘host’ keys that sshd expects when its starts).

[sourcecode language=”powershell”]
.\ssh-keygen.exe -A
[/sourcecode]

winlinuxremoting_07_2016-12-02_11-23-28

  • Open Firewall (Windows PowerShell 5.x) Server: (This command works in Windows 10)

[sourcecode language=”powershell”]
New-NetFirewallRule -Protocol TCP -LocalPort 22 -Direction Inbound -Action Allow -DisplayName SSH
[/sourcecode]

winlinuxremoting_08_2016-12-02_11-23-28

  • Or for a Workstation:

[sourcecode language=”powershell”]
netsh advfirewall firewall add rule name=’SSH Port’ dir=in action=allow protocol=TCP localport=22
[/sourcecode]

winlinuxremoting_10_2016-12-02_11-23-28

  • Then, to set sshd in auto-start mode and open up firewall:

[sourcecode language=”powershell”]
Set-Service sshd -StartupType Automatic
Set-Service ssh-agent -StartupType Automatic
[/sourcecode]

winlinuxremoting_06_2016-12-02_11-23-28

  • Edit the sshd_config file at the location where you installed Win32 Open SSH and make sure to enabled the following lines:
    Port 22
    PasswordAuthentication yes
    RSAAuthentication yes
    PubkeyAuthentication yes

winlinuxremoting_02_2016-12-02_11-23-28

Also, add the following line to the Subsystem section:
Subsystem powershell C:/Program Files/PowerShell/6.0.0.13/powershell.exe -sshs -NoLogo -NoProfile

Finally, you must add the following line in the system path: “C:\Program Files\OpenSSH”

winlinuxremoting_09_2016-12-02_11-23-28

Failure to add this path line will cause the following error because it can’t execute sshd.exe:

winlinuxremoting_03_2016-12-02_11-23-28

If there’s a need to restart Windows SSHD services, just use the following PowerShell commands:

[sourcecode language=”powershell”]
Stop-Service sshd
Start-Service sshd
[/sourcecode]

Instructions for Ubuntu SSH installation:

  • Download using Ubuntu apt repository using sudo:

[sourcecode language=”powershell”]
$ sudo apt install openssh-client
$ sudo apt install openssh-server
[/sourcecode]

  • Make changed to Ubuntu sshd_config file to enabled some lines below:

[sourcecode language=”powershell”]
$ sudo gedit /etc/ssh/sshd_config
[/sourcecode]

  • Again, make sure to enabled the following lines:
    PasswordAuthentication yes
    RSAAuthentication yes
    PubkeyAuthentication yes
  • Add the following line to the Subsystem section:
    Subsystem powershell powershell -sshs -NoLogo -NoProfile

When done, in Ubuntu, just restart sshd service :

[sourcecode language=”bash”]
sudo service sshd restart
[/sourcecode]

If you want to verify Ubuntu SSH is working, just type the following command:

[sourcecode language=”bash”]
ps -A | grep sshd
[/sourcecode]

This should display a line showing sshd. If is blank, then is not on.

Connecting Linux to Windows

This is strange but, more likely, you will get an error the first time you try to connect. Just try the second try and it will work.

[sourcecode language=”powershell”]
Enter-PSSession -Hostname earth2 -Username max_t
[/sourcecode]

winlinuxremoting_11_2016-12-02_11-23-28

Connecting Windows to Linux

Again, I’ve seen the same behavior. It will fail the first time and the it will work.

[sourcecode language=”powershell”]
Enter-PSSession -Hostname orion -Username maxt
[/sourcecode]

winlinuxremoting_05_2016-12-02_11-23-28

What’s next?

After all have been tested, you can now connect from Windows to a Linux system. Then, you can execute your Linux commands from the Linux PowerShell session at your Windows Desktop. Is that amazing!!

Keeping things Simple!

Additional tips

This tips may not hurt to do:
1. winrm quickconfig
2. Run the ./InstallPsRemoting.ps1

winlinuxremoting_04_2016-12-02_11-23-28

Also, if you don’t have a domain set up, then set a workgroup environment, and adding the IP-Addresses, computer names for either Windows and Linux host file:
In Windows the *hosts file can be found under “C:\Windows\System32\Drivers\etc“, and in Linux Ubuntu can be found under “/etc/hosts“.

winlinuxremoting_12_2016-12-02_11-23-28

The editors need to be open “Run as Administrator” in order to make changes to this file. In Windows, open Powershell “Run as Administrator” the execute notepad:

[sourcecode language=”powershell”]
notepad C:\Windows\System32\Drivers\etc\hosts
[/sourcecode]

In Linux, use the sudo then the editor of choice;

[sourcecode language=”bash”]
sudo gedit /etc/hosts
[/sourcecode]

I’m hoping this information will get you all going in doing PowerShell Remoting between Windows and Linux.

Additional Resource Information

OpenSSH Instructions at:
https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH
https://github.com/PowerShell/PowerShell/blob/309bf603f9aff9009dce7e725d42b2d4f99f58c9/demos/SSHRemoting/README.md

For Ubuntu questions:
https://help.ubuntu.com/community