有的时候,我们对于不同的网站想使用不同的SSH key,但是多个 SSH key 如何进行管理呢?下面是一个例子,在这个例子中分别为 gitlab(公司里常用) 和 github(个人用) 添加并且关联了不同的 SSH key,能够区分工作和个人学习的代码托管环境,供参考。
1.生成 SSH key
$ ssh-keygen -t rsa -C "xxx@163.com"
# 这一步很重要,参见 https://help.github.com/articles/error-agent-admitted-failure-to-sign/
$ ssh-add ~/.ssh/id_rsa
$ cat ~/.ssh/id_rsa.pub
2.将 SSH key 添加到 GitLab
打开 http://yourgitlab.com/profile/keys ,添加刚才创建的 SSH key 到你的 GitLab
3.测试是否添加成功
# 输入以下命令,如果出现 Welcome to GitLab, xxx! 则添加成功
$ ssh -T git@yourgitlab.com
4.多个 SSH key共存时的配置示例
编辑 ~/.ssh/config
# GitLab
Host yourgitlab.com
HostName yourgitlab.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# GitHub
Host github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
网友评论