How to Use an SSH Config File in Ubuntu for Easier Server Connections

If you manage multiple Ubuntu servers, typing out the full SSH command every time you need to connect quickly becomes tedious. Remembering commands like ssh [email protected] -p 2222 -i ~/.ssh/prod_key for a dozen different servers is prone to error and highly inefficient.

The solution is the SSH Configuration File (~/.ssh/config). This client-side file allows you to create short, memorable aliases for your server connections. By defining the username, IP address, port, and identity file (SSH key) ahead of time, you can reduce complex connection strings to a simple command like ssh prod-db.

Step 1: Create the SSH Config File

The SSH configuration file resides in the hidden .ssh directory within your user’s home folder. It does not exist by default on most Ubuntu installations, so you may need to create it.

  1. Open your terminal.
  2. Ensure the .ssh directory exists by running:
    mkdir -p ~/.ssh
  3. Set the correct permissions for the directory. SSH is very strict about security, and the directory must not be readable by other users:
    chmod 700 ~/.ssh
  4. Create the config file using a text editor like nano:
    nano ~/.ssh/config

Step 2: Define a Basic Host Alias

Inside the text editor, you can begin defining your server profiles. The syntax is straightforward. Each profile begins with the keyword Host, followed by the alias you want to use. Subsequent indented lines define the connection parameters.

Here is an example of a basic configuration for a web server:

Host web-server
    HostName 203.0.113.50
    User admin
    Port 22

With this saved in your config file, you no longer need to type ssh [email protected]. Instead, you simply type:

ssh web-server

The SSH client automatically reads the config file, substitutes the correct IP address and username, and initiates the connection.

Step 3: Define Advanced Connection Parameters

The real power of the config file becomes apparent when dealing with non-standard setups, such as custom ports and specific SSH keys.

Imagine you have a production database server running on port 2222, and it requires a specific RSA key file (prod_key.pem) rather than your default id_rsa key. Your configuration would look like this:

Host prod-db
    HostName 198.51.100.12
    User dbadmin
    Port 2222
    IdentityFile ~/.ssh/prod_key.pem
    IdentitiesOnly yes
  • IdentityFile: Tells the SSH client exactly which private key to offer to the server.
  • IdentitiesOnly yes: Forces the SSH client to only use the specified key, preventing it from trying every key in your .ssh folder (which can lead to the server rejecting your connection due to “too many authentication failures”).

Step 4: Managing Multiple Hosts

You can add as many servers as you need to the file. Simply separate them with a blank line. For example:

Host web-server
    HostName 203.0.113.50
    User admin

Host prod-db
    HostName 198.51.100.12
    User dbadmin
    Port 2222
    IdentityFile ~/.ssh/prod_key.pem
    IdentitiesOnly yes

Host home-nas
    HostName 192.168.1.150
    User pi

Step 5: Apply Global Configurations

Sometimes you want a setting to apply to all your SSH connections. You can do this by using the wildcard * as the Host alias. This is typically placed at the very bottom of the config file.

For example, if you want to ensure your SSH connections don’t time out and drop when you leave your desk for a coffee, you can configure the client to send a “keep-alive” packet every 60 seconds to all servers:

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3

Step 6: Save and Secure the File

Once you have finished defining your hosts in nano:

  1. Press Ctrl + O to save the file, then press Enter to confirm the file name.
  2. Press Ctrl + X to exit the editor.

Finally, ensure the permissions on the config file are strict, otherwise the SSH client will refuse to use it:

chmod 600 ~/.ssh/config

By investing a few minutes in creating an SSH config file, you eliminate the need to memorise IP addresses and complex connection flags, drastically speeding up your server administration workflow.

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.