Hostname Tab-Completion for OpenSSH

  2008-01-30


I use OpenSSH daily. In fact the only app I probably use more is vim. However, until yesterday I was typing out the full username and hostname when using ssh:

   ssh username@ridiculouslylong.domain.com

Ugh

Using the same username makes life a bit simpler:

   ssh ridiculouslylong.domain.com

Meh, better, but I’m really lazy!

I’d heard about tab-completion for hostnames from various blogs, but never knew how to do it. I hopped into #debian and thirty seconds later someone had kindly told me about <b>~/.ssh/config</b>

Not only is any host listed in your ~/.ssh/config auto-completed on the command line by hitting tab, but you can also specify what username to connect as! So my .ssh/config file looks something like:

Host host1.domain.com<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User randomuser<br /> Host www.somewhereelse.com<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User someotheruser<br /> Host mail.domain2.com<br /> Host domain3.com

Now I can just type:

&nbsp;&nbsp;&nbsp;ssh h<TAB><ENTER>

to connect to host1.domain.com as randomuser.

Beautiful.

Check out man ssh_config for details and other options.

Also, if you’re not using ssh keys instead of passwords, you’re doing too much work. Seahorse makes SSH keys simple.