第一步:生成密鑰
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "[email protected]"
出現:
Generating public/private rsa key pair.
Enter file in which to save the key (/u//.ssh/id_rsa):
以後可能多個帳號,所以可以存放在是/u//.ssh/id_rsa.user1
在.ssh下將會出現兩個文件 id_rsa.user1 和 id_rsa.user1.pub
第二步:添加公鑰
將id_rsa***.pub的公鑰信息複製到對應gitlab的添加ssh key裏
第三步:添加密鑰
因爲默認只讀取id_rsa,爲了讓SSH識別新的私鑰,需將其添加到SSH agent中:
ssh-add .ssh/id_rsa_***
如果出現Could not open a connection to your authentication agent的錯誤,試用以下命令:
ssh-agent bash
ssh-add .ssh/id_rsa_***
第四步:多個帳號對應同個gitlab的情況--修改config內的別名
在~/.ssh目錄下找到config文件,如果沒有就創建:
touch config
內容可以是:
Host githubuser1
HostName github.com
PreferredAuthentications publickey
identityFile ~/.ssh/id_rsa_user1
Host
githubuser2
HostName github.com
PreferredAuthentications publickey
identityFile ~/.ssh/id_rsa_user2
第五步:測試
$ ssh -T git@githubuser1