I regularly have to SSH into my servers, and on most of the ones I connect to the SSH server is running on a non-standard port for obvious security reasons. I used to include the port number manually in the SSH command, for example:
ssh seymour.dennistt.com -p 922
This worked… as long as I remembered to type the port number in, which was probably less than half the time on the first go. So I finally got tired of having to retype the command with the port number in it. Luckily you can create a SSH client configuration file which will remember this for you, and even give you a shortcut.
Check if you already have a config file at ~/.ssh/config. If not, create it with the following commands
cd ~ mkdir .ssh cd .ssh touch config chmod 600 config
If you already have a .ssh folder, executing line 2 will give you a warning but it’s no harm. Now you can open up the config file in your favourite editor. Mine looks like this:
IdentityFile ~/.ssh/id_rsa Host seymour Hostname seymour.dennistt.com Port 922
Line 1 is where my SSH private key is located, and what I use for password-less SSH logins. This is out of the scope of this mini blog post, so if you’re interested in learning more just search online for tutorials on how to setup SSH public-key authentication.
Line 3 is the declaration of a host. This can be a short name for the host. All the following parameters up to the next “Host” declaration will only be applied to this host. The IdentityFile parameter on Line 1 is in the global scope and applies to all hosts.
Line 4 indicates the hostname to connect to, in case the name for the host is not the domain name.
Line 5 indicates the port to connect to.
Now when I want to SSH into blackcomb I can just type:
And SSH will do the rest. No more forgetting the port number! Awesome!
There are other parameters you can put in the config file. If you have different keys to different servers, you may put a different IdentityFile line for each host. You can also setup shortcuts for SSH tunneling.