The ~/.ssh/config file allows us to automate the way we connect to remote servers. Let’s assume you need to access your VPS to deploy an application. The user is deploy and SSH is listening on port 2222 instead of the default port 22. Without the configuration file, you would have to type the following command:
$: ssh -p 2222 -i ~/.ssh/id_ed25519 deploy@[IP]
If you have multiple users or even multiple VPSs, this can start to get complicated, as you will have to enter the command multiple times.
Creating the configuration file
First, let’s start by creating a configuration file. If you already have one, you can skip this part.
$: touch ~/.ssh/config
Basic configuration
Let’s start with the basics. Our first configuration will be for the deploy user we mentioned at the beginning.
Host deploy
Hostname [IP]
Port 2222
User deploy
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Understanding the options
Now let’s understand what each of these options are.
Host - Here you can add any name you want, but it is recommended to have a good naming convention because this will be the name you will use to connect to the server.
Host deploy
We can also add global settings for the host.
Host *
AddKeysToAgent yes
ServerAliveInterval 60
Hostname - In this field, you can enter the server’s IP or DNS.
Hostname [IP]
Port - Here you must enter the port that the server will connect to.
Port 2222
User - In this field, enter the username that will connect.
User deploy
IdentitiesOnly e IdentityFile - Here we are saying that SSH will only use the key we are passing and will not attempt to use any other.
IdentitiesOnly yes
IdentityFile ~/.ssh/id_ed25519
Result
With this configuration, we don’t need to type the command I wrote above. Instead, we can use it this way:
$: ssh deploy