How to Use SSH Config
Connecting via SSH Config file
By default, the ssh config file is located inside ~/.ssh
directory.
If the file is not present, you can create one using:
Now the format for writing a remote host configuration inside a config file is as follows:
The space provided from the second line is not compulsory but helps in making the file more readable.
For our use case the configuration to connect to our AWS EC2 instance would be as follows:
After saving the following configuration we can now ssh directly with the host name provided above.
Running the above command lets us connect to the EC2 instance directly.
SSH config file syntax
A single ssh config file can have multiple ssh configurations. For example:
The entire list of parameters can be found here
Along with having multiple configurations we can also use a lot of wildcards while creating out configuration files
( * ) Can be used as a substitute for one or more characters. For example, in case there is a common
IdentityFile
for all dev servers, we can add the following line in config file:
( ? ) Can be used as a substitute for a single character. For example, in case we want to write configuration for all servers, with same prefix we can write:
We can connect to this server via command like ssh nano-server
tall-server
omni-server
but not via dev-server
as dev
only contains 3 characters.
( ! ) Can be used to negate the matches to the expression that is written after it
The above configuration file would mean that until the host is prod-server
set value of user field to low-priority-user
Based on these wildcards, we can write a sample configuration file as follows:
In the above file we have defined separate configurations for prod-server
and stag-server
with their separate IdentityFile. While for dev-server
and any other possible server, there is a default pem
file.
Also for all servers except prod-server
the LogLevel is set to DEBUG
:
Last updated