[Updated] How to SSH into a remote server using private key and Windows Terminal | One-click connect.
Wiindows terminal is proving to be an awesome tool for terminal(CLI) lovers. With its cusomization capabilities the possibilities are endless. Here in this article I'm going to explore connecting to a remote Linux server using SSH and private key using Windows Terminal.
Prerequisite to this tutorial is that you need to have Windows Terminal installed on your Windows computer. (That can be easily done from the Windows Store)
What is the default option?
Till now if you wanted to ssh into a remote computer you'd have to:
- Open Windows Powershell or Command Prompt.
- Type in
ssh user@REMOTE-IP-ADDRESS-OR-FQDN
- Enter user password
Everytime you want to start a new ssh session. Not anymore.
Step 1: Create a public/private rsa key pair.
Open Command Prompt/Powershell or as I like it, Powershell in Windows Terminal. Type in ssh-keygen -t rsa
. It'll ask, in which file (or where) to save the key. I'd suggest you go with the default option i.e. C:\Users\<username>/.ssh/id_rsa
Press Enter
without typing in anything.
Now it'll ask you to enter a passphrase. This is an optional step, if multiple users access your computer then it is a nice thing to do but, if you are the only one accessing then there's no need. Just hit the Enter key "twice".
Step 2: Copy the public key into the remote system.
Usually if you're on a linux pc it is as simple as this command ssh-copy-id <user>@REMOTE-IP-ADDRESS-OR-FQDN
. But ssh-copy-id is not available on Windows, hence, the following command.
cat ~/.ssh/id_rsa.pub | ssh user@REMOTE-IP-ADDRESS-OR-FQDN "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys"
In my case it looks something like this
cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys"
After executing the above command it'll ask for the user password for the remote computer. Enter your password and you're done copying your public key into the remote computer.
Step 3: Setup Windows Terminal to connect in one click.
Open Windows Terminal (if not already open). Then click on the down arrow next to the Pwershell tab and then Settings
.
This will open the Settings
pane, and on the bottom left you'll see an option to Open JSON
, click on it to open the settings.json
file. You can use any editor to open the file. My poison is VSCode.
Scroll down to "profiles"
and copy and paste (don't forget to change to your user and ip address) the following inside the "list"
array, as shown in the screenshot below.
{
"guid": "{0caa0dad-35be-5f56-a8ff-afceaeaa6103}",
"name": "Home Server",
"commandline": "ssh -i \"~/.ssh/id_rsa\" user@REMOTE-IP-ADDRESS-OR-FQDN",
"hidden": false
},
Save the settings.json
file and click the same down arrow on the Windows Terminal and you'll see your entry in the drop down list.
Select it and Boom! you're connected to the remote system in just One Click!
Until next time Ciao
.