Make (efficient) use of ~/.ssh/config

Last year, a blog entry by Joël “from the Internet” caught my attention on HN. He wrote about the benefits of using an SSH configuration file to avoid typing full host names, user login names and deviating SSH ports and reduce the work to make a port forwarded connection. Because it simplified my life as advertised, I strongly urge you to read the blog post right now!

Things get even more efficient, if you have a large array of machines with host names that follow a certain pattern. In my case, I have access to a cluster of GPU and CPU nodes, all bearing the same prefix and a per-machine number. Instead of repeating myself over and over again, I can specify a wild carded pattern that matches zero or more (*) characters or exactly one (?):

Host foo-*-?.bar.baz.co.uk
    User me
    IdentityFile ~/.ssh/foo_key

Still, it is tiresome to type the fully qualified host name. But it can be shortened (provided that OpenSSH>=5.6 is installed) by re-using the expanded nickname with the %h specifier:

Host foo-*-?
    User me
    HostName %h.bar.baz.co.uk
    IdentityFile ~/.ssh/foo_key

Now, I can simply log into a machine with ssh foo-gpu-1 instead of ssh -i ~/.ssh/foo_key me@foo-gpu-1.bar.baz.co.uk. The manpage on ssh_config is a nice and short read, so don’t miss out the other available options.