MARWAN docs NOC knowledge base
DocsGNU/Linux

Enable ssh autocomplete on Debian 11

Add tab completion of SSH host names and IP addresses on Debian 11, as on RHEL-like systems.

Updated Sami Ait Ali Oulahcen

If you're used to autocomplete hosts and IPs in RHEL like systems such as Rocky or Alma Linux, having this feature missing on Debian can be disappointing. This is because the entries in ~/.ssh/known_hosts are hashed as a mitigation against host key leakage, and so can not be read by SSH completion. However, these entries are also found in ~/.ssh/config and can be reteived from there. The script in the next paragraph will do exactly that.

1. The autocomplete script#

Create the file /etc/bash_completion.d/ssh and copy the following content into it.

#!/bin/bash
_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

2. Make the script executable and source it#

chmod +x /etc/bash_completion.d/ssh
. /etc/bash_completion.d/ssh