Some time ago, I decided to reduce the size of my SSH config file. For those who don’t know, this file is usually used to store server aliases to easily SSH into remote machines.

For example, to avoid typing the long command ssh deploy@11.11.11.11 -i /somepath/11.pem, I would put the following in my SSH config:

# ~/.ssh/config

Host my-server-one
  User deploy
  HostName 11.11.11.11
  IdentityFile "/somepath/11.pem"

With that in place, my terminal command shortens to ssh my-server-one. There are some extra settings I can put in place which come in handy down the line. Overall, the config takes the following form:

# ~/.ssh/config

Host my-server-one
  ForwardAgent yes
  ServerAliveInterval 120
  StrictHostKeyChecking no
  User deploy
  HostName 11.11.11.11
  IdentityFile "/somepath/11.pem"

It’s a great way to easily store server connection configurations. But after you add a certain number of servers, the file can be very cluttered - as was mine.

I decided to use some settings which my config-blocks could inherit from. Example:

# ~/.ssh/config

# These settings apply to all configs if not overriden
Host *
  ForwardAgent yes
  ServerAliveInterval 120
  StrictHostKeyChecking no

# These settings apply to hostnames beginning with "my-server-"
Host my-server-*
  User deploy

Host my-server-one
  Host 11.11.11.11
  IdentityFile "/somepath/11.pem"

Host my-server-two
  Host 22.22.22.22
  IdentityFile "/somepath/22.pem"

Host my-server-three
  Host 33.33.33.33
  IdentityFile "/somepath/33.pem"

Host my-server-four
  Host 44.44.44.44
  IdentityFile "/somepath/44.pem"

That way I greatly reduced my 500+ lines SSH config file to 67 lines.