How to use ssh-agent with multiple (valid) keys and yet choose which one to use?

Let’s say there are two keys A and B, both of which are valid for [email protected] public key authentication. Since authorized_keys is configured for different behaviour depending on the key, ~/.ssh/config on the client uses something like

Host A.host
    HostName host
    User user
    IdentityFile ~/.ssh/A
Host B.host
    HostName host
    User user
    IdentityFile ~/.ssh/B

That works fine. However, the moment I use ssh-agent and add both keys A and B (e.g. in order to enter their passphrases at login instead of when I call the respective ssh A.host or ssh B.host), the connection will always use the same id for both virtual hosts. Is there any way to specify which stored key ssh should use from ssh-agent without having to remove the other key?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As answered elsewhere, the trick is adding the option IdentitiesOnly yes which makes sure that only the configured keys will be used even if others are available from the agent.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x