Janitha Karunaratne

I convert Caffeine in to Ideas and Code

Easy life with SSH Configs

If you are ssh'ing constantly to a lot of boxes using long ssh commands with many switches, you are doing it wrong. Make your life easier with the ssh config file ~/.ssh/config.

Host home home.example.com
    HostName home.example.com
    User j
    Port 12345
    LocalForward 45901 localhost:5901
    LocalForward 40080 localhost:80

Now all you need to do is "ssh home" and it will connect using those options. That easy. All the options can be found in ssh_config(5)

Better yet, the Host option can contain wildcards, so you can set common options for sets of domains. I use it extensively when logging in to Amazon EC2 machines.

Host *amazonaws*
    IdentityFile ~/.ssh/your_ec2_key.pem
    User root

Now all you need to do is "ssh blah-1-2-3-4.ec2.amazonaws.blah.com" and it will connect using your EC2 private key as root.

Another great usage is if you constantly vnc in to your home computer remotely through ssh tunnels. Usually you set up a ssh with a local forward, then fire up vnc manually. Well now you can do it in one command.

Host remote
    Hostname home.example.com
    User someuser
    Port 12345

    LocalForward 59999 localhost:5901
    ExifOnForwardFailure yes

    PermitLocalCommand yes
    LocalCOmmand vncviewer localhost:59999 &>2 &

So now you just simply type "ssh remote" and it will set up the tunnel and pop up the vnc window. Enjoy!