How to manage multiple ssh keys in the ~/.ssh directory

You can have them anywhere you want, but their permission and the permission of the parent folders need to be strict:

  • no writable access for the parent folder (for others and all)
  • 644 for a public key
  • 600 for a private key.

You then:

  • declare those different keys in ~/.ssh/config (example here)
  • change the remote url in order to use the appropriate entry of the ~/.ssh/config file which described the right ssh key to use.

That means an entry like:

Host mygithub
    User           git
    IdentityFile   ~/.ssh/mypath/mykey # wherever your "new" key lives
    IdentitiesOnly yes

Allows you to replace an url like [email protected]:username/repo with:

git remote set-url origin mygithub:username/repo

Leave a Comment