Copied from my own answer on unix.SE:
It seems that specifically in Ubuntu the entries in ~/.ssh/known_hosts
are hashed, so SSH completion cannot read them. This is a feature, not a bug. Even by adding HashKnownHosts no
to ~/.ssh/config
and /etc/ssh/ssh_config
I was unable to prevent the host hashing.
However, the hosts that I am interested in are also found in ~/.ssh/config
. Here is a script for Bash Completion that reads the entries from that file:
_ssh()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(grep '^Host' ~/.ssh/config | awk '{print $2}')
COMPREPLY=( $(compgen -W "$opts" -- ${cur}) )
return 0
}
complete -F _ssh ssh
Put that script in /etc/bash_completion.d/ssh
and then source it with the following command:
$ . /etc/bash_completion.d/ssh
I found this guide invaluable and I would not have been able to script this without it. Thank you Steve Kemp for writing that terrific guide!