Today we will see a little trick usually reserved for system administrators. It enables auto-completion when you want to connect to a remote server via SSH command. This is useful if you manage dozens of different servers and you’re tired of typing the name of the machine each time.
To set up auto-completion of commands ssh, scp and sftp, open a terminal, edit your ~/bashrc for him to add the following two lines.:
# Auto Completion SSH
complete -W “$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ‘ ‘ | sed -e s/,.*//g | uniq | grep -v “\[“`;)” ssh scp sftp
If this doesn’t work, try this line :
complete -W “$(echo $(grep ‘^ssh ‘ .bash_history | sort -u | sed ‘s/^ssh //’))” ssh
Save and exit. To save changes for the account, issue this command:
It only remains for you to test with ssh + the beginning of a host name + Tab.
Be careful though, this trick only works if you are already connected to the server before. Indeed, if the added line in bashrc closer we look, we see that the server list is generated based on that found in ~/.ssh/known_hosts. However, the latter is completed after accepting the ssh key identification at the first connection (when you type ‘yes’ before the password).