If we connect to the server in the console and use the ‘ssh USER @ HOSTNAME‘ command. We may be prompted for a password in this case, or not if we use the ssh key. I have described how to configure and use SSH keys so that we do not have to enter a password in another article. Today I wanted to introduce a way how you can simplify the whole thing even more by changing the .ssh/config file and using aliases.
If we use non-standard ports or keys from non-standard places in the connection, the entire command is slightly longer:
ssh -p 2202 [email protected]
ssh -i /path/key.pem [email protected]
Additionally, we need to remember all this information with each non-standard connection. Imagine we now have 100 such servers with a custom configuration. It would be a bit difficult to remember. Of course, we should save everything in some password manager, but we can also help ourselves in other ways.
By editing or adding the ~/.ssh /config file, we can enter aliases for each connection, along with the login method and custom options.
# open the file for editing, use vi, nano or whatever you like $ vim ~/.ssh/config
Host alias1 HostName 188.8.131.52 User USERNAME Host alias2 HostName example.com Port 2202 User USERNAME IdentityFile ~/.ssh/id_example1 IdentitiesOnly yes Host alias3 HostName 184.108.40.206 User USERNAME PubkeyAuthentication no
- HostName – you enter the name or IP address of the server you want to connect to
- User – username you are going to connect to
- Port – you enter if you connect on a non-standard port
- IdentityFile – if you connect using a key then here you specify a non-standard key location
- IdentitiesOnly – force only key bindings to be used
- PubkeyAuthentication – whether to try to connect using ssh keys
For standard ports or places, the parameters can be omitted. You can find more parameters that you can use at https://www.ssh.com/ssh/config/.
Now to connect instead of using the command:
ssh -p 2202 -i /.ssh/id_example1 [email protected]
Just use the command:
If you want to know how to use ssh keys, check out this article.