天天看點

管理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. 本地添加私鑰
    1. 本地添加私鑰名指令,如果出現“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]

        ;成功收到的回複為“Hi USERNAME! You’ve successfully authenticated, but GITEE.COM does not provide shell access. ”
      • github的測試方法

        ssh -T [email protected]

        ;成功收到的回複為”Hi ’Your Name‘ ! You’ve successfully authenticated, but GitHub does not provide shell access.“
  5. 借鑒文章
    • git 配置多個SSH-Key
    • 管理git生成的多個ssh key