生成并添加第一個ssh key
第一次使用ssh生成key缅帘,默認(rèn)會在用戶~(根目錄)下生成 id_rsa, id_rsa.pub 2個文件;所以需要添加多個ssh key時也會生成對應(yīng)的私鑰和公鑰。
$ ssh-keygen -t rsa -C "youremail@yourcompany.com"
在Git Bash中執(zhí)行這條命令一路回車,會在 ~/.ssh/ 目錄下生成 id_rsa 和 id_rsa.pub 兩個文件,用文本編輯器將 id_rsa_pub 中的內(nèi)容復(fù)制一下粘貼到github(gitlab)上毯焕。
添加私鑰
$ ssh-add ~/.ssh/id_rsa
$ ssh-add ~/.ssh/id_rsa_github
如果執(zhí)行ssh-add時提示"Could not open a connection to your authentication agent",可以現(xiàn)執(zhí)行命令:
$ ssh-agent bash
然后再運行ssh-add命令磺樱。
可以通過 ssh-add -l 來確私鑰列表
$ ssh-add -l
通過 ssh-add -D 來清空私鑰列表
$ ssh-add -D
修改配置文件
在 ~/.ssh 目錄下新建一個config文件
touch config
添加內(nèi)容:
# gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
測試
$ ssh -T git@github.com
輸出
Hi user! You've successfully authenticated, but GitHub does not provide shell access. 就表示成功的連上github了