管理Git生成多個ssh key

管理Git生成多個ssh key

  1. 生成兩個key
    • 生成key的命令ssh-keygen -t rsa -C “Your Email Address” -f 'Your Name',-f後面給的生成key的名字,如果沒有指定新的名字,那麼每次ssh-keygen生成的名字相同,就會發生覆蓋同名文件的情況的發生。
  2. 生成兩個key後,添加到對應服務器的ssh kyes管理設置中。
  3. 本地添加私鑰
    • 本地添加私鑰名命令ssh-add ~/.ssh/Your Key Name,如果出現“Could not open a connection to your authentication agent”的問題,可以執行命令ssh-agent bash,再運行添加命令。另外,可用通過
      • ssh-add -l 查看私鑰列表
      • ssh-add -D來清空私鑰列表
  4. 修改配置文件
    1.在~/.ssh目錄下新建一個config的文件
    2.添加以下內容
     # oschina
     Host git.oschina.net
     HostName git.oschina.net
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/oschina
     # github
     Host github.com
     HostName github.com
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/id_rsa
    
    3.一點說明
    • 以上的我採用的是oschina的碼雲和github的兩種kye的結合體,其中每段最後的IdentityFile跟着是你自己ssh key的名字,不要搞錯了。
    • 最後就是測試咯
      • oschina的測試方法 ssh -T [email protected];成功收到的回覆爲“Welcome to Git@OSC,‘Your Name’ ”
      • github的測試方法 ssh -T [email protected];成功收到的回覆爲”Hi ’Your Name‘ ! You’ve successfully authenticated, but GitHub does not provide shell access.“
  5. 借鑑文章
發佈了73 篇原創文章 · 獲贊 15 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章