1

This question was originally asked on SO, which is incorrect. As I was not able to (get it) moved to serverfault, I am re-asking it.

I am trying to configure tab completion for the ssh command in zsh to use an additional known_hosts file (which not located in ~/.ssh). By additional I mean I still want to use the "default" known_hosts file.

Currently, when I hit TAB after ssh, I get a list of hosts which seems to come from ~/.ssh/known_hosts and (probably) /etc/hosts. I would like to also get the hosts listed in ~/other/path/known_hosts.

I found a lot of material online, but somehow I cannot wrap my head around this one. The zsh webiste for example lists a style called known-hosts-files which sounds very promising. Also when I see how it is used it seems quite to the point.

But I cannot figure out how to use known-hosts-files.

I tried, for example, the following in my .zshrc:

zstyle ':completion:*:*:ssh:*:*' known-hosts-files '~/other/path/known_hosts' 

and many variations of it to no avail.

For good measure I have also added (in my .zshrc):

zstyle ":completion:*:commands" rehash 1 

but this did not change anything.

There is an older answer here on serverfault and another answer) tackling the same question, but I do not fully understand them and it seems that known-hosts-files should do exactly what I want?

I also use Oh-my-zsh, if that matters...

1 Answer 1

2

Well, finally I found the culprit: quotes.

When setting known-hosts-files to just a single file (like in my example), the quotes can be used, but not with ~!

Adding this to .zshrc works as intended (macOS):

zstyle ':completion:*:*:ssh:*:*' known-hosts-files 'Users/<username>/other/path/known_hosts' 

In hindsight it seems reasonable: to set known-hosts-files to a list of files, do not use quotes at all (~ is then OK to use again).

So the following works:

zstyle ':completion:*:*:ssh:*:*' known-hosts-files /etc/ssh/ssh_known_hosts ~/.ssh/known_hosts ~/other/path/known_hosts 

This seems to be related to the fact that the shell is interpreting the quoted text as a literal single string... Naturally...

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.