· patched
SSH Config
Give a host a short name and stop retyping user, port and key file: how ~/.ssh/config works, and what it can do that the command line cannot.
- Networking
- Operating Systems
- Unix
As well as having to type passphrases, you have to remember the right user, key file, port and other settings for each host you want to SSH into – which quickly becomes overwhelming.
That’s why, in addition to the command line options we have seen so far, SSH can also read the same options from configuration files.
The configuration data is applied from the following sources, in this order:
- command-line options
- user’s configuration file (located in
~/.ssh/config) - system-wide configuration file (located in
/etc/ssh/ssh_config)
How to write ~/.ssh/config and /etc/ssh/ssh_config files
The configuration files are split into sections, each representing a Host specification. Each section allows us to define a server we’ll frequently want to connect to and give it a pet name, so that we can specify some default parameters for it.
Let’s take for example this command to SSH into server.123.mydomain.com, with lots of hard-to-remember bits:
$ ssh -i ~/ssh/my_key_with_a_long_name_id_rsa -p 1337 myusername@server.123.mydomain.com
Instead of typing all of that out each time we want to connect, we can put a Host specification in ~/.ssh/config like this:
Host myserver
HostName server.123.mydomain.com
User myusername
Port 1337
IdentityFile ~/ssh/my_key_with_a_long_name_id_rsa
And in the future, simply connect with:
$ ssh myserver
Tip:
You can also use wildcards in the value for Host, meaning you can use:
Host *
Key Value
...
to specify parameters for all hosts, or, for example:
Host i-*
Key Value
...
to specify parameters for all hosts whose names look like Amazon EC2 instance IDs.
This is just a basic example, but an important thing to note is that you can specify just about any parameter in SSH configuration files. In fact, there are even a few advanced configuration parameters which are unavailable as command line arguments.
To still allow them to be used on the command line, SSH offers a special flag:
$ ssh -oParameterKey="ParameterValue" myuser@myserver
So it is definitely worth taking a look at the complete list in case they are ever needed.
See the man page for more info.