If you’re a web designer, computer programmer, or sysadmin, you’re probably logging in and out of remote clients all day long. This can quickly become a very cumbersome task if you aren’t familiar with the inner workings of shell. Remembering host machine addresses, log ins, and passwords can easily become a chore, and before you know it, you can find yourself digging through a file crammed full of passwords just to find the one you need.
Using SSH config hosts
Using the SSH config file can greatly improve your workflow by helping you track all of these variables in one place. If you’ve never used the SSH config file before, you’re in for a treat. To start off, lets imagine the following scenario: You have passwordless SSH key login set up, and you want to simplify the login process. Rather than typing this each time:
$ ssh email@example.com
you can set up your SSH config file to shorten the string. Open up the SSH config file on your local machine by typing:
$ vi ~/.ssh/config
Once you’ve opened your config file, you can proceed to edit it. Using the standard Vi commands, you can modify this file to add a new “host”. You can add multiple hosts to your config file, and each one can have its own unique settings. For arguments sake, let’s imagine the following fake ssh login:
$ ssh tiggerTheTiger@onehundredacrewoods.com
In order to shorten this login down, we can add a new host to the SSH config files as follows:
Host TtT HostName onehundredacrewoods.com User tiggerTheTiger
You can replace TtT with whatever you’d like to use to log in. Now that this file has ben set up, save and quit. You can now log into your onehundredacrewoods.com server by simply typing:
$ ssh TtT
Admittedly, this is quite a bit simpler than typing your full username and host every time you SSH. These changes also affect SCP, which makes file transfer to and from the server much easier.
Setting up a keep alive interval
It’s happened to all of us: you’re half way done with a project you’ve been working on through SSH, and you decide to get up and make a sandwich. You come back to your machine 5 minutes later to find out that your connection has timed out, and your shell is locked down. Now, you have to close your shell, log back in, find your file, and deal with the .swp file sitting around. This can quickly become not only a damper to your productivity, but also a major pain in the ass.
Luckily, using the SSH config file, you can set up what’s known as a keep alive interval. An SSH keep alive interval will send off a signal to the web server on a specified interval, a certain number of times in order to keep your connection active. For example, let’s say that we wanted to keep our connection to the server alive for 2 hours. We could add the following to our SSH config file.
Host * ServerAliveInterval 120 ServerAliveCountMax 60
Let’s break down the preceding code. The first line signifies that the options following it are valid for all SSH hosts. This means that any connection you make through SSH will stay open for 2 hours. The second line specifies the interval in seconds at which a signal should be sent to the web server. In our example, a signal is sent every 2 minutes. The final line specifies how many times a signal should be sent to the server. In our example, we send the signal 60 times. This means that every 2 minutes, a signal will be sent to the server up until 60 signals have been sent (and alternatively, that our web server will stay online for 2 hours).
The SSH config file is an extraordinarily useful tool, and if you haven’t already, you should definitely incorporate it into your workflow. There are countless other tasks that can be accomplished through the use of a config file, and you can find a great guide here. No matter what your occupation, if you deal with remote servers, the SSH config file will definitely be a time saver.