因為有兩個 git 公司一個gitLab 自己一個github
解決方案是生成兩套key太防,再生成一個配置文件
第一步:
將文件目錄切換到.ssh
$ cd ~/.ssh
查看當(dāng)前目錄
$ ls
第二步:
生成兩個ssh key
$ ssh-keygen -t rsa -C "XXX@XXX.XXX"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/XXX/.ssh/id_rsa): id_rsa
這里注意名稱第二個key不要將第一個key覆蓋
然后
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_test.
Your public key has been saved in id_rsa_test.pub.
The key fingerprint is:
SHA256:XXXXXXXXXX
XXX@XXX.XXX
The key's randomart image is:
+---[RSA 2048]----+
| X |
| X |
| X |
| X |
| X |
| X |
| X |
| X |
| X |
+----[SHA256]-----+
重復(fù)步驟2生成第二個key
$ ssh-keygen -t rsa -C "XXX@XXX.XXX"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/XXX/.ssh/id_rsa):id_ras_work
第三步:
$ vim id_rsa.pub
復(fù)制內(nèi)容
把id_rsa.pub加到你的第一個賬號上
$ vim id_rsa_work.pub
復(fù)制內(nèi)容
把 id_rsa_work.pub加到你的第二個賬號上
第四步:
把該key加到ssh agent上攒岛。由于不是使用默認(rèn)的.ssh/id_rsa若厚,所以你需要顯示告訴ssh agent你的新key的位置
$ ssh-add ~/.ssh/id_rsa
$ ssh-add ~/.ssh/id_rsa_work
$ ssh-add -l 確認(rèn)屬否添加成功
第五步:
配置config
$ vim config
gitlab
Host gitlab
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa
github
Host github
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
通過$ cat config 來查看config是否配置成功