跳到主要内容

SSH 公钥设置

Gitee 提供了基于 SSH 协议的 Git 服务,在使用 SSH 协议访问仓库仓库之前,需要先配置好账户 SSH 公钥。

仓库公钥(部署公钥)请移步 添加部署公钥

生成 SSH 公钥

Windows 用户建议使用 Windows PowerShell 或者 Git Bash,在 命令提示符 下无 catls 命令。

1、通过命令 ssh-keygen 生成 SSH Key:

ssh-keygen -t ed25519 -C "Gitee SSH Key"
  • -t key 类型
  • -C 注释

输出,如:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/git/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/git/.ssh/id_ed25519
Your public key has been saved in /home/git/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:ohDd0OK5WG2dx4gST/j35HjvlJlGHvihyY+Msl6IC8I Gitee SSH Key
The key's randomart image is:
+--[ED25519 256]--+
| .o |
| .+oo |
| ...O.o + |
| .= * = +. |
| .o +..S*. + |
|. ...o o..+* * |
|.E. o . ..+.O |
| . . ... o =. |
| ..oo. o.o |
+----[SHA256]-----+
  • 中间通过三次回车键确定

2、查看生成的 SSH 公钥和私钥:

ls ~/.ssh/

输出:

id_ed25519  id_ed25519.pub
  • 私钥文件 id_ed25519
  • 公钥文件 id_ed25519.pub

3、读取公钥文件 ~/.ssh/id_ed25519.pub

cat ~/.ssh/id_ed25519.pub

输出,如:

ssh-ed25519 AAAA***5B Gitee SSH Key

复制终端输出的公钥。

设置账户 SSH 公钥

用户可以通过主页右上角 「个人设置」->「安全设置」->「SSH 公钥」->「添加公钥 ,添加生成的 public key 添加到当前账户中。

需要注意: 添加公钥需要验证用户密码

添加账户 SSH 公钥

通过 ssh -T 测试,输出 SSH Key 绑定的用户名

$ ssh -T git@gitee.com
Hi USERNAME! You've successfully authenticated, but GITEE.COM does not provide shell access.

在添加完公钥后,用户可以在 「个人设置」->「安全设置」->「SSH 公钥 浏览查看当前账户已经添加的 SSH 公钥,并对公钥进行管理/删除操作。

浏览 SSH Key

查看/删除 SSH Key

仓库的 SSH Key 和账户 SSH Key 的区别?

账户的 SSH Key 和账户绑定,当账户具有 推送/拉取 权限时可通过 SSH 方式 推送/拉取 的仓库。

通过 ssh -T 测试时,输出 SSH Key 绑定的用户名:

$ ssh -T git@gitee.com
Hi USERNAME! You've successfully authenticated, but GITEE.COM does not provide shell access.

仓库的 SSH key 只针对仓库,且我们仅对仓库提供了部署公钥,即仓库下的公钥仅能拉取仓库,这通常用于生产服务器拉取仓库的代码。

通过 ssh -T 测试时,输出 Anonymous:

ssh -T git@gitee.com
Hi Anonymous! You've successfully authenticated, but GITEE.COM does not provide shell access.