How to Define an SSH Connection Using .ssh/config File in Windows

Randula Koralage
3 min readMay 22, 2022
Photo by Markus Spiske on Unsplash

Let’s start with some background knowledge, If you are strict to the topic, don’t hesitate to scroll down :)

With Windows 10’s April 2018 Update, a built-in SSH client is enabled by default in Windows 10.

You can simply check whether your ssh is enabled by typing ssh in your windows command line.

ssh

S-S-H

Secure Shell is shortened as SSH, and it is a protocol that provides an opportunity for users to access another computer securely through a network.

The basic SSH command looked like follows:

ssh username@server_ip_address

This will prompt you a question to enter the password and once you provide the password you are in. The above mechanism is used for password logins for remote servers. Anyone with your password can access to your server. The following domain was found in a tutorial and you can understand how password-based SSH works.

password based ssh

Identity key in SSH

Identity keys are private keys that an SSHclient uses to authenticate itself when logging into an SSH server. Basically, identity keys are used for authenticating users.

For example, When you are creating an EC2 in AWS, You’ll receive a PEM file containing a private key and you must use this key to be able to SSH into new EC2 instances.

The following command is used to connect using identity key file:

ssh -i path_to_keyfile.pem username@server_ip_address

Using A Config File To Simplify Identity Key Based SSH

It is annoying to keep all the IPs, users, and .pem file locations in memory. You can make the SSH process very easy with a config file. This can be done in both Windows and Linux operating systems. Please follow the following simple steps.

Step 1:

Navigate to .ssh folder of your machine.

In windows it is in : cd C:\Users\YOURNAME\.ssh
In Linux it is in Home : cd ~/.ssh

Step 2:

Create a file called config if not exists. Also need to provide it globally readable access.

Linux:chmod 600 ~/.ssh/config

windows. Tip: use git bash to execute linux commands in your windows machine!

Step 3:

Define the host information using the following format and save the file.

Ex:

  • You can replace NAME_1, and NAME_2 with any convenient string that you can name the host.
  • USER_NAME is the user that you are trying to connect..
  • HOST_NAME_OR_IP is the hostname/ip of the remote server that you are trying to connect
  • PATH_TO_IDENTITY_FILE is the path of .pem file that provides authentication to users to the particular remote server.

In addition to that, you can use the following property to keep your terminal without disconnecting while it is idle.

Host *
ServerAliveInterval 60

BONUS TIP: Keeping SSH Session Alive When Your Terminal is Idle

The ssh daemon (sshd), which runs server-side, closes the connection from the server-side if the client goes idle. To prevent connection loss, we can instruct the ssh client to send a sign-of-life signal to the server once in a while.

In the above example, client-side ssh client sends a live signal once every 60s to a remote server to inform them that he is alive. This will keep your session existing for a long even when you are involving some other task.

Step 4:

Now restart your command prompt and try to connect using the name you used to indicate the Host

ssh NAME_1

ssh dev1

This will save your time and effort :)

--

--