I'm looking for a feature that help me to autocomplete the servers I "sshed" in sometime and are in my .bash_history file without messing with known_hosts file or /etc/bash_completition, just grab them from my history.

I cannot mess with the known_hosts file because the info in there is like "encrypted" and looking how to avoid this is like not that important.


$ tail -n 2 ~/.ssh/known_hosts
|1|D90I41jNxFy83ZhAoyG7gj0+LpM=|Zkru+0Yi9Q9UNafHpj1IVMGGnAw= ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArih5PrEeGsMHm+9ytkiiOgNNIh6fQAEFfdDw9IwkOGs13g+YN6k61rRbdXj97N+vvuGslcCL9uDMY+M6bqDX4k2kGJuvmsSBgIWOjOiOtrW6wWaflGO0OKQ39F1R92Bw3gB1kNg8Es3XjNE1D+CRXt/EQLWLBFmF3p41PpoRg/Gyvw4XqQSMOYdGsr0OaE8ozrXWO2rQo2NDe6BDJzSGqZ234jCtnRLEtFBFvisTEbNCYFRkhKMSfrUecyK5bjHRehEHKfsPxNDTCOl7xrx1DDTng/BIgxyCA21s+xa05oNulkMMd/1qNrekoUUFxwJpb1rf6vrW5CkeMIOhCr5Nxw==
|1|Cl6c716uEoPmovaRDDwkM+bYFTc=|VXUrsTRDptMOg/Nm3anFMwjy5O8= ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArih5PrEeGsMHm+9ytkiiOgNNIh6fQAEFfdDw9IwkOGs13g+YN6k61rRbdXj97N+vvuGslcCL9uDMY+M6bqDX4k2kGJuvmsSBgIWOjOiOtrW6wWaflGO0OKQ39F1R92Bw3gB1kNg8Es3XjNE1D+CRXt/EQLWLBFmF3p41PpoRg/Gyvw4XqQSMOYdGsr0OaE8ozrXWO2rQo2NDe6BDJzSGqZ234jCtnRLEtFBFvisTEbNCYFRkhKMSfrUecyK5bjHRehEHKfsPxNDTCOl7xrx1DDTng/BIgxyCA21s+xa05oNulkMMd/1qNrekoUUFxwJpb1rf6vrW5CkeMIOhCr5Nxw==

I found this solution but for other not important reason, I have not the ~/.ssh/config file.


Much easier way would be to disable hashing of your known_hosts with HashKnownHosts no in your ~/.ssh/config to allow autocomplete from all history, or just list your hosts in your ~/.ssh/config (you can store there all your Ports and Users, or create aliases).

I appreciate the creativity, but why to reinvent wheel when we have got already the same functionality out of the box (except Ubuntu shipping HashKnownHosts yes by default, which breaks this functionality).


So as I only and only want to know how to autocomplete my hosts targets within the bash_history file, I found this feature hoping to help anybody in the same situation.

Just execute the following line

$ complete -W "$(echo $(grep '^ssh ' .bash_history | sort -u | sed 's/^ssh //'))" ssh
  • complete - is a bash builtin function. So there is not a binary on the system. It handles how commands will be completed when pressing
  • -W - make an autocompletition wordlist
  • grep '^ssh' .bash_history - return all lines that begin with ssh from .bash_history file
  • sort -u - make the output lines uniq
  • sed 's/^ssh //' - remove the beginning ssh string from each line
  • ssh - when executing ssh command

If you not only ssh hostname and use others such as ssh user@hostname or ssh user@hostname -p 2222 you must play with the regex and grep|sed features to only keep the hostname or whatever you want to autocomplete

Insert the complete line to your .bashrc file so you have the autocomplete feature every you start a session.

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.