git配置多個ssh key

在平常工做中公司會用到gitlab託管代碼,可是本身的一些項目會放到github上,這時就須要爲每一個託管平臺設置ssh key。下面是具體操做:git

1.生成ssh-key

進入到.ssh目錄下,輸入如下命令生成ssh-key,your_email@example.com填入本身的郵箱github

ssh-keygen -t rsa -C "your_email@example.com"

此時第一次輸入的文件名,若是直接按回車會自動生成私鑰和公鑰:id_rsa、id_rsa.pub;
第二次和第三次是密碼和確認密碼,此時直接回車便可。bash

clipboard.png

2.將ssh-key添加到ssh agent

ssh-add 私鑰文件名

若是執行ssh-add時提示」Could not open a connection to your authentication agent」
能夠先執行命令:服務器

ssh-agent bash

而後在從新運行ssh-add命令ssh

clipboard.png

3.修改配置文件

將不一樣的帳號對應的不一樣的ssh key 和不一樣的遠程服務器關聯起來,這個配置是在config下配置的(若是沒有config能夠本身新建)gitlab

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_a

Host git.gitlab.net
    HostName git.gitlab.net
    User git
    IdentityFile ~/.ssh/id_rsa_gitlab

其中,Host和HostName填寫git服務器的域名。
IdentityFile指定私鑰的路徑。
未加入配置文件的網站會自動應用id_rsa測試

4.將id_rsa.pub 上傳到GitHub或GitLab上

clipboard.png

測試下是否成功網站

ssh -T git@gitlab.com

出現welcome to gitlab!就表明鏈接成功了spa

如今你的多個ssh key就可使用了.net

相關文章
相關標籤/搜索