Configure your favorite ssh hosts to type less

ssh is pretty straight forward. I spend years typing long lines into the console or setting up complicated UI to manage my favorite ssh hosts. But there are some tricks, which could save you a lot of time.

At first, you need to create an ssh config file. If you are on a mac the file is located at /etc/ssh_config. All other unix-like machines should create the file in ~/.ssh/config. To check where the OS is looking for a config file you could also type

ssh -v test

It should return something like this:

OpenSSH_5.9p1, OpenSSL 0.9.8x 10 May 2012
debug1: Reading configuration data /etc/ssh_config

Make sure the file has read-write permission to only your user.

chmod 600 /etc/ssh_config

Now, configure a your favorite hosts in the ssh_config file

Host example
HostName example.com
Port 667
User username
IdentityFile ~/.ssh/id_rsa

Now, instead of writing this:

ssh -i ~/.ssh/id_rsa -p 4431 username@example.com 

You can write this:

ssh example

As a small benefit this also works with scp.

scp /path/to/some/file example

Now let’s tunnel. If you are used to write this, to make a connection to a remote mySQL database

ssh -f -N -i ~/.ssh/id_rsa -L 9906:127.0.0.1:3306 username@database.example.com
# -f puts ssh in background 
# -N makes it not execute a remote command 

You should consider modifying your config like this:

Host tunnel
HostName database.example.com
IdentityFile ~/.ssh/id_rsa
LocalForward 9906 127.0.0.1:3306
User username

To be able to finally establish a tunnel with this command:

ssh -f -N tunnel

Published: July 23 2013

blog comments powered by Disqus