Git配置多个SSH-Key

1.背景

当有多个git账号时,比如:
a. 一个gitee,用于公司内部的工作开发;
b. 一个github,用于自己进行一些开发活动;

2.解决方法

生成一个gitee用的SSH-Key

ssh-keygen -t ed25519 -C "666@majiameng.com" -f ~/.ssh/gitee_id_rsa

生成一个github用的SSH-Key

ssh-keygen -t rsa -C '666@majiameng.com' -f ~/.ssh/id_rsa

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

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

4.用ssh命令分别测试

$ ssh -T git@gitee.com
$ ssh -T git@github.com

测试gitee,成功的话会返回下图内容

$ ssh -T git@gitee.com

Hi 我的数据裤呢?! You've successfully authenticated, but GITEE.COM does not provide shell access.
测试github,成功的话会返回下图内容

$ ssh -T git@github.com

The authenticity of host 'github.com (20.205.243.166)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
Hi majiameng! You've successfully authenticated, but GitHub does not provide shell access.
Last modification:May 8, 2023
如果觉得我的文章对你有用,请随意赞赏