Simplify Your SSH Connections with SSH Config File

Managing remote servers requires either a very good memory to remember connection options like usernames, remote addresses, ports and further details or a good way to document all the details for each server.

The SSH config file should be your helping hand to control and simplify SSH connections. If you didn't have any SSH client installed yet, please go ahead and do it. OpenSSH is our tool of choice.

Complicated Connections

Connecting to a remote server via SSH requires a username, server url or IP address and the SSH server port listening for connections. Let's look at a specific example. Assuming your server url is yourserver.url with user marcus on port 2222.

Your connection command looks like this:

ssh marcus@yourserver.url -p 2222  
# connection established and logged in

All parameters are required to pass within the connection string.

Simplify with SSH Config File

The SSH config file isn't created automatically while installing SSH on your machine. The config file needs to be placed into your .ssh folder. By default, the location is ~/.ssh. Let's create the config file using nano command line editor. Since nano is Linux specific, you can use any other editor of your choice.

nano ~/.ssh/config  

This command opens the nano editor with a blank file. Saving to disk will create the desired SSH config file.

We define the file content using the key-value system. Each key-value-pair is stated in a separate line. A key gets its value assigned by separating both by either whitespace or equal sign or a combination of equal sign with spaces. The SSH clients interpret all statements identical.

Port 2222  
Port=2222  
Port = 2222  

Each configuration in your config file is initiated by the keyword Host followed by an identifier.

Common SSH configuration options

  • HostName: the hostname or IP address of your remote server. You can skip this definition if the Host identifier already specifies the actual hostname you want to connect with.
  • User: the connection username.
  • Port: the port where your remote SSH server is listening for connections. Default value 22.

The options above describe the basic configuration for an entry in the SSH config file. There are additional SSH connection items and tweaks which can be used for more complex setups.

General Tweaks and SSH connection items

  • Compression: a useful option for (very) slow connections.
  • ServerAliveInterval: use this option to let both peers stay in contact and avoid session closes due to SSH timeouts. Configure this option to let SSH send a packet to keep the connection between client and server alive. Also, you can use this option to know if your unreliable connection is still alive.
  • StrictHostKeyChecking: this option is used to configure whether SSH automatically adds hosts to the ~/.ssh/known_hosts file. By default, you're asked to confirm the addition to the known hosts. The default value can be annoying connecting to multiple different hosts, so you may want to set this to no and add every connected remote host to known hosts automatically.

Actually, there are more options to configure SSH. You can keep those items listed above in mind and in case you run into issues with your SSH connections, change the values and check whether they improve.

Complete configuration entry example:

Host yourserver  
    HostName yourserver.url
    User marcus
    Port 2222

Of course you can define multiple entries in your config file. Just separate them by an empty line

Host yourserver  
    HostName yourserver.url
    User marcus
        Port 2222

Host anotherserver.tld  
    User marcus
    Port 2222

The second example entry omits the HostName definition because it's already set as Host identifier.

Just save the file and leave nano. Your created file will be recognized by your SSH client for future connections.

Connect Painlessly

From now on, you can use the defined Host identifier for any connection to your remote server.

ssh yourserver  
ssh anotherserver.tld  

Your SSH client parses the config file and matches the defined Host identifier values with your provided identifier. In case they match, the specific configuration gets automatically loaded from the config file.

That's all the magic! Enjoy the simplification of your SSH configuration.


Additional Ressources

  1. SSH Connection Items and Tweaks
  2. OpenSSH client

Explore the Library

Find interesting tutorials and solutions for your problems.