Ubuntu Server 18.04 – Simplifying SSH connections with a config file

How to Setup a Node.js Application Using PM2

Before we leave the topic of SSH, there’s another trick that benefits convenience, and that is the creation of a local configuration file for SSH. This file must be stored in the .ssh directory of your home directory, and be named config. The full path would look something like this:


This file doesn’t exist by default, but if it’s found, SSH will parse it and you’ll be able to benefit from it. Go ahead and open this file in your text editor, such as nano:

nano /home/your_username/.ssh/config

This config file allows you to type configuration for servers that you connect to often, which can simplify the SSH command automatically. The following are example contents from such a file that will help me illustrate what it does:

host myserver 
    Port 22 
    User jdoe 
Host nagios 
    Hostname nagios.local.lan 
    Port 2222 
    User nagiosuser 

In the example contents, I have two hosts outlined, myserver and nagios. For each, I’ve identified a way to reach it by name or IP address (the Hostname line), as well as the Port and User account to use for the connection. If I use ssh to connect to either Host by the name I outlined in this file, it will use the values I have stored there, for example:

ssh nagios 

That command is a lot shorter than if I set all the options manually. Considering I have a config file for SSH, that command is essentially the same as if I identified the connection details manually, which would’ve been the following:

ssh -p 2222 nagiosuser@nagios.local.lan 

I’m sure you can see how much simpler it is to type the first command than the second. With the config file for SSH, I can have some of those details automatically applied. Since I’ve outlined that my nagios server is located at nagios.local.lan, its SSH user is nagiosuser, and it’s listening on port 2222, it will automatically use those values even though I only typed ssh nagios. Furthermore, you can also override this entry as well. If you provide a different username when you use the ssh command, it will use that instead of what you have written in the config file.

In the first example (for server myserver), I’m providing an IP address for the connection, rather than a hostname. This is useful in a situation where you may not have a DNS entry for your target server. With this example, I don’t have to remember that the IP address for myserver is I simply execute ssh myserver and it’s taken care of for me.

The names of each server within the config file are arbitrary, and don’t have to match the target server’s hostname. I could’ve named the first server potato and it would still have routed me to, so I can create any sort of named shortcut I want, whatever I find is most convenient for me and easiest to remember. As you can see, maintaining a config file in your home directory containing your most commonly used SSH connections will certainly help keep you organized and allow you to connect more easily.

Comments are closed.