跳到主要内容
方的代码随记
返回

Git 配置多个 SSH-Key

更新于:

背景

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

默认只会用 ~/.ssh/id_rsa,需要为不同 Host 指定不同私钥。

操作步骤

1、为每个账号生成独立的 SSH-Key

ssh-keygen -t rsa -C 'xxxxx@company.com' -f ~/.ssh/gitee_id_rsa
ssh-keygen -t rsa -C 'xxxxx@qq.com'      -f ~/.ssh/github_id_rsa
ssh-keygen -t rsa -C 'xxxxx@qq.com'      -f ~/.ssh/git_company_id_rsa

2、新建 ~/.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/github_id_rsa

# bitbucket.org
Host bitbucket.org
HostName bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

# git.company.com
Host git.company.com
HostName git.company.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/git_company_id_rsa

3、逐个测试连通性

ssh -T git@gitee.com
ssh -T git@github.com
ssh -T git@bitbucket.org
ssh -T git@git.company.com

成功时的返回:

Hi ****! You've successfully authenticated, but GITEE.COM does not provide shell access.

如果私钥设置了 passphrase,会先提示输入:

Enter passphrase for key '/Users/***/.ssh/github_id_rsa':
Hi ****! You've successfully authenticated, but GitHub does not provide shell access.

补充


分享这篇文章:

上一篇
Git 与 yum 的证书错误处理
下一篇
Docker Compose 部署 Immich