Home How to configure connection options for SSH client
Post
Cancel

How to configure connection options for SSH client

For different servers, you might be needed to set custom options to be able to have access. These moments we will consider in the current article.

Your SSH config file should be located within the .ssh folder in your user’s home directory.

1
touch ~/.ssh/config

The config file is organized by hosts. Each host definition can define connection options for the specific matching host. Wildcards are also available to allow for options that should have a broader scope.

Each of the sections starts with a header defining the hosts that should match the configuration options that will follow. The specific configuration items for that matching host are then defined below. Only items that differ from the default values need to be specified, as the host will inherit the defaults for any undefined items. A section is defined from the Host header to the following Host header.

The general format will look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
Host firsthost
SSH_OPTION_1 custom_value
SSH_OPTION_2 custom_value
SSH_OPTION_3 custom_value

Host secondhost
ANOTHER_OPTION custom_value

Host *host
ANOTHER_OPTION custom_value

Host *
CHANGE_DEFAULT custom_value

SSH will match the hostname given on the command line with each of the Host headers that define configuration sections. It will do this from the top of the file downwards, so an order is incredibly important.

We should point out that the patterns in the Host definition do not have to match the actual host that you will be connecting with. You can essentially use these definitions to set up aliases for hosts that can be used in lieu of the actual hostname.

For instance, consider this definition:

1
2
3
Host testing
HostName test.example.com
User john

This host allows us to connect as john@test.example.com by typing this on the command line:

1
ssh testing

If you want to have a look at full list of options check [wpsm_hidelink text=”this site” link=”https://www.ssh.com/ssh/config/”].

We want to consider basic options to access our server:

1
2
3
4
5
# reheda.pro
Host reheda.pro
HostName <public_ip>
User test
IdentityFile ~/.ssh/id_rsa

For example, to log in to our server we specified user, id_rsa private key, and hostname. And now we can use the following command to connect our server:

1
ssh reheda.pro

The equivalent of the options above without config file is:

1
ssh test@<public_ip> -i ~/.ssh/id_rsa

As we can see, when you specify some options in the SSH config file it simplifies the process of the connections to your server.