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.

This entry was posted in GNU/Linux, IT, Open Source, Technology and tagged , . Bookmark the permalink.
  • http://dancoulter.com Dan

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

  • http://michael.susens-schurter.com/blog/ Michael Schurter

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

  • Don Spaulding

    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!

  • Paul

    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!

  • http://alt-j.com AltJ

    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.

  • Paul

    Thanks! I too have been trying to do this for a while! I have a vague recollection that a version of linux that I used to run used to automatically note what hostnames I’d ssh’d into and then would tab complete those too… anyone have any ideas on this?
    I especially like being able to note differing usernames between systems, ultra lazy!