ssh tab completion with bash

Tired of typing out all your hostnames when ssh’ing a lot? Here’s a quick and easy way to get tab completion working for ssh!

1. Disable hashing of known hosts:

echo “HashKnownHosts no” >> ~/.ssh/config

2. Add the following line to your .bash_profile:

complete -W “$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ‘ ‘ | sed -e s/,.*//g | uniq | grep -v “\[“`;)” ssh

Then reload bash (by closing your terminal and reopening it, or just by running bash).

Now typing ssh <tab><tab> will list previously used hosts. If you have ssh’d to before, ssh ex<tab><tab> should autocomplete to ssh!

I hope this is useful!

3 Responses to “ssh tab completion with bash”

  1. Andy Millar Says:

    It may also be worth noting that in Ubuntu, this “just works(tm)” if you do not hash your known hosts.

  2. Tim Hughes Says:

    Or you could just install the bash completion package which gives you this and a lot more :-)

    Name : bash-completion
    Arch : noarch
    Version : 20060301
    Release : 13
    Size : 476 k
    Repo : installed
    Summary : Programmable completion for Bash
    URL :
    License : GPLv2+
    Description: bash-completion is a collection of shell functions that take advantage of the programmable completion feature of bash 2.

  3. SIMAVLI Says:

    thank you

Leave a Reply