Secure Shell (SSH) is one of the most commonly used and secured ways to connect to a remote server. It is easy to connect with a remote server with SSH but what if you want to connect with different servers and you do it frequently. It becomes difficult to remember all the IPs addresses, usernames, commands, and passwords.

So to solve this problem, SSH allows you to set and use the ssh config file for different use-cases. SSH uses a system and custom or user-specific configuration file. You can customize and configure client-side connection options and can store information of every remote server you connect with. In today’s article, we will explain how to use the ssh config file and explain some of the common configuration options.

With an SSH config file, you can define and store settings for specific remote machines. And this eliminates the need to keep a track of every information like password, username, IP address, etc required to connect with a remote server.

What is the location of the SSH Config file

So as we told you previously, two types of configuration files are available in SSH – System-wide and User-specific(Custom) configuration files. And their locations are:

  1. System-wide configuration file: This file mainly contains default information that applies to all the system users and you can find it at /etc/ssh/ssh_config
  2. User Specific or Custom configuration file: This configuration file is specific to the user and in this article; this file will be our major focus.

Every user can maintain a custom or client-side SSH configuration file and it can store some common information that is used while making a connection. The file remains in read and write format for the user and others cannot access that.

The Client or custom configuration file is stored in the user’s home directory in the ~/.ssh directory. You will find it here – ~/.ssh/config or $HOME/.ssh/config

By default, the user-specific configuration file does not exist. So you can create it with this command:

touch ~/.ssh/config 

Also, if you do not find the ~/.ssh directory, you can create it with this command:

mkdir -p ~/.ssh 
chmod 0700 ~/.ssh  

Understand the structure of the SSH Config file

The Configuration file is specific to every host and contains information related to connection with a server. Every section starts with a header definition for the host and it is followed by the information and values that should be matched for connections. The file format will look like this:

Host Host1
    ssh_option1 Value Custom Value
    ssh_option2 Value Custom Value
    ssh_option3 Value  Custom Value

Host Host2
    ssh_value Value Custom Value

Host *
    Ssh_option Value Custom Value

How does the SSH configuration file work

Normally if you want to connect to a remote server, you will use this command:

ssh -i ~/.ssh/id_rsa -p 22 [email protected] 

It is not an easy task to remember the hostname and IP address of all the servers you connect with. So with the configuration file, you can connect with a single command.

ssh hostname 

The SSH will use the information from the configuration file to connect to the remote server. So let’s understand the working of the config file. For example, if you run the following command:

ssh Host1 

SSH will first match the hostname with each host mentioned in the config file with a top-to-down approach and will find the Host1 header file. Once SSH finds a match for Host1, it will check for other hosts too if there is any other similar match. If there is no other match, SSH will interpret the option values mentioned with Host1 to connect with the server.

Using the SSH Config File

Now we know that what is SSH Config file is and how does it work. Now let’s understand how you can use it to simplify the remote connection environment.

Open the configuration file in an Editor.

vi ~/.ssh/config 

And you can define username, IP address, and port values in it. For example:

Host TecAdmin
         HostName 192.167.54.19
         Port 2222
         Forwardx11 no

Host *
         User tecadmin1
         IdentityFile ~/.ssh/id_rsa
         Protocol 2
         Compression yes
         ServerAliveInterval 60
         ServerAliveCountMax 20
         LogLevel Info

Explanation of values used in the file:

  • HostName: IP address of the remote server host in which you want to login.
  • User: Name of the user, you will log in as.
  • Port: The port you want to connect on the remote server.
  • Protocol: The version of protocol SSH should prefer. You can enter multiple values separated by a comma.
  • IdentityFile: Location of file that contains RSA, ECDSA, and DSA authentication Identity of the user.
  • ForwardX11: It allows you to forward the remote server display on your machine.
  • Compression: You want to use compression during the remote server connection or not. Turning it on can be useful for you if you have a slow connection
  • ServerAliveinterval: It is used to set a timer in seconds for the server connection and in the given time if no response is received from the server, ssh will send a message to request a response.
  • ServerAliveCountMax: It sets the number of messages that should be sent to request a response from the server.
  • LogLevel: It tells the verbosity level used when logging.

Conclusion

So now you know, how a single configuration file can be so useful for you if you connect with remote servers frequently. You do not need to remember multiple hostnames, ip addresses, and ports.