Simplifying SSH Connections With a Config File

Last Updated on: 

One issue I have when I am connecting to my remote servers is that I manually type the server I am connection to by hand, for example

ssh -p 433

What if you could simplify the above to:

ssh username

Easy peasy!

In this guide, you will learn how to simplify ssh connections with a config file.

The way this works is you create a configuration file for servers that you connect to often, this way, ssh knows your server and can automatically pull the info when you reference it by a name.

Go ahead and open up your config file in a text editor:

sudo nano ~username/.ssh/config

The tilde (~) is referencing the username home directory.

If you don’t have the config file, the command would automatically create one as long as you have the .ssh directory.

The config file is a per-user configuration file, and since you would be adding your server ssh configuration in this file, you are highly recommended to create strict permission: read/write for the user, and not writable by others (I would show you how to do this in a moment).

Also, it may be group-writeable provided that the group in question contains only the user. Back to the config file, let’s create a read & write permission for the user, in my case the file is own by root:

-rw-r--r-- 1 root   root    137 Feb 29 20:11 config

Change the group and user to the username that should access the config file:

sudo chown username:username config

This would change both the user and group of the config file to the username you specify.

I want to make the config file only readable and writeable by the user, this way no other user can access, use the following command:

sudo chmod 600 config

You can learn more on Permissions on Files & Directories

Now, go ahead and structure the config file this way:

host: serverA
     port 45
     user userserver_a

host: serverB
     port 22
     user userserver_v

In the above example, I have 2 hosts outlined, serverA & serverB. I identified a way to reach serverA by its IP address and serverB by its fully qualified domain name, you can use whatever soothes you, it just makes much sense to use the FQDN (Fully Qualified Domain Name), this way, even if you change your IP address, you’ll still be able to connect as you aren’t relying on IP addresses.

Back on track..

Now, if I want to connect to serverA, I’ll do

ssh serverA

It is as simple as that.

I should also mention that the host (the first line) can be anything, I might decide to name it baby1 and it would still connect as long as you fill in the user.

That is it about simplifying ssh connections with config files, see you later 😉


Comment policy: Respectful and beneficial comments are welcome with full open hands. However, all comments are manually moderated and those that doesn't relate with what the passage is saying or offensive comments would be deleted. Thanks for understanding!

Leave a Reply

Your email address will not be published. Required fields are marked *