本地ssh设置多个git项目访问

前因:

自己本地的~/.ssh里原本有个id_rsa,到了公司后新的git项目配置后,把自己原有的文件覆盖了,导致github和公司的项目我只能选一个,郁闷,怎么区分开呢?

大致逻辑是新生成一对密钥文件,指定成不同的文件名,然后在config里配置不同的host用不同的文件,具体的步骤如下:

生成新的ssh密钥

进入根目录的.ssh里,没有也行,生成文件名时会生成目录的

cd ~/.ssh

生成文件名和文件位置

ssh-keygen -t rsa -C "github" -f id_rsa_github

指定生成的文件名是id_rsa_github
指定备注是github
密码一般为空即可
此时的~/.ssh目录下会有:

  • id_rsa_github 私钥文件
  • id_rsa_github.pub 公钥文件

再把key加到ssh-agent里

Start the ssh-agent in the background.

eval "$(ssh-agent -s)"
Agent pid 59566

If you're using macOS Sierra 10.12.2 or later, you will need to modify your ~/.ssh/config file to automatically load keys into the ssh-agent and store passphrases in your keychain.

Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa

ssh-add -K ~/.ssh/id_rsa

再把.pub里的加到github的ssh key里
注意:生成的文件名是什么,加到config里的file名字就用自己生成的,ssh-add的也是

此时git clone git://地址 就OK了

参考地址github官网

原文地址:https://www.cnblogs.com/efan/p/10255492.html