一个开发者在同一台机器上拥有多个 Git 账号,是比较常见的。如 Gitee 用于工作,GitHub 用于个人,这时候往往需要配置多个 SSH-Key。
生成 SSH-Key
# 生成 GitHub 用的 SSH-Key
$ ssh-keygen -t rsa -C 'xxx@qq.com' -f ~/.ssh/github_id_rsa
# 生成 Gitee 用的 SSH-Key
$ ssh-keygen -t rsa -C 'xxx@company.com' -f ~/.ssh/gitee_id_rsa
在 ~/.ssh 目录下的 config 文件(若没有则创建),添加如下内容:
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
其中 Host 和 HostName 填写 Git 服务器的域名,IdentityFile 指定私钥的路径。
用 SSH 命令分别测试。
$ ssh -T git@github.com
$ ssh -T git@gitee.com
如果成功的话,会返回以下内容。
$ ssh -T git@github.com
Hi toFrankie! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@gitee.com
Hi 越前君! You've successfully authenticated, but GITEE.COM does not provide shell access.
参考 Gitee(link:https://gitee.com/help/articles/4229)

