Hostname Tab-Completion for OpenSSH

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 ~/.ssh/config

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
     User randomuser
Host www.somewhereelse.com
     User someotheruser
Host mail.domain2.com
Host domain3.com

Now I can just type:
   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.

Tags: ,

5 Responses to “Hostname Tab-Completion for OpenSSH”

  1. Dan Says:

    I thought you linux people liked typing. I thought that’s why you use linux.

  2. Michael Schurter Says:

    @Dan: Typing is good. Less typing is better.

  3. Don Spaulding Says:

    Michael:

    Just happened upon your blog again today looking at djangopeople. I too live my life through ssh and have been vainly hitting the tab key for the last month or so expecting it to Just Work. Nano’d my .ssh/config and 15 seconds later was tab-completing like Coca-Cola. Much thanks!

  4. Paul Says:

    Michael:

    What a fascinating post. I read the whole thing, and understood all the English words and have no idea what you’re talking about. I’m proud to have a smart brother!

  5. AltJ Says:

    cool idea! I have a few hosts that I frequently login to so I just setup aliases for them in my .bashrc:

    alias dbhost=’ssh -C root@mysqlhost.somelongdomainname.com

    Re: Paul’s comment, I get the same feedback from my relatives about my blog.

Leave a Reply