Open Source PowerShell has been on fire, getting tons of community support and really making people think about what’s to come with a single language to manage a heterogenous data center.
To highlight this point, in my recent Pluralsight Play By Play Microsoft Open Source PowerShell on Linux and Mac with Jason Helmick and Jeffrey Snover I did a demo on using PowerShell remoting where I connected from a Linux machine to three other machines and retrieved lists of top processes from each…two Linux and one Windows. I used one script to accomplish this and no passwords. A simple implementation highlighting a very big idea. After, some people have asked…how did I do this without passwords?
Open Source PowerShell Remoting uses SSH as its communication protocol, so when we connect to a remote system using PowerShell Remoting we’ll need to enter a password. Normally SSH requires passwords to log into remote systems but it also allows for what’s called passwordless authentication, which means users can log into remote systems without having to key in a password. It does this, securely, by using a key pair to authenticate the user to the server. Basically you generate a key pair, copy the public key to the remote server and there you have it…you no longer have to enter a password when you SSH into the remote system. Let’s see how this is done.
You need a couple things to set up this demo
- A user account with the same name on each computer – create a user on each machine, Linux and Windows, with the same username.
- OpenSSH configured on all hosts – easy on Linux. It’s there by default. On Windows check out this link. Once you complete the installation of OpenSSH on your Windows system, test logging into that system from a remote computer with SSH. This will use the password for a user on that Windows system (likely the one you just created in step 1). If that doesn’t work, you won’t be able to proceed.
- Open Source PowerShell installed on all hosts – check out this link here.
- Enable PowerShell Remoting over SSH – check out this link here. Once you have this configured, be certain to test PowerShell remoting, using passwords. Test Linux to Linux and also Linux to Windows.
Now once we have the ability to connect to our hosts with SSH and we’ve confirmed we can use PowerShell SSH Remoting, we can move on to configuring passwordless authentication.
First, on your Linux machine (I’m using a Mac, but there literally is no difference here) you can use your existing public key if you have one, which is stored in your home directory in .ssh/id_rsa.pub or you can generate a new one.
To generate a new SSH key pair on your Linux machine
- Type ssh-keygen
- The program will ask you for a file name, just press enter
- It will then ask you for a passphrase, press enter again and once more to confirm
Demo-MacBook-Pro:.ssh demo$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/demo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/demo/.ssh/id_rsa.
Your public key has been saved in /Users/demo/.ssh/id_rsa.pub.
The key fingerprint is:
The key’s randomart image is:
| .. .oo |
| .oo =. .+ |
| . .+*o=o= |
|. ..oB=== o |
|o.=o*.E+S . |
| +.=oO o . |
| . *.+ |
| o . |
Make sure that if you’re running SSH as a service, the account the service is running as had the ability to read this file. In my case the account NT SERVICE\SSHD needed read access.
Confirm SSH passwordless access from Linux (or Mac) to Windows
With that you should be able to connect from your Linux (or Mac) to your Windows machine from the machine where you generated your SSH key without any password. Likewise for your Linux machines.
Demo-MacBook-Pro:~ demo$ ssh firstname.lastname@example.org
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
Let that sink in for a second, I just SSH’d into a Windows machine…
…and finally connect via PowerShell remoting over SSH with passwordless authentication
OK now we’re in the home stretch…we can now create a PowerShell remoting session over SSH with passwordless authentication.
PS /Users/demo> Enter-PSSession-HostName 172.16.94.9 -UserName demo
[172.16.94.9]: PS C:\Users\demo\Documents>
And there we have it we’re able to connect to using PowerShell Remoting over SSH without a password.