github/gitlab添加多個ssh key

系統:macOS X
因爲公司的代碼管理放在了gitlab.com上,因此添加了一個ssh key,
生成ssh key的代碼以下:
  一、$ ssh-keygen -t rsa -C 「youremail@yourcompany.com」(回車,This creates a new ssh key, using the provided email as a label.)
  二、上一步回車後。會彈出詢問你保存ssh key的文件存放在哪裏(默認是/Users/you/.ssh/id_rsa,若是不想切換地址,那就直接回車,本次測試是保存在默認地址的):
    
  三、上一步回車以後,會讓你輸入一個secure passphrase(能夠直接回車):
    
 
  四、完成以後,到~/.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,此次詳細點~
相關文章
相關標籤/搜索