大多数 Git 服务器都会选择使用 SSH-Key 来进行授权。
一、用户根目录下创建 .ssh 文件夹
mkdir .ssh
二、在 .ssh 目录下生成各平台 ssh-key
1、创建方式
(1) ssh-keygen -t rsa
(2) ssh-keygen -t rsa -C 'github.com'
(3) ssh-keygen -t rsa -C 'github.com' -f id_rsa_github
三种方式均可:
方式(2): -C 后面的 'github.com' 是生成 ssh-key 的注释
方式(3): -f 后面是生成 ssh-key 的名称
结果如图所示:

- 红色框1处表示此次存储的 ssh-key 名称,不填默认为 id_rsa
- 红色框2和红色框3处创建和确定 push 文件时需要输入的密码,可以为空
注:方式(3)不会出现红色框1处,因为 -f id_rsa_github 已经设置过 ssh-key 名称
2、创建各平台对应 ssh-key
github
ssh-keygen -t rsa -C 'github.com' -f id_rsa_github
gitlab
ssh-keygen -t rsa -C 'gitlab.com' -f id_rsa_gitlab
gitee
ssh-keygen -t rsa -C 'gitee.com' -f id_rsa_gitee
三、添加 ssh-key 到各平台
1、查看获取 ssh-key
方式1
cat id_rsa_github.pub

方式2
pbcopy < id_rsa_github.pub
命令复制 id_rsa_github.pub 内容,直接粘贴即可
2、添加到各平台



四、在 .ssh 目录下配置 config
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
# gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee
config 内容,其中 Hos 和 HostName 填写 git 服务器的域名,IdentityFile 指定私钥的路径。
五、测试 ssh-key
1、测试 github
ssh -T git@github.com
结果如下图:

2、测试 gitlab
ssh -T git@gitlab.com
结果如下图:

3、测试 gitee
ssh -T git@gitee.com
结果如下图:

网友评论