81

I have a few servers configured in ~/.ssh/config, such as alpha and beta. How might I configure Bash such that the commands $ ssh alTab and $ scp file.tgz alTab autocomplete the names of the configured servers?

I don't want to add the servers to another file (i.e. a Bash array) each time one is added, as we add and remove servers regularly and the list is quite large.

This is on Kubuntu 12.10, and I do have bash-completion installed.

3
  • 2
    Do you have the bash-completions package installed for your distro? I think this is part of the standard completion routines although I might be confusing it with zsh.
    – Caleb
    Jun 10, 2014 at 7:17
  • My install does exactly this.
    – slm
    Jun 10, 2014 at 7:28
  • This is on Kubuntu 12.10, and I do have bash-completion installed.
    – dotancohen
    Jun 10, 2014 at 7:44

8 Answers 8

90

Found it!!

It seems that 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 ~/.ssh/config.d/* 2>/dev/null | grep -v '[?*]' | cut -d ' ' -f 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!

13
  • 1
    It seems that in Centos 6 entries in ~/.ssh/known_hosts over-ride entries in ~/.ssh/config eg. if you have foo.bar.baz.com in known_hosts then it will match the whole thing if you do "ssh foo"<tab> - this is undesirable if you have a different user or other settings you want applied from the ssh confg. If you have an entry in ssh config that does not autocomplete properly then check if it is present in known_hosts and delete it. It should now work as normal taking the settings from config.
    – Imran-UK
    Apr 18, 2016 at 14:46
  • @Imran-UK: It is known that Ubuntu does not do that sensible thing, under the pretense of security. Apparently the thought is that a malicious entity might, as the user, edit ~/.ssh/config, despite chmod 0600 on the directory. Hey, I wasn't the one who made the decision!
    – dotancohen
    Apr 18, 2016 at 14:57
  • 1
    I use zsh & oh-my-zsh, I just discovered that enabling the "ssh" plugin and putting "HashKnownHosts yes" in my ~/.ssh/config seems to do the right thing. You can change shell whatever the distro so this might be a way around it.
    – Imran-UK
    Apr 20, 2016 at 9:21
  • @Imran-UK: In fact I've tried zsh in the past and I do intend on trying it again. I'll look into that. Shukran my friend!
    – dotancohen
    Apr 20, 2016 at 9:24
  • 1
    I know I'm a little late to the party, but RHEL 7 will autocomplete entries that are in /etc/hosts, other distros may do the same.
    – Centimane
    Jul 25, 2016 at 20:23
18

Pre-packaged

You don't say what distro you're using but on my Fedora 19 system I have the following package installed, bash-completion which provides this feature through this completion rule file:

/usr/share/bash-completion/completions/ssh

Here's the package I have installed:

$ rpm -aq |grep completion
bash-completion-2.1-2.fc19.noarch

If you look through that rule file you'll see stanzas that are interrogating the $HOME/.ssh/config file:

$ grep config /usr/share/bash-completion/completions/ssh
    local configfile
    local -a config
        # Search COMP_WORDS for '-F configfile' or '-Fconfigfile' argument
                    configfile="$(dequote "${1:2}")"
                    [[ $1 ]] && configfile="$(dequote "$1")"
        _known_hosts_real -a -F "$configfile" "$cur"
    local configfile
        # Search COMP_WORDS for '-F configfile' argument
                    configfile="$(dequote "${1:2}")"
                    [[ $1 ]] && configfile="$(dequote "$1")"
        _known_hosts_real -a -F "$configfile" "$cur"
    local configfile prefix
        # Search COMP_WORDS for '-F configfile' or '-Fconfigfile' argument
                    configfile="$(dequote "${1:2}")"
                    [[ $1 ]] && configfile="$(dequote "$1")"
                _known_hosts_real -c -a -F "$configfile" "$cur"

Rolling your own

I also found this Gist, known_hosts_autocomplete.sh, that does something similar except with the $HOME/.ssh/known_hosts file.

# add to ~/.bash_profile, and close/reopen a shell.  Will autocomplete any
# hosts found in known_hosts.

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

You could do something similar using your $HOME/.ssh/config file if for some reason you're unable to find the completion rule file for ssh already pre-packaged.

6
  • Thank you. It seems that Ubuntu does not ship with /usr/share/bash-completion/completions/ssh. Could you post that entire file somewhere that I might copy it? Thanks!
    – dotancohen
    Jun 10, 2014 at 7:48
  • You can get all the files from this archive: pkgs.fedoraproject.org/repo/pkgs/bash-completion/…
    – slm
    Jun 10, 2014 at 7:55
  • I'm sorry to be a pain, but even copying the ssh file from that tar archive to /usr/share/bash-completion/completions/ssh doesn't help! Neither does running the complete -W ... command from the Bash prompt. What might be the issue here?
    – dotancohen
    Jun 10, 2014 at 8:10
  • Thank you. This does seem to be a known design decision in Ubuntu. Thus I am accepting the answer even though I cannot get bash completion working for ssh or scp.
    – dotancohen
    Jun 10, 2014 at 9:13
  • 2
    Homebrew for OSX also has this: bash-completion and zsh-completions Oct 16, 2014 at 10:36
10

I found that the autocomplete was not working because Ubuntu hashes known hosts. You can add

Host *
    HashKnownHosts no

To your .ssh/config file but existing hosts won't be un-hashed.

4
  • Thank you. I've actually already added this directive to ~/.ssh/config and changed the directive in /etc/ssh/ssh_config, and I've removed the ~/.ssh/known_hosts file. After logging into the servers again, the host names are still being saved hashed.
    – dotancohen
    Jan 28, 2015 at 15:22
  • I have edited my answer; you will need to specify a host and nest the hash directive inside; I didn't list that as many .ssh/config files will already have this line. Let me know if that works.
    – M1ke
    Jan 29, 2015 at 10:28
  • This is definitely the easy way to do it! Given that (newer versions of) Ubuntu now have ssh completion enabled, this is probably what is wanted. Sep 24, 2015 at 8:51
  • As I added below, you can do this, but you probably shouldn't -- you don't need to in order to get completion working anyway. However, if you don't agree and just wanted to un-hash the existing hosts after adding that above, you can just rm ~/.ssh/known_hosts, and that will cause it to be re-generated (un-hashed) whenever you log into a new server. Jan 8, 2018 at 0:31
8

To enable ssh autocompletion in Debian and Ubuntu:

sudo apt-get install bash-completion

Note that this has nothing at all to do with known_hosts hashing, contrary to what was stated above and the original question. If you wanted to autocomplete from known_hosts, then of course you would have to disable hashing, but that is strongly recommended against.

For example, I have:

Host *
    HashKnownHosts yes

in my .ssh/config, and I still have ssh auto-completion working just fine against hosts listed in .ssh/config and /etc/hosts. You do need to add the host to .ssh/config as the OP stated:

Host my-awesome-host Hostname the.real.host.name

(Or, you can add a host entry to /etc/hosts, which is another source for the Debian/Ubuntu scripts.)

Then, you can just type ssh my-awe<tab> and it will be automagically completed. Again, this is even if you HashKnownHosts, which is highly recommended. (Note that bash completion needs to be enabled in your bash shell, and you need to specifically have those scripts installed as above for your distribution.)

Then, add these lines to your .bashrc to enable it (requires a logout and log back in, or just a new bash typed in to launch a new shell. (You don't need to enable if it's already enabled in /etc/bash.bashrc and /etc/profile sources /etc/bash.bashrc).

if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

This will enable ssh autocompletion (among other things!) from ~/.ssh/config, /etc/hosts, etc.

Note that Debian defaults to ash instead of bash. You can switch to bash easily:

sudo usermod -s /bin/bash "$USER"

(You'll need to log out and log back in for this to take effect.)

4

On Ubuntu 14.04, ssh auto-completes servers mentioned in your ~/.ssh/config

I noticed it when I realized that only one of the servers I commonly access auto-completed. The only difference between the two was an entry in the ssh config file that was related to authentication. When I added a new entry to the config file for the other server, it started auto-completing too.

Here's the entry for those who were asking:

HOST server-name 
    GSSAPIAuthentication=no

I would be very surprised if it mattered what you were specifying in the config (as long as it is still valid, of course).

2
  • Thank you Noah, what was that entry? On my 14.04 and 14.10 systems autocomplete does not work.
    – dotancohen
    Apr 3, 2015 at 19:52
  • I don't think the entry matters--I am pretty sure it is just having them in there--but I will add the entry to my answer.
    – Noah
    Apr 3, 2015 at 20:00
1

On CentOS 7 all it took was yum install bash-completion to auto complete hosts from .ssh/config when typing ssh and hitting Tab.

1

On macosx brew install bash-completion then add the following to ~/.profile

if [ -f $(brew --prefix)/etc/bash_completion ]; then
  . $(brew --prefix)/etc/bash_completion
fi
1
  • worked for me on macbook pro Apr 8 at 4:59
0

Add the following into ~/.bashrc or ~/.bash_profile

# Autocomplete ssh commands
WL="$(perl -ne 'print "$1\n" if /^Host (.+)$/' ~/.ssh/config | grep -v "*" | tr "\n" " ")"
complete -o plusdirs -f -W "$WL" ssh scp

Then run source ~/.bashrc or source ~/.bash_profile to pick up the configuration

Credit > https://blog.tinned-software.net/use-ssh-comfortably-with-command-history-and-auto-complete/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.