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

非 Y 不嫁゛ 提交于 2019-11-26 21:40:35

问题


I'm sure we all get this error from time to time:

$ git push origin master
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

The typical remedy is to simply create a public/private key pair and share it with your git host (in my case bitbucket, with their instructions)

The thing is though, I have many accounts that require that I have a public/private key pair (for example i need to save a key to connect to AWS.. etc).. so what I do is that i create these keys and save them in separate directories ie

~/.ssh $ find .
./awskeys
./awskeys/id_rsa
./awskeys/id_rsa.pub
./bitbucket
./bitbucket/id_rsa
./bitbucket/id_rsa.pub

but then this error pops up every now and then.. to solve it I have to move the relevant keys back to the root ~/.ssh. this doesn't seem right to me. How can I reliably do this?


回答1:


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 git@github.com:username/repo with:

git remote set-url origin mygithub:username/repo


来源:https://stackoverflow.com/questions/23751625/how-to-manage-multiple-ssh-keys-in-the-ssh-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!