Getting the most out of your ssh config

I typically find myself with voluminous bashrc files filled with aliases and functions for connecting to specific hosts via ssh. I would like an easier way to manage the various ssh hosts, ports, and keys.

I typically maintain an ssh-agent across multiple hosts, as well as various tunnels; reverse tunnels, and chained tunnels -- but I would like to simplify my normal ssh commands using an ssh config.

First, always remember to RTFM,

man ssh

This is an excellent starting point, the man page contains plenty of information on all the ins-and-outs of an ssh config.

To get started, simply create a plaintext file "config" in your .ssh/ directory.

Setting Defaults

$HOME/.ssh/config will be used by your ssh client and is able to set per-host defaults for username, port, identity-key, etc

For example,

# $HOME/.ssh/config
Host dev
    HostName dev.anattatechnologies.com
    Port 22000
    User twarnock
    ForwardAgent yes

On this particular host, I can now run

$ ssh dev

Which is much easier than "ssh -A -p 22000 twarnock@dev.anattatechnologies.com"

You can also use wildcards, e.g.,

Host *amazonaws.com *ec2.nytimes.com *.dev.use1.nytimes.com
    User root

which I find very useful for cases where usernames are different than my normal username.

Tunnels

Additionally, you can add tunneling information in your .ssh/config, e.g.,

Host tunnel.anattatechnologies.com
    HostName anattatechnologies.com
    IdentityFile ~/.ssh/anattatechnologies.key
    LocalForward 8080 localhost:80
    User twarnock

Even if you chose to use shell functions to manage tunnels, the use of an ssh config can help simplify things greatly.

This entry was posted in shell tips, ssh. Bookmark the permalink.

Comments are closed.