SSH configuration file
notes
ssh
How to configure a remote host in the SSH config file
TL;DR
To add a new connection to the SSH config file (~/.ssh/config
), open it and write the following:
Host [hostname_alias]
HostName [hostname]
User [username]
#PubKeyAuthentication yes # If accessing via SSH
#IdentityFile ~/.ssh/id_ed25519 # point to the private SSH key if available
#OTHER_SSH_OPTION value # General format of SSH options
Then, from terminal, executing ssh [hostname_alias]
will try to set a connection to the specified HostName
using the specified User
.
Breakdown
- Each section starting with
Host
defines a connection. The lines below this statement define properties of the specific connection. [hostname_alias]
specifies an alias for the connection locally. Different aliases may specify the sameHostName
, e.g. to handle different users on a remote server.HostName [hostname]
specifies the targethostname
. It can be an IP address (86.87.88.89
) or a text string (github.com
). In the first case no domain resolution is necessary. In the second, the internal/etc/hosts
file will be queried and if no match is found a DNS query will be performed.User [username]
indicates the username to use in that SSH connection. If not not defined, it defaults to the current username.
A real example I use to connect to my home Raspberry Pi:
Host raspberry
Hostname 192.168.X.Y
PubKeyAuthentication yes #
IdentityFile ~/.ssh/id_ed25519
Resources
- Useful summary of SSH config file format and options.
- Stackoverflow thread on the interaction between
/etc/hosts
and~/.ssh/config
.