SSH Config

Published on 15 Sep 2019

SSH Config

I have been across this about two years back and I thought everyone would know how to use it. But turns out, at work I find it very useful and many of my co-workers are not aware of it entirely.

This post will talk a bit about ssh_config and how it can make your life easier, assuming you use SSH very frequently. Also being a *nix user I don’t know if this will be useful for Microsoft Windows users.

I use one laptop for work related stuff which comprises mostly connecting to, debugging and setting up AWS EC2 Instances. Now when it comes to connecting to any instance I used to type a long command as follows.

ssh -i ~/that-access-key.pem ec2-user@x.x.x.x

This should look simple like we can add a bash alias or something to it. But the bigger problem comes when you have a custom AMI and it uses different port sometimes.

ssh -i ~/that-access-key.pem ec2-user@x.x.x.x -p 1234

Once again if there’s different user, ssh private key or port for different accounts like.

ssh -i ~/that-key-1.pem ubuntu@10.0.x.x -p 1234
ssh -i ~/that-key-2.pem admin@10.1.x.x -p 1231

This just makes life bit messy to just SSH into a server. Think about if there’s something on fire or a production bug.

*drum roll* :drum:

Let’s handle this with ssh_config. To get more details about ssh_config you can do man ssh_config but most handy and useful part should be covered in this post.

Grouping hosts

The configuration can be done at a all host level (*) which would be the default configurations for all the hosts you SSH to. There is a possiblity to group hosts together and then apply configurations to that group of hosts.

So following are some examples of groups

# All hosts
Host *

# Single host
Host github.com

# Group of hosts
Host 10.0.*.*

# Another group
Host 10.1.*.*

Actual configurations - the fun part

These are basically all the configurations which are supported by SSH Client. The list is really big but what I have used till now are as follows


So final ssh config should look like this. By default ssh client looks in /etc/ssh/ssh_config and overriding those configurations from ~/.ssh/config.

Host 10.0.*.*
  Port 1234
  User ubuntu
  IdentityFile ~/that-key-1.pem
Host 10.1.*.*
  Port 1231
  User admin
  IdentityFile ~/that-key-2.pem

Bonus Tip

I have some servers which have different access keys and use git push to that server. So essentially I have to override the port and ssh private key whenever I am pushing it to that place. I have added that Host in configuration and now it pushes flawlessly.

Last Updated on: 15 Sep 2019