Introduction
This page is dedicated to advanced SSH usage examples. We will discuss the following concepts:
- config files (for client)
- tunneling
- jumping
Config files
Config files allow you to specify certain rules for all or chosen hosts. The file has a really simple structure.
It is divided into sections which begin with the Host keyword. Sections are read one by one and the first
matching section takes precedence over the remaining sections - you write more specific sections at the top and the more general
sections below.
Why even bother?
You might say that SSH client doesn't need any special configuration - you just type the user@host and that's it. Well, what happens when you manage multiple servers? Maybe you want to use a different pair of keys for each servers? Maybe the server uses a port other than the default 22 to avoid automated bots trying to log in?
That's where config files come in handy!
Example scenario
Let's assume that you manage 3 servers, with the following access info:
- very.long.hostname.example1.com
- user: admin
- port: 22
- key name: id_rsa
- example2.com
- user: billthemaster
- port: 2222
- key name: example2_ecdsa
- 192.168.133.7
- user: management
- port: 22
- key name: id_rsa
You got tired having to always specify the identity file location with the -i option and the port with -p option for example2.com.
Don't even mention admin@very.long.hostname.example1.com!
In the given example, the config file could look like this:
Host server1
HostName very.long.hostname.example1.com
User admin
IdentityFile ~/.ssh/id_rsa
Host server2
HostName example2.com
Port 2222
User billthemaster
IdentityFile ~/.ssh/example2_ecdsa
Host server3
HostName 192.168.133.7
User management
IdentityFile ~/.ssh/id_rsa
Host *
IdentityFile /path/to/some/other/key
You can see here usage of Host *. Options specified in this section will affect all other hosts.
But where do I put this file?
SSH looks for the options in the following order:
- command line arguments
~/.ssh/config/etc/ssh/ssh_config
You can also specify a custom path with the -F argument, for example:
ssh -F ~/Documents/projects/someproject/config/ssh production
...or discard any config file:
ssh -F /dev/null username@hostname
There's more to ssh config files, but I direct you to man ssh_config for more information