Introduction

In an average day for me, I connect between 5 and 20 times to hosts using SSH and which hosts I’m connecting to changes each day. Like most people on Windows, I use Putty which has the handy feature of “Saved Sessions”. This is used to save the various settings for each host, which generally includes the username, IP address, and port number. It can also be used to save less common settings like SSH tunnels, which I something I use frequently.

Putty Saved Session Example

When I made the switch to Linux for my work desktop I missed the “Saved Sessions” functionality of Putty. Fortunately, there is a similar feature provided by ssh .

SSH in Bash

Connecting to a Host

The way most people use the ssh command is to specify the required options for the host at the command prompt. For example, if I wanted to connect to a host at 10.3.1.7 as the tecno user and create an SSH tunnel to it on port 8080 to port 5000 on my localhost it would look like this:

$ ssh tecno@10.3.1.7 -L 5000:locahost:8080

Here I am specifying the user to connect as tecno , the IP address of the host 10.3.1.7 , and the SSH tunnel to create 5000:localhost:8080 . If this is a connection that I am making frequently it would be annoying to explicitly add these parameters each time.

This is where the ssh client config file comes into play. A profile can be set up for the host to include certain options when connecting to it. Using either the ~/.ssh/config file to set the configuration on a per-user basis or the /etc/ssh/ssh_config file to set it at a system-wide level. In my example, I am going to set it only for my user. So in my ~/.ssh/config file I would add the following lines:

Host utility HostName 10.3.1.7 User tecno LocalForward 5000 localhost:8080

Note: if the file doesn’t already exist you can create it with the following command:

$ touch ~/.ssh/config && chmod 600 ~/.ssh/config

This profile allows me to use the following command to connect to my utility container without typing out the username, IP address, or tunnel, just the hostname set for the profile.

$ ssh utility

Any ssh options you can specify at the command line can also be included in the profile for a host.

My favourite part of this approach is that it provides tab compilation in Bash for the hostname, which speeds up the process of connecting to host even more.

Groups of Hosts

Another functionality for the host profiles, which Putty doesn’t include, is adding host options based on wildcards. In my work environment hosts with similar functions are grouped by IP address ranges. Each of the different groups may have different SSH options required to connect. Let’s take our remote VPN clients for an example, I know that when I ssh to one of these hosts they will be in the 10.0.7.0/24 range and I will have to be the nicholas user. So I can create a profile as follows to always connect as nicholas instead of the user I’m connecting from (usually tecno ):

Host 10.0.7.* User nicholas

Now when I run ssh 10.0.7.16 it will assume the nicholas user.

Although this is a simple example, it also applies to more complex situations. Let’s say you have a group of remote MySQL servers that you can only connect to over SSH. These servers all use a non-standard ssh port of 2222 . You need to use your non-default ssh private key located at ~/.ssh/database_key to authenticate as the admin user. Plus to connect to the database an SSH tunnel is required. Fortunately, all of the hosts are in the 217.88.200.0/24 public IP range so the following profile can be used:

Host 217.88.200.* User admin IdentityFile ~/.ssh/database_key Port 2222 LocalForward 3306 localhost:3306

With this profile, you only have to remember the IP address of the host you are connecting to, not all of the additional ssh options.

Conclusion

I utilize this approach for almost every host I connect to for work and after using it for a couple of months I much prefer this over Putty’s “Saved Sessions”. I can now connect to a host with all of the options pre-set in the config file and using tab completion in Bash to get the hostname. This speeds of the process every time I SSH to a host, especially one that I haven’t connected to in a while.

To get more information about using the ssh client config file I would recommend checking out ssh.com’s article on it which provides a great overview of its functionality and each of the options available.