Effortless SSH Connections: A Primer on ~/.ssh/config

Effortless SSH Connections: A Primer on ~/.ssh/config

There is a lot you can do with SSH's config file. However, this will be a brief introduction for you to get your feet wet.

If you are connecting to multiple servers it may be helpful to be able to name those servers, instead of connecting to them specifying the username and IP address each time.

For that you can use ~/.ssh/config. As in everything in Linux, a configuration file that helps make administering your machine easier.

If the directory doesn't exist you can create it.

mkdir -p ~/.ssh && chmod 700 ~/.ssh

The first part of the command creates the directory while the second gives it the appropriate permissions for your user.

We still need to create the config file and give it permissions as well.

touch ~/.ssh/config && chmod 600 ~/.ssh/config

The SSH config file has the following structure

Host Hostname
    SSH_OPTION value
    SSH_OPTION value

Host Hostname_two
    SSH_OPTION value

Each paragraph starts with Host instruction and contains specific SSH options that are used when connecting with the remote SSH server.

Normally when you connect to a remote server using SSH you would specify a remote user name, host name and port.

ssh user@<ip address> -p 2344

For example:

ssh john@webdev.com -p 2322

To connect to the server using the same options, you configure ~/.ssh/config and name it, in this case, webdev. But you can name it whatever you want as long as it makes sense to you.

For example. My server that uses ghost as it's blogging engine is called ghost. Whenever I want to log into the server all I do is type ssh ghost.

To be able to do that we write the instructions in the config file.

Host webdev
    Hostname webdev.com
    User john
    Port 2322

Now when you type ssh webdev the ssh client will read the configuration file and use the information contained within to connect to the server.

That was a brief introduction on how to simplify managing your different machines.

man ssh_config will show a list of available ssh options. The SSH config file is read by other programs as well. Such as, scp, sftp, rsync.