使用 SSH 连接 Git 服务器
刘清 edytuje tę stronę 2 miesięcy temu

关于 SSH

SSH (Secure Shell) 是一种安全的远程登录协议,可以让你通过安全的加密连接进行远程登录。目前,Mac、Windows 10、Linux 系统均有内置 OpenSSH 客户端。如果你想通过 SSH 连接到 Gitea,首先需要创建一对 SSH 密钥(即公钥和私钥),然后将公钥上传到 Gitea。

生成/添加 SSH 公钥

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

1.打开本地终端,即编码的终端。

2.你可以按如下命令来生成 SSH 密钥对。其中的-C "your_email@example.com" 实际上是一条注释信息,可以填写成你的邮箱地址,并不影响生成密钥的过程。

ssh-keygen -t ed25519 -C "your_email@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/ferris/.ssh/id_ed25519):[回车,使用默认路径]
Created directory '/home/ferris/.ssh'.
Enter passphrase (empty for no passphrase):[可输入密码]
Enter same passphrase again:[再次输入密码]
Your identification has been saved in /home/ferris/.ssh/id_ed25519
Your public key has been saved in /home/ferris/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:BjlFiHdNt3tI95XnKimBpUV2mhBinfm0jOL9JeG+YDg your_email@example.com
The key's randomart image is:
+--[ED25519 256]--+
|     .o+=*+ o    |
|    ..o+=+o= .  .|
|     .+. ==.o ..o|
|      .o.==. + oo|
|     . oS...o . o|
|      .o. o..o . |
|      E oo.oo .  |
|       o .o. .   |
|          ..     |
+----[SHA256]-----+

3.按照上述 ssh-keygen 的三次提示分别输入回车、密码、确认密码即可生成 SSH 密钥对。通过查看 ~/.ssh/id_ed25519.pub 文件内容,获取到你的 SSH 公钥。请注意:私钥是 ~/.ssh/id_ed25519。保护好私钥,不要将私钥泄露到网络上。

cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJZ1HF2aEd61o7/EJ350EbXfOksjjNGdB66CwGVRco1g your_email@example.com

4.复制生成的 SSH 公钥,登录 gitea 通过个人主页 「用户设置」->「SSH/GPG密钥」->「管理 SSH 密钥」 ,添加生成的 SSH 公钥添加进去。

添加公钥

添加后,在终端(Terminal)中输入

ssh -T git@afanai.top

首次使用需要确认并添加主机到本机 SSH 可信列表。若返回 Hi there, USERNAME! You've successfully authenticated... 内容,则证明添加成功。

Hi there, USERNAME! You've successfully authenticated with the key named id_ed25519, but Gitea does not provide shell access.

添加成功后,就可以使用 SSH 协议对仓库进行操作了。比如拉取、推送、删除分支。