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

前因:

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

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

生成新的ssh密钥

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

cd ~/.ssh

生成文件名和文件位置code

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

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

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

再把key加到ssh-agent里it

Start the ssh-agent in the background.class

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_rsafile

ssh-add -K ~/.ssh/id_rsa

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

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

参考地址github官网

相关文章
相关标签/搜索