为一个 Git 仓库配置多个远程仓库

个人实际应用 Git 时可能需要将一个本地仓库同步到多个远程仓库,比如将一个仓库同时同步到 Gitee 和 Github 中。下面介绍多个远程仓库的 SSH 密钥的配置方式。

# 生成连接 Gitee 的 SSH 密钥文件
[vincent@#localhost vincent]# ssh-keygen -t rsa -b 4096 -C '[email protected]' -f ~/.ssh/gitee_id_rsa
# 生成连接 Github 的 SSH 密钥文件
[vincent@#localhost vincent]# ssh-keygen -t rsa -b 4096 -C '[email protected]' -f ~/.ssh/github_id_rsa
# 新建一个 ~/.ssh/config 文件
[vincent@#localhost vincent]# vim ~/.ssh/config
# 在 ~/.ssh/config 文件中添加以下内容
[vincent@#localhost ~]$ cat ~/.ssh/config
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa

Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
# 启动 SSH 客户端进程
[vincent@#localhost ~]$ eval "$(ssh-agent -s)"
Agent pid 32070
[vincent@#localhost ~]$ ssh-add ~/.ssh/gitee_id_rsa
Identity added: /home/vincent/.ssh/gitee_id_rsa (/home/vincent/.ssh/gitee_id_rsa)
[vincent@#localhost ~]$ ssh-add ~/.ssh/github_id_rsa
Identity added: /home/vincent/.ssh/github_id_rsa (/home/vincent/.ssh/github_id_rsa)
# 将 ~/.ssh/gitee_id_rsa.pub 中内容复制填入 Gitee 的 SSH 公钥输入框里
# 将 ~/.ssh/github_id_rsa.pub 中内容复制填入 Github 的 SSH 公钥输入框里
# Windows 平台的 Git Bash 中可以直接使用 clip 命令将复制到粘贴板中
# clip < ~/.ssh/gitee_id_rsa.pub
# clip < ~/.ssh/github_id_rsa.pub
# 使用 ssh -T 命令验证连接,发现 ~/.ssh/config 文件权限不足
[vincent@#localhost ~]$ ssh -T [email protected]
Bad owner or permissions on /home/vincent/.ssh/config
# 修改.ssh/config的权限:
[vincent@#localhost ~]$ chmod 600 .ssh/config 
# 或者
[vincent@#localhost ~]$ chown $USER .ssh/config
[vincent@#localhost ~]$ ll ~/.ssh/config
-rw-------. 1 vincent vincent 208 10月 31 23:15 /home/vincent/.ssh/config
[vincent@#localhost ~]$ ssh -T [email protected]
The authenticity of host 'gitee.com (212.64.62.183)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
ECDSA key fingerprint is MD5:27:e5:d3:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,212.64.62.183' (ECDSA) to the list of known hosts.
Hi rosydawn! You've successfully authenticated, but GITEE.COM does not provide shell access.
[vincent@#localhost ~]$ ssh -T [email protected]
Hi hzxrosydawn! You've successfully authenticated, but GitHub does not provide shell access.

你可能感兴趣的:(为一个 Git 仓库配置多个远程仓库)