Easy SSH sessions with Windows Terminal

Ryan Rousseau - Sep 21 '20 - - Dev Community

I have not used Linux very much since running Ubuntu almost 15 years ago. Now I have some Raspberry Pi and recently converted my virtual machine host to Ubuntu. So I have been using SSH a lot more, and I want to make it easier to SSH into my growing litter of Linux boxes.

This guide is mostly instructions for future me when I inevitably forget them or pave over my machine again.

For the examples, I'll be using the username user and the machine reliablechair, which is on the network at reliablechair.lan.

Install OpenSSH

OpenSSH is offered as a Windows feature, but I prefer to install it with Chocolatey.

Run the following commands in an elevated PowerShell console.

choco install openssh
cd 'C:\Program Files\OpenSSH-Win64\'
Set-Service -Name ssh-agent -StartupType Automatic
Start-Service ssh-agent

Create an SSH key

The next step is to create an SSH key. I am making one for each target machine, which is probably not necessary. You can use the same key for multiple machines if desired. In that case, you can leave off the filename and use the default name provided by ssh-keygen.

You can choose to use a passphrase to secure the key or not. If you do, you will have to enter the passphrase at some point before SSHing into the machine.

These commands do not need an elevated console.

cd ~
ssh-keygen -t rsa -b 4096 -f .\.ssh\reliablechair

Add an initial entry to SSH config

This entry will let us connect to reliablechair with a password while copy the public key over. Without setting this up, you could get connection errors if SSH tries to use other SSH keys on your machine.

With this entry, using ssh reliablechair will be the same as ssh user@reliablechair.lan.

Host reliablechair
  User user
  HostName reliablechair.lan
  PreferredAuthentications password

Copy the public key to the target machine

This command will copy the public key into the target machine's authorized keys file.

cat .\.ssh\reliablechair.pub | ssh reliablechair "mkdir -p .ssh && cat >> .ssh/authorized_keys"

Update entry in SSH config

Switch the entry for reliablechair to use the SSH key. Setting AddKeysToAgent to yes will add the key to your ssh-agent session. This means that if you set a passphrase on the key, you won't have to enter it after the initial session.

Host reliablechair
  User user
  HostName reliablechair.lan
  PreferredAuthentications publickey
  IdentitiesOnly yes
  IdentityFile c:\Users\Ryan\.ssh\reliablechair
  AddKeysToAgent yes

ssh-add

You can also call ssh-add directly.

ssh-add $env:HOME\.ssh\reliablechair

SSH connection profile for Windows Terminal

Open the Windows Terminal settings. Add a new entry under profile > list. Use a new GUID for each entry.

{
  "guid": "{474e775e-4f2a-5b96-ac1e-a2962a402335}",
  "hidden": false,
  "name": "Reliable Chair",
  "commandline": "ssh reliablechair"
}

Now there is an option to open a connection to reliablechair in the new tab menu for Windows Terminal.

Wrap-up

With a little configuration, it is easy to create SSH sessions from Windows Terminal.

This post was originally published at blog.rousseau.dev. Cover photo by John Barkiple on Unsplash.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .