Simplify your life with SSH config file and ClusterSSH
If you’re anything like me, you probably log in and out of a half dozen remote servers on a daily basis. And if you’re even more like me, you have trouble remembering all of the various usernames, remote addresses and command line options for things like specifying a non-standard connection port or forwarding local ports to the remote machine. And sometimes you have to do the same thing in a lot of servers, this post is for you.
To simplify your life, press continue reading :-)
Let’s say that you have a remote server named dev.example.com, which has not been set up with public/private keys for password-less logins. The username to the remote account is youruser, and to reduce the number of scripted login attempts, you’ve decided to change the default SSH port to 2200 from the normal default of 22. This means that a typical command would look like:
$ ssh email@example.com -p 22000
We can make things simpler and more secure by using a public/private key pair; I highly recommend using ssh-copy-id for moving your public keys around. It will save you quite a few folder/file permission headaches.
$ ssh firstname.lastname@example.org -p 22000
# Assuming your keys are properly setup…
Now this doesn’t seem all that bad. To cut down on the verbosity you could create a simple alias in your shell as well:
$ alias dev=’ssh email@example.com -p 22000′
$ # To connect:
This works surprisingly well: Every new server you need to connect to, just add an alias to your .bashrc
However, there’s a much more elegant and flexible solution to this problem.
# contents of $HOME/.ssh/config
This means that I can simply $ ssh dev, and the options will be read from the configuration file. Let’s see what else we can do with just a few simple configuration directives.
Personally, I use quite a few public/private keypairs for the various servers and services that I use, to ensure that in the event of having one of my keys compromised the dammage is as restricted as possible. For example, I have a key that I use uniquely for my github account. Let’s set it up so that that particular private key is used for all my github-related operations:
The use of IdentityFile allows to specify exactly which private key I wish to use for authentification with the given host. You can, of course, simply specify this as a command line option for “normal” connections:
$ ssh -i ~/.ssh/blah.key firstname.lastname@example.org
but the use of a config file with IdentityFile is pretty much your only option if you want to specify which identity to use for any git commands. This also opens up the very interesting concept of further segmenting your github keys on something like a per-project or per-organization basis:
Which means that if I want to clone a repository using my organization credentials, I would use the following:
$ git clone git@github-org:orgname/some_repository.git
As any security-conscious developer would do, I set up firewalls on all of my servers and make them as restrictive as possible; in many cases, this means that the only ports that I leave open are 80/443 (for webservers), and port 22 for SSH (or whatever I might have remapped it to for obfuscation purposes). On the surface, this seems to prevent me from using things like a desktop MySQL GUI client, which expect port 3306 to be open and accessible on the remote server in question. The informed reader will note, however, that a simple local port forward can save you:
$ ssh -f -N -L 9906:127.0.0.1:3306 email@example.com
# -f puts ssh in background
# -N makes it not execute a remote command
This will forward all local port 9906 traffic to port 3306 on the remote dev.example.com server, letting me point my desktop GUI to localhost (127.0.0.1:9906) and have it behave exactly as if I had exposed port 3306 on the remote server and connected directly to it.
Now I don’t know about you, but remembering that sequence of flags and options for SSH can be a complete pain. Luckily, our config file can help alleviate that:
LocalForward 9906 127.0.0.1:3306
Which means I can simply do:
$ ssh -f -N tunnel
And my local port forwarding will be enabled using all of the configuration directives I set up for the tunnel host.
Using a small app named ClusterSSH $ apt-get install cluster ssh you can do your ssh to many shells as you want and type the same thing to all of them, very cool ;-)