本地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官網

相關文章
相關標籤/搜索