DEV Community

Anand Mukundan
Anand Mukundan

Posted on • Originally published at dotnetindia.com on

Open SSH on Windows

I had a scenario a week or two ago where I wanted to connect from a Mac to a Windows machine and run some PS scripts. (Well the discussion on why not use PowerShell Core is for a different post).

So the easiest way is to do SSH. The windows machine I needed to connect to was a Win 7 machine and so did not ship with SSH built in. So I just downloaded Open SSH that the powershell team had ported to Windows and deployed it. And then connected from the Mac, no problem, everything worked great.

Now I wanted to integrate this into a script and did not want to enter a user name/password combo and SSH has a way to support this using public/private key pairs. If you search the internet, there are hundreds of articles on how to generate a key pair and copy the public key over to the server to allow the client to connect. The problem is I could not find what to do on the Windows Server side of SSH as the directory structure it uses differs slightly from what the UNIX version.

So some debugging and log analysis later found out the Windows version uses the .ssh (i.e. C:\Users\xxx) directory to find the pub key file (file should be named authorized_keys). The only gotcha is that the user directory is the user who logs in via the SSH. I had setup a local user that the SSH script will use to log in, and I was initially trying to put the file into my user dir, when it needs to be in the "trying to log in user's" user dir. The authorized keys file is just a text file containing all public keys. So if you have more than one public key, just append to your authorized keys file.

Once the file was in the right place, you still need to run the powershell scripts the PS team provides to fix the Host and User File permissions(These are available in the directory where you installed OpenSSH). These are both very easy to run and should get you started quickly.

Original Post

Top comments (0)