DEV Community

Ahmed Musallam
Ahmed Musallam

Posted on

7

How To Autocomplete SSH Hosts

So you know about SSH config file, there are several awesome articles on the subject:

This awesome article

and this one that covers some tips and tricks

Now, with all of that, you still have to remember the full host entry to connect and it will not be autocompleted for you.

let's say I have the following in my config file:




Host unicorn-one
  HostName 1.2.3.4
  User amusallam
  IdentityFile ~/.ssh/id_rsa

Host unicorn-two
  HostName 2.2.3.4
  User amusallam
  IdentityFile ~/.ssh/id_rsa

Host unicorn-three
  HostName 3.2.3.4
  User amusallam
  IdentityFile ~/.ssh/id_rsa


Enter fullscreen mode Exit fullscreen mode

and now I type ssh unicorn into terminal and hit ⇥ Tab, it will not auto complete...

sad gif

But fear not! for I have what your heart desires!

Autocomplete Script

I've only tried this on MacOs.

courtesy of this stackexchange answer:

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…

add the following file: /etc/bash_completion.d/ssh whose contents:



_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


Enter fullscreen mode Exit fullscreen mode

Then load that file: in your ~/.bash_profile, add:



## load ssh autocompletion
. /etc/bash_completion.d/ssh



Enter fullscreen mode Exit fullscreen mode

and reload by running source ~/.bash_profile or just terminate your terminal and reopen it.

and voila!

(The flashes you see below are me hitting ⇥ Tab)

Alt Text

easy peasy!

Extra helpful alias

if you want to see a list of all your hosts, you can add this alias to your .bash_profile:



# list all "Host" and "HostName" lines, then remove the strings: "Host " and "HostName "
alias sshhosts="grep -w -i -E 'Host|HostName' ~/.ssh/config | sed 's/Host //' | sed 's/HostName //'"



Enter fullscreen mode Exit fullscreen mode

reload by running source ~/.bash_profile or just terminate your terminal and reopen it.

then you can run it:



sshhosts


Enter fullscreen mode Exit fullscreen mode

from unicorn example above, this prints:



unicorn-one
  1.2.3.4
unicorn-two
  2.2.3.4
unicorn-three
  3.2.3.4


Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (2)

Collapse
 
tobiashochguertel profile image
Tobias Hochgürtel

The "Extra helpful alias" isn't useful when you have your ssh config stored in several config files. As example, I have a few ssh-config files with host definitions stored under ~/.ssh/config.d/and my ~/.ssh/config has just one line to include all config files from the sub-folder config.d/.

My ~/ssh/config has only one line:

Include config.d/*.config
Enter fullscreen mode Exit fullscreen mode

and the directory: config.d/ has the following files:

~/.ssh/config.d/1-homeoffice.config
~/.ssh/config.d/2-github.com.config
...
Enter fullscreen mode Exit fullscreen mode

Quick solution for fixing the "extra Helpful alias is to use the $opt-variable-command from the bash-completeion:

alias sshhosts="grep '^Host' ~/.ssh/config ~/.ssh/config.d/* 2>/dev/null | grep -v '[?*]' | cut -d ' ' -f 2-"
Enter fullscreen mode Exit fullscreen mode

okay it prints then only the Host description, without their ip-address / hostname value. But it lists all definitions from all my ssh-config files.

Collapse
 
vg4r profile image
Vugar Suleymanov

Also it is good to add complete -F _ssh scp to the end of /etc/bash_completion.d/ssh file, to enable autocompletion in scp command.. Thank you for helpful article

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay