Why do some ssh sessions offer autocomplete and some not?

This isn't really a programming question, but it has to do with your shell. You can try starting the bash shell (by typing bash at the prompt) and see if you can autocomplete.

If that works you can use which bash to verify it's location and then chsh -s /bin/bash to set your shell permanently.

A list of available shells can also be found in /etc/shells.


It is a combination of the shell that is being used in your ssh session as well as it's configuration.

While your shell may support autocompletion it may not be configured for it. If you're using the bash shell, you can edit your local .bashrc file for the following to provide autocomplete.

# enable bash completion in interactive shells
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

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!


IIRC it could also be an issue that ssh hashes the hostnames in ~/.ssh/known_hosts

most installations I know use ~/.ssh/known_hosts as source for the list of available hosts for completion but some systems also started to set "HashKnownHosts yes", which prohibits using known_hosts as a source....

if your known hosts lines start with something like

|1|BWO5qDxk/cFH0wa05JLdHn+j6xQ=|rXQvIxh5cDD3C4

then hashing is activated.