60

Is it possible to run SSH using another known_hosts file than the standard $HOME/.ssh/known_hosts for the user?

Note, that SSH uses $HOME original for the user. If you try to change it, e.g.

export HOME=some_other_location

it does not affect the location SSH uses to look for known_hosts.

Charles
  • 50,943
  • 13
  • 104
  • 142
user1085687
  • 601
  • 1
  • 6
  • 5
  • Yes. ~/.ssh/known_hosts2 . We symlink it to a file in our synced files folder. Any new server fingerprint is added there and everybody has instant access. – Pascal_dher Jun 16 '17 at 12:37

1 Answers1

93

You may specify an alternative known_hosts file in $HOME/.ssh/config or /etc/ssh/ssh_config, the parameter UserKnownHostsFile.

Alternatively, if you prefer to use alternative known_hosts file for a specific SSH invocation, you may use the SSH command option -o UserKnownHostsFile=<alternative known_hosts file>, e.g. ssh -o UserKnownHostsFile=~/my_temporary_known_host ssh_host.

Raymond Tau
  • 3,429
  • 26
  • 28
  • 1
    Thanks for your answer. I tried to avoid modifications in these configuration files, but it seems to be impossible. ;-( S.R. – user1085687 May 29 '12 at 15:55
  • 28
    Modifying the user's own `$HOME/.ssh/config` should not be a big problem, but `/etc/ssh/ssh_config` maybe. Or, you can specify that parameter on the ssh command line, i.e. `ssh -o UserKnownHostsFile=somewhere someone@ssh_host` – Raymond Tau May 29 '12 at 16:04
  • 2
    The `ssh -o` option is the correct answer to this question. It should also be an answer, not a comment. – CubicleSoft Jul 16 '16 at 16:06
  • 3
    Answer modified as per suggested, providing both methods to the readers. – Raymond Tau Jul 17 '16 at 06:18
  • I've added this option into `$HOME/.ssh/config` in my `Host *` global section and it works for all host aliases. – TCB13 Dec 06 '19 at 11:32