Advanced OpenSSH - using the configuration file

Posted by Markus Benning on November 16, 2019

The openssh client has a configuration file which is handy for configuring things you would otherwise repeat over and over on the command line.

The user configuration file is located at:

~/.ssh/config

An example for the configuration format:

# comment
Host host1.example.tld
  User bob1967

Host *
  User bob

The configuration will be searched for each option from top to bottom and the first match wins. This means default options should always be added at the end and more specific options first.

You can find the full documentation of the configuration file in the ssh_config(5) man page.

Using host aliases

The configuration file can be used to define aliases:

Host staging
  Hostname bla-staging.zone-a.example.tld

Host lab1
  Hostname 10.0.18.67

Now you can just ssh staging or ssh lab1.

Disable password authentication

When using ssh keys to login password-less you may want to disable password authentication by default:

Host *
  PasswordAuthentication no

This will stop ssh from prompting for password in case of authentication failures.

It can still be switched on for individual hosts or by adding -o PasswordAuthentication=yes on the command line.

Enable server alive messages

If you have problems with flappy connections or NAT gateways dropping your idle connections you can enable server alive messages:

Host *
  ServerAliveInterval 15
  ServerAliveCountMax 3

In this case a alive message will be sent every 15 seconds over you ssh connection keeping it alive. Connection will be dropped after 3 failures (45 seconds).