HOW TO: SSH Aliases
When I think back on 2010, I’d have to say I SSH’d into well over 100 different machines. Late last year I found out about the SSH config file, and coming across it was one of those “Why didn’t I find out earlier?” moments.
The SSH config file is located in ~/.ssh/config or can be added if it doesn’t already exist. There is a slew of things you can add to this file (which you can find from man ssh\_config) but the neatest thing I’ve found is setting server aliases.
Let’s assume you SSH frequently with this command: ssh email@example.com
By adding this to ~/.ssh/config:
Host example HostName example.com User exampleuser
You can reduce that to previous command to
Do you have a multi-server infrastructure that you log into consistently?
Host project.web1 HostName web1.project.com User webadmin Host project.web2 HostName web2.project.com User webadmin Host project.db1 HostName db1.project.com User webadmin Host project.util1 HostName util1.project.com User webadmin Host project.stage HostName stage.project.com User webadmin
Which boils down to command like
ssh project.web1 ssh project.db1 .... ssh firstname.lastname@example.org # you always have the option of overriding at the command line.
You can also specify the host by IP directly:
Host example HostName 127.0.0.1 User exampleuser
And you can use different SSH keys:
Host example2 Hostname example.com User exampleuser IdentityFile ~/.ssh/another_ssh.identity
If you’re a heavy remote user, this saves a lot of typing and lookups from READMEs and notes. It’s nice to be able to refer to my aliases rather than scattered URL and IP addresses across projects.
Another great perk: the config also affects (most) other tools that depend on SSH like SCP, Rsync, and even Transmit!