Deprecation notice

Deprecation Notice: This blog is no longer updated. Please visit my current blog at

Sunday, April 10, 2011

Saving time with SSH: ~/.ssh/config

If you're a web developer these days, you probably end up SSHing to a lot of different hosts. In the past, it used to be simple: ssh hostname, type your password, get in. Now it's much more common to use identity files (perhaps not your personal identity file, either), custom port numbers, and other custom SSH settings.

Often, people solve problems like this with an alias:

alias sshweb='ssh -p 3022 -X'

It turns out there's a better way: use your local ssh config file! This is a file that lives in your ~/.ssh directory, simply named config. It uses all of the same conventions as the /etc/ssh/ssh_config file, so you may already be comfortable with the syntax and usage.

In this file, you can simply write Host foo, and everything that follows this will apply only to ssh sessions with that host. Even better, it works with scp and other tools that invoke OpenSSH (scripts, libraries, etc.). The shell alias approach doesn't help you there!

Obligatory example:

Host home
Port 4022
ForwardX11 yes

Host webstage
User webuser
IdentityFile ~/.ssh/id-webserver
ForwardAgent yes

Pretty much any SSH configuration variable can be set on a per-host basis this way. You can also see that hosts can be given multiple names (separated by spaces), as in the "webstage" example. This host can be reached by SSHing to either "webstage" or "". I use this because, on the command line, I prefer to type a short name, but our deployment scripts generally use the entire hostname. Listing them both means the same SSH configuration will always be used.

Now go get rid of those aliases (or stop typing long-ass command lines all the time) and enjoy this newfound power!

More Resources

No comments:

Post a Comment