论代码托管平台哪家强,当然是非 github 莫属了,但鉴于国内直连 GitHub 这忽好忽差的网络环境,还是 gitee 靠谱(也就靠谱了,有时找东西还得是GitHub啊)。所以一般都会有两个账号啦,左手 gitee,右手 GitHub,那么问题来了,我要在一台电脑上使用这俩货该怎么配置嘞?
食用前提,电脑已经安装好了 git;本文演示环境为 win11,其他操作系统同理(建议在 git bash 中操作)
cd ~/.ssh
进入 .ssh 文件夹ls
查看是否有不需要的,使用 rm 文件名
删除cd 进入到 ~/.ssh 目录下执行
ssh-keygen -t ed25519 -C "[email protected]" -f "id_rsa_github"
如果你使用的旧系统不支持Ed25519算法,请使用
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f "id_rsa_github"
然后无脑敲回车就好,直到出现这个
Your identification has been saved in id_rsa_github.
Your public key has been saved in id_rsa_github.pub.
The key fingerprint is:
SHA256:1ruKefyMCVLzyS7Rrh75sOOesq1jKO5mzHnGqh3rm5g [email protected]
The key's randomart image is:
+--[ED25519 256]--+
| |
| |
| |
| . |
| o.S . |
| ..*.. . |
|o +.. =++ . |
|.X.B+oo@++ . |
|E*XooO@=*o+ |
+----[SHA256]-----+
或这个
Your identification has been saved in id_rsa_github.
Your public key has been saved in id_rsa_github.pub.
The key fingerprint is:
SHA256:i74gOIYOxNsXtdNsTHJdGzED+zx2hl+17lZ3mINm21Q [email protected]
The key's randomart image is:
+---[RSA 4096]----+
| ..*. |
| . o = |
| o o o . .|
|. . O o . E|
| o . oS= B B.|
|o.o ..o. = @ =|
|=o..... . o + ++|
|+. ..o . o. |
| . o. .. |
+----[SHA256]-----+
与创建 github 的操作一致,仅在 -f 文件名参数上使用 _gitee 做区别
ssh-keygen -t ed25519 -C "[email protected]" -f "id_rsa_gitee"
如果你使用的旧系统不支持Ed25519算法,请使用
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f "id_rsa_gitee"
~/.ssh
文件夹内生成以下文件~/.ssh
$ ls
id_rsa_gitee id_rsa_github id_rsa_gitee.pub id_rsa_github.pub
如果执行报错 “Error connecting to agent: No such file or directory”,可参考win 执行 ssh-add 报错 Error connecting to agent: No such file or directory
ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitee
Identity added: C:/Users/liuti/.ssh/id_rsa_github ([email protected])
在 ~/.ssh 目录下新建一个config文件(可用 touch 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
cat ~/.ssh/id_rsa_github.pub
得到你的公钥内容,复制下来cat ~/.ssh/id_rsa_gitee.pub
得到你的密钥内容,复制下来用ssh命令分别测试
ssh -T [email protected]
ssh -T [email protected]
有些同学可能会有弹窗提示
“The authenticity of host ‘gitee.com (xxx.xxx.xxx.xxx)’ can’t be established.
ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxx”
此时我们只需要在弹窗输入框输入 yes 并回车就好
如果成功,会返回如下内容
$ ssh -T [email protected]
Hi 呀呀呀呀呀樂! You've successfully authenticated, but GITEE.COM does not provide shell access.
$ ssh -T [email protected]
Hi Pedro-null! You've successfully authenticated, but GitHub does not provide shell access.