git 配置多個SSH Key

2021-07-26 15:39:13 字數 1409 閱讀 1088

專案託管的倉庫多了,使用的賬號多了,自然用到的key就不同了,比如gitlab,bitbucket, github, 公司的code倉庫等,所以管理好key很重要。

1,生成乙個公司用的ssh-key

$ ssh-keygen

-t rsa -c

"[email protected]」 -f ~/.ssh/id-rsa

2,生成乙個github用的ssh-key

$ ssh-keygen

-t rsa -c

"[email protected]」 -f ~/.ssh/github-rsa

此時,.ssh目錄下應該有4個檔案:id-rsa和id-rsa.pub,github-rsa和github-rsa.pub,分別將他們的公鑰檔案(id-rsa.pub,github-rsa.pub)內容配置到對應的code倉庫上

3,新增私鑰

$ ssh-add ~/.ssh/id-rsa 

$ ssh-add ~/.ssh/github-rsa

如果執行ssh-add時提示」could not open a connection to your authentication agent」,可以現執行命令:

$ ssh-agent bash

# 然後再執行ssh-add命令。

# 可以通過 ssh-add -l 來確私鑰列表

$ ssh-add -l

# 可以通過 ssh-add -d 來清空私鑰列表

$ ssh-add -d

4,修改配置檔案

# 若.ssh目錄下無config檔案,那麼建立

touch 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/github_rsa

5,測試

$ ssh -t [email protected]

# 輸出

welcome to gitlab, your name!

Git配置多個SSH Key

之前週末在家使用github建立ssh key進行blog的提交,但是第二天在用公司,使用公司的gitlab提交 時發現賬號是我github的賬號,我想肯定是github生成的ssh key把之前我公司gitlab的ssh key給覆蓋了 檢視我所有ssh key cd ssh ls github ...

Git配置多個SSH Key

之前週末在家使用github建立ssh key進行blog的提交,但是第二天在用公司,使用公司的gitlab提交 時發現賬號是我github的賬號,我想肯定是github生成的ssh key把之前我公司gitlab的ssh key給覆蓋了 檢視我所有ssh key cd ssh ls github ...

git 配置多個SSH Key

專案託管的倉庫多了,使用的賬號多了,自然用到的key就不同了,比如gitlab,阿里雲git,公司的code倉庫等,所以管理好key很重要。1,生成乙個公司用的ssh key ssh keygen t rsa c 1email company.com f ssh id rsa2,生成乙個github...