git 多SSH key配置以及常見問題

git 多SSH key配置以及常見問題

針對(duì)每個(gè)賬戶生成不同的 SSH Key

ssh-keygen -t rsa -C "<郵箱,如:xxx@qq.com>"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): <輸入生成文件的名稱,隨意命名>
后面一路直接回車,不需要輸入密碼

例如:

生成第一個(gè) SSH Key:javabk_test_rsa

$ ssh-keygen -t rsa -C "javabk@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): javabk_test_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in javabk_test_rsa
Your public key has been saved in javabk_test_rsa.pub
...//省略

生成第二個(gè) SSH Key: javabk2_test_rsa

$ ssh-keygen -t rsa -C "javabk2@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): javabk2_test_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in javabk2_test_rsa
Your public key has been saved in javabk2_test_rsa.pub
...//省略

ssh-add 添加到高速緩存(可選)

通過 ssh-add 命令將專用私鑰添加到ssh-agent的高速緩存中,提升性能

ssh-add ~/.ssh/javabk_test_rsa
ssh-add ~/.ssh/javabk2_test_rsa

添加 SSH 公鑰到服務(wù)端

將上面每個(gè)生成的 .pub 里面的內(nèi)容全部配置到倉庫的SSH KEY,如上面的例子,將 javabk_test_rsa.pub 文件的內(nèi)容輸出,然后復(fù)制到服務(wù)端對(duì)應(yīng)配置SSH KEY的地方。

配置 ~/.ssh/config 文件

如果 ~/.ssh/config 文件不存在,則創(chuàng)建一個(gè)。配置如下:

Host javabk_github.com
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/javabk_test_rsa
        User javabk

Host javabk2_github.com
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/javabk2_test_rsa
        User javabk2

參數(shù)說明:

Host: 隨意命名,保證唯一,建議用戶名 + 倉庫域名地址

HostName: 倉庫域名地址

PreferredAuthentications: 強(qiáng)制使用Public Key驗(yàn)證

IdentityFile: 密鑰文件的路徑

User: 指定用戶名

常見問題

  1. git push 提交時(shí)報(bào)權(quán)限拒絕錯(cuò)誤,如下:

    $ git push
    ERROR: Permission to xxxx.git denied to xxx用戶名.
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    

    解決思路:

    1. 檢查報(bào)錯(cuò)的用戶名是否是提交倉庫應(yīng)該用到的用戶名,如果不是,檢查 .ssh/config 里面對(duì)應(yīng)的Host 有沒有指定 User;同時(shí)如果是同個(gè)倉庫多個(gè)用戶名,檢查提交代碼工程的 .git/config 文件中,[remote "origin"] 下面的 url = git@xxxHost:yyy.git 中的 Host是否跟 .ssh/config 的Host配置對(duì)應(yīng),否則可以修改想要的Host,這樣才會(huì)用到相關(guān)的配置進(jìn)行提交。

    錯(cuò)誤案例::

    • ~/.ssh/config , 同個(gè)網(wǎng)站,多個(gè)用戶名。其中第一個(gè)Host的用戶名是javabk,第二個(gè)用戶名是 javabk2

      Host github.com
              HostName github.com
              PreferredAuthentications publickey
              IdentityFile ~/.ssh/javabk_test_rsa
      
      Host javabk2_github.com
              HostName github.com
              PreferredAuthentications publickey
              IdentityFile ~/.ssh/javabk2_test_rsa
      
    • git clone 代碼后,工程里面的 .git/config 如下。側(cè)重看:url = git@github.com 中的 github.com,這個(gè)其實(shí)是對(duì)應(yīng)到上面的 ~/.ssh/config 里面 Host 為

      github.com 配置

      [core]
              repositoryformatversion = 0
              filemode = false
              bare = false
              logallrefupdates = true
              symlinks = false
              ignorecase = true
      [remote "origin"]
              url = git@github.com:javabk/javabk_demo.git
              fetch = +refs/heads/*:refs/remotes/origin/*
      [branch "main"]
              remote = origin
              merge = refs/heads/main
      
      
    • javabk2用戶的倉庫代碼進(jìn)行g(shù)it push 提交時(shí)報(bào)錯(cuò),如下,發(fā)現(xiàn)用戶名并不是 javabk2,而是javabk,應(yīng)該是沒指定時(shí)使用了默認(rèn)賬號(hào)

      $ git push
      ERROR: Permission to xxxx.git denied to javabk.
      fatal: Could not read from remote repository.
      
      Please make sure you have the correct access rights
      and the repository exists.
      

      解決:

      1. 修改代碼工程里面的 .git/config 中 [remote "origin"] 下的 url = git@github.com:javabk/javabk.demo.git 中的 github.com 改成 javabk2_github.com(對(duì)應(yīng) ~/.ssh/config 中對(duì)應(yīng)第二個(gè)用戶名的Host 配置)。如果這步還不行,嘗試執(zhí)行2

        url = git@javabk2_github.com:javabk/javabk_demo.git
        
      2. 在倉庫代碼里面指定對(duì)應(yīng)的用戶名和郵箱

        git config user.name "javabk2"
        git config user.email "javabk2@qq.com"
        

        或者直接編輯工程里面的 .git/config,添加下面這部分內(nèi)容(其實(shí)上面的命令就是添加這部分內(nèi)容)

        [user]
                name = javabk2
                email = javabk2@qq.com
        

本文由mdnice多平臺(tái)發(fā)布

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容