Skip to Content

Manage your SSH hosts in your SSH config file

Posted on

As a system administrator or web developer, you are using SSH daily to log in to your servers. If you only manage a handful of machines, you might be keeping track of their connection details in a text file or wiki entry.

But as that list of machines steadily grows, you soon find yourself wasting time looking up connection details: which SSH key did we use for the client’s server? What was the username on our database node? Which port is open for SSH?

You can save yourself a lot of time by making use of SSH’s config file. It allows you to create aliases for every host you need along with their required connection details.

Let’s say we want to connect to the host on port 2200 using the private key ~/ssh/production.

The SSH command would look like this:

ssh [email protected] -p 2200 -i ~/.ssh/production

That’s quite a lot to remember for tens or even hundreds of hosts!

We can simplify this command by adding a new host entry to the ~/.ssh/config file. Each entry starts with the keyword Host followed by an identifier. Inside the block you then add the required directives to connect.

Our command above would translate to the following entry:

Host foo-production
   user deploy
   port 2200
   identityfile ~/.ssh/production

We now defined a new Host under the foo-production alias. This means we can invoke ssh with only the alias as argument:

ssh foo-production

Much better! You can now start adding more hosts by copying that block and changing the connection details.

By the way, you can also use the same alias in the scp command to transfer files:

scp foo-production:/var/logs/mysql.log /tmp/mysql.log

If you want to create host entries system-wide, you can edit /etc/ssh/config. If you need to tweak the connection details a bit more, you can find a list of all available options in the ssh_config documentation.

comments powered by Disqus