2015-09-17 | Git | Unlock

Git配置多个SSH-Key

背景

当有多个git账号时,比如:

a. 一个gitlab,用于公司内部的工作开发;
b. 一个github,用于自己进行一些开发活动;
c. 一个gitee,用于其他的一些开发活动;

解决方法

(1)生成一个gitlab用的SSH-Key

ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/id_rsa_gitlab

(2)生成一个github用的SSH-Key

ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/id_rsa_github

(3)生成一个gitee用的SSH-Key

ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/id_rsa_gitee

(4)在 ~/.ssh 目录下新建一个config文件,添加如下内容(其中Host和HostName填写git服务器的域名,IdentityFile指定私钥的路径)

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
# github
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab

(5) 用ssh命令分别测试

1.ssh -T [email protected]    
成功返回 ==>  Welcome to GitLab, @xxx!

2.ssh -T [email protected]
成功返回 ==>  Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

3.ssh -T [email protected]
成功返回 ==>  Hi xxx! You've successfully authenticated, but GITEE.COM does not provide shell access.

评论加载中