配置:同一台电脑已有github,增加gitee

重新创建ssh key

# 进入用户目录下的 .ssh 文件夹下
cd ~/.ssh
# 生成 key,将邮件地址替换为你 Gitee 或者 Github 使用的邮件地址
ssh-keygen -t rsa -C "[email protected]"

接下来会看到下面的提示:

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/your_user_name/.ssh/id_rsa): id_rsa_gitee

这一步如果默认回车,会生成名为 id_rsa 的文件,输入不同的名字来便于识别文件,比如生成 Gitee 的 ssh key 可以设置为 id_rsa_gitee,设置 Github 的 ssh key 可以设置为 id_rsa_github

接下来不输入一直回车:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

在 .ssh 文件夹中创建 config 文件,添加以下内容以区分两个 ssh key:

# 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

创建完config文件,将新的SSH key的密钥加入ssh 的 agent中

$ ssh-add id_rsa_gitee

如果需要清除之前的,执行:

$ ssh-add -D
$ ssh-add id_rsa
$ ssh-add id_rsa_gitee

通过 ssh-add -l若执行ssh-add -D是出现这个错误:
Could not open a connection to your authentication agent,
则先执行 ssh-agent bash

复制id_rsa_gitee.pub 的内容,打开 Gitee 网站找到设置,再找到 SSH Keys,添加复制的 public key

测试连接是否正常
在命令行输入:

ssh -T [email protected]

若返回如下内容,则 Github 连接正常:

Hi yourname! You've successfully authenticated, but GitHub does not provide shell access.

继续在命令行输入:

ssh -T [email protected]

若返回如下内容,则 Gitee 连接正常。

Welcome to Gitee.com, yourname!

你可能感兴趣的:(配置:同一台电脑已有github,增加gitee)