How to Create SSH Host Aliases in Ubuntu Linux

Managing multiple remote servers via SSH can become tedious when memorizing different IP addresses, ports, and usernames. This guide provides a straightforward explanation of how to define SSH Host aliases in the SSH client configuration file on Ubuntu Linux, allowing you to connect to your servers using short, memorable nicknames instead of long command strings.

The SSH Config File Location

In Ubuntu Linux, SSH client configurations are managed in a user-specific file located at ~/.ssh/config. If this file does not exist, you can create it.

To open or create the file using the nano text editor, run the following command in your terminal:

nano ~/.ssh/config

SSH Alias Syntax

To define a host alias, you use the Host directive followed by the configuration parameters for that connection. The basic syntax is as follows:

Host my-alias-name
    HostName server-ip-or-domain
    User username
    Port port-number
    IdentityFile /path/to/private/key

Explanation of Parameters

Example Configuration

Below is an example of an SSH config file containing two different host aliases:

Host webserver
    HostName 192.168.1.50
    User ubuntu
    Port 2222

Host dbserver
    HostName db.example.com
    User admin
    IdentityFile ~/.ssh/id_rsa_db

How to Use the Alias

Once you have saved and closed the file, you can connect to your remote servers by simply typing ssh followed by the alias you defined:

ssh webserver

This command automatically applies the configured username, port, IP address, and SSH key defined in the configuration file.

Setting Correct File Permissions

For security reasons, the SSH client requires the configuration file to have restricted permissions. If the permissions are too open, SSH will ignore the file. Run the following command to set the correct permissions:

chmod 600 ~/.ssh/config