Howto force ssh to use a specific private key?

44

5

With ssh -i <private key filename> you can instruct ssh to use an extra private key to try authentication.

The documentation is not clear on how to explicitly use only that key.

Herman van Rink

Posted 2014-06-24T07:18:34.793

Reputation: 1 193

Answers

65

You can use the IdentitiesOnly option:

ssh -o "IdentitiesOnly=yes" -i <private key filename> <hostname>

Herman van Rink

Posted 2014-06-24T07:18:34.793

Reputation: 1 193

12actually 'IdentitiesOnly' disables prompting ssh-agent, but still offers defaults and ssh_config'd keys. – rogerovo – 2014-06-25T06:55:38.940

2The important thing for me was that it does not look in e.g. my ~/.ssh directory for keys to try. – Herman van Rink – 2014-06-25T08:50:17.787

Thanks! I needed the -o "IdentitiesOnly=yes" bit to prevent ssh-agent from overriding the private key specified. – user2708667 – 2019-03-19T20:10:08.990

This is super handy for determining which key works with a given host when you have keys cached in ssh-agent. The only way I could figure it out without this flag was to use strace to dump the IO, which was pretty tedious. – Wil – 2019-06-05T16:11:24.860