Autocomplete SSH Hostnames

There are plenty of SSH autocomplete (or command-line completion) scripts available on the web, but I found most don’t go far enough — they usually just parse the ~/.ssh/known_hosts, ignoring the ~/.ssh/config and /etc/hosts files. Some of these scripts also generate a static autocomplete list at login, and can’t include new hostnames added during the session. The following script uses a function call to autocomplete hostnames dynamically, and fetches hostnames from the ~/.ssh/known_hosts, ~/.ssh/config and system-wide /etc/hosts file.

There are several places to execute an autocomplete script — my personal preference (if you have root access) is a script located in /etc/profile.d/ Most Linux distributions have an /etc/profile that sources additional files under /etc/profile.d/, and in those that don’t (like Mac OS X for example), you can include the following code at the end of your /etc/profile script [credit: CentOS 6.3].

An alternative (for those who can’t or won’t modify /etc/profile) might be to include the following script in your ~/.bashrc file instead.

Download the script.

Find this content useful? Share it with your friends!