SSH The Easy Way

Using the ssh config file

Frequently I will be assisting other people with some server administration work. Helping configure a web server, database or some other necessary tool of our trade. I am constantly surprise at how many people are not aware that their command line ssh can use a config file to make your like much easier.

Instead of typing in something like:

ssh -i ~/.ssh/id_rsa -p 2222

with the usage of an ssh config file you could simply connect using:

ssh web4

The “name” can be anything you like.

All you need to do is create a file called config in your home’s .ssh directory ( ~/.ssh/config) and add the following lines to it.

Host web4 User mikep Port 2222 Hostname IdentityFile ~/.ssh/id_rsa

That's all it takes!

But you can also add a few other little tricks to your config file. Does the server have a timeout on it that keeps kicking you out whenever your are idle too long? If so try adding the following line at the top of your config file:

Host * ServerAliveInterval 30 ServerAliveCountMax 2

Is there a service that is only available to the localhost that you require access to? Well you can also setup port forwarding to tunnel through ssh to use that service.

Let’s say we need to access a MySQL DB on the remote host that is behind a firewalll we do not control or only listens on it’s localhost interface. We can use the following configuration to access it.

Host web4 User mikep Hostname IdentityFile ~/.ssh/id_rsa LocalForward 3306

Once connected, you can connect to your mysql server easily either by using a GUI client and connecting to localhost on port 3306 or from the command line simply by executing the mysql client.

mysql -—u mikep -p

Hopefully this helps you be a little more productive! :)