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 firstname.lastname@example.org -i /somepath/11.pem, I would put the following in my SSH config:
# ~/.ssh/config Host my-server-one User deploy HostName 220.127.116.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 18.104.22.168 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 22.214.171.124 IdentityFile "/somepath/11.pem" Host my-server-two Host 126.96.36.199 IdentityFile "/somepath/22.pem" Host my-server-three Host 188.8.131.52 IdentityFile "/somepath/33.pem" Host my-server-four Host 184.108.40.206 IdentityFile "/somepath/44.pem"
That way I greatly reduced my 500+ lines SSH config file to 67 lines.