SSH Client Config File (~/.ssh/config) Tips

Created On: 2017-01-05 Updated On: 2017-01-09

SSH client is one of the common tools used on linux system. Its config file have some interesting use cases that may not be obvious to new users.

Define Host Alias

Host can be used to define host alias. You can define one or more alias for one hostname.

For example,

Host myserver
HostName 1.2.3.4

Host ipad
HostName 192.168.1.102

Now you can connect to the host using host alias on the command line:

ssh myserver
ssh ipad

Many tools that use ssh can accept host alias, such as git, rsync, fabric1.

rsync -air -n ~/myfiles myserver:/tmp/

Connect to Private IP Using a Gateway Server

If you have a ssh server running on a private network, usually you must first login to a gateway server that has both a public network and a private network. Using ssh config file, this process can be simplified.

Suppose you have a gateway server at 1.2.3.4 and 10.1.2.2, a private server at 10.1.2.3. You can define an entry for the private host like this:

Host pri
HostName 10.1.2.3
ProxyCommand ssh sshgw nc %h %p

Host sshgw
HostName 1.2.3.4

Now you can connect to private node directly on the command line:

ssh pri

Network traffic is still routed via the gateway server, but it's much easier for the user.

Config Default Ports For All Hosts

In some environment, ssh server is configured to not listen on default port 22. In such case, you can config a default port for all hosts:

Host *
Port 9632

Now you don't need to explicitly use -p parameter on the command line.

If you manage more than 1 environment, you may learn more about host pattern matching. You can use '*' and '?', and match on hostname or ip address. You can read more about that in man ssh_config.

Footnotes:

1

Requires env.use_ssh_config = True, see http://docs.fabfile.org/en/1.13/usage/env.html#use-ssh-config

Is this post helpful?