github/gitlab添加多個ssh key


系統:macOS X
由於公司的代碼管理放在了gitlab.com上,所以添加了一個ssh key,
生成ssh key的代碼如下:
  1、$ ssh-keygen -t rsa -C “youremail@yourcompany.com”(回車,This creates a new ssh key, using the provided email as a label.)
  2、上一步回車后。會彈出詢問你保存ssh key的文件存放在哪里(默認是/Users/you/.ssh/id_rsa,如果不想切換地址,那就直接回車,本次測試是保存在默認地址的):
    
  3、上一步回車之后,會讓你輸入一個secure passphrase(可以直接回車):
    
 
  4、完成之后,到~/.ssh下可以查看到id_rsa和id_rsa.pub,將id_rsa.pub里的key拷出來,添加到你的gitlab賬戶里就行了。
 
—————————————我是分割線————————————
 
接着在github.com網站上再生成一個ssh key,生成過程與上一次唯一不同的地方在於保存ssh key的文件要換一下,假如上一次是/Users/you/.ssh/id_rsa,那么這次就可以切換成/Users/you/.ssh/id_rsa_github,相應的也會生成一個id_rsa_github.pub;
注意:
  因為SSH默認只讀取id_rsa,為了讓SSH識別新的私鑰,需要使用命令將其添加到SSH agent,命令如下:
    $ssh-add ~/.ssh/id_rsa
    $ssh-add ~/.ssh/id_rsa_github
  若執行ssh-add時提示“Could not open a connection to your authentication agent”,則執行下面的命令:
    $ssh-agent bash
  然后再運行ssh-add命令(可以通過ssh-add -l查看私鑰列表);
  接着修改配置文件:
    在~./ssh目錄下新建一個config文件,命令如下:
      touch config
      編輯config文件,往里添加內容如下:
        # gitlab
          Host gitlab.com
                  HostName gitlab.com
                  PreferredAuthentications publickey
                  IdentityFile ~/.ssh/id_rsa
        # GitHub
          Host github.com
                  HostName github.com
                  PreferredAuthentications publickey
                  IdentityFile ~/.ssh/id_rsa_github
  最后測試:
    $ssh -T git @github.com
    輸出:Hi user! You've successfully authenticated, but GitHub does not provide shell access. 就表示成功的連上github了
 
 
說明:我的上一篇隨筆也簡單說明了怎么新建ssh key,這次詳細點~
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM