使用 SSH 连接 Git 服务器

关于 SSH

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

创建完 SSH 密钥后,在你的本地计算机上,打开一个终端窗口,并运行以下命令:

ssh -T git@gitea.com

如果你的 SSH 密钥配置正确,这条命令应该能够成功连接到 Gitea。

在这里,git 是一个默认的账号,它是你的 SSH 服务器的登录名,而不是 Gitea 账号或者其他任何名称。gitea.com 是 Gitea 服务器的主机名或 IP 地址。如果你的 Gitea 设置中有相应的 SSH 公钥,这条命令应该能够成功连接到 Gitea。

另外,你还可以使用 SSH 协议操作 Git 其他功能,比如拉取或推送 Git 仓库,或者执行远程命令。例如,要将远程 Git 仓库拉取到本地,你可以运行以下命令:

git clone git@gitea.com:gitea/awesome-gitea.git

在这条命令中,gitea/awesome-gitea.git 是你的远程 Git 仓库的名称。

生成/添加 SSH 公钥

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

1.打开终端

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

$ ssh-keygen -t ed25519 -C "your_email@example.com"

提示: 如果系统不支持较新的 Ed25519 算法,你仍然可以使用 RSA 算法:

$ ssh-keygen -t rsa -b 4096 -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 公钥,通过个人主页 「设置」->「SSH/GPG密钥」->「管理 SSH 密钥」 ,添加生成的 SSH 公钥添加到仓库中。

管理 SSH 密钥

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

ssh -T git@gitea.com

首次使用需要确认并添加主机到本机 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 协议对仓库进行操作了。比如拉取、推送、删除分支。

仓库级别的「部署密钥」和账户级别「SSH密钥」的区别

你可以在仓库主页 「设置」->「部署密钥」->「添加部署密钥」,将 SSH 公钥添加到仓库中。

添加部署密钥

账户级别的「SSH密钥」同时具备推送/拉取的权限,对用户创建/参与的任意仓库均能使用,应用范围广、使用方便。

仓库级别的「部署密钥」只针对当前设置的仓库,且默认情况下不开启推送权限,我们仅仅将部署密钥用作拉取代码,这通常用于 CI/CD 服务器拉取仓库来执行流水线任务。

另外,如果在填写「部署密钥」时勾选了“启用写权限”,将赋予部署密钥推送权限。

测试 SSH 连接

1.打开终端,使用 ssh -T 测试你的服务器

ssh -T git@gitea.com

第一次连接服务器时,你会看到以下提示信息:

The authenticity of host 'gitea.com (IP ADDRESS)' can't be established.
RSA key fingerprint is SHA256:Fo6Tm/SLyse8uglFB1JShqQWchU0kcPzSRueD1O9K0I.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

2.输入 yes 验证 Gitea 服务器指纹

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

如果出现错误,请参考 https://docs.gitea.io/zh-cn/faq/#ssh-issues

git@gitea.com: Permission denied (publickey).

为 Git 客户端配置多个 SSH 私钥

💡 一般而言,在一个设备上使用一个默认密钥对足以,无需为不同服务器生成密钥对。

使用场景

同时拥有多个密钥对,并且为不同的服务器应用了对应的公钥,比如:

  • 一个用于企业 Gitea 实例:~/.ssh/gitea
  • 一个用于个人 GitHub 账号: ~/.ssh/github

解决方法

1.生成一个公司内部专用的 SSH 密钥对

ssh-keygen -t ed25519 -C 'my@company.com' -f ~/.ssh/gitea

2.生成一个GitHub专用的 SSH 密钥对

ssh-keygen -t ed25519 -C 'my@example.com' -f ~/.ssh/github

3.新建一个SSH配置文件 ~/.ssh/config,添加如下内容

其中 HostHostName 填写 git 服务器的域名,IdentityFile 填写对应的私钥的路径

Host gitea.com
    HostName gitea.com
    Port 22
    User git
    IdentityFile ~/.ssh/gitea

Host github.com
    HostName github.com
    Port 22
    User git
    IdentityFile ~/.ssh/github

4.分别测试SSH连通性

ssh -T git@gitea.com
ssh -T git@github.com

更改 SSH 密钥口令

在创建 SSH 密钥时,您可以根据 ssh-keygen 程序的提示设置一个密钥口令,这个口令可以保护您的私钥安全。然而,这个口令并不是必须的,所以在一些网络教程、甚至服务器密钥初始化中都直接跳过口令设置步骤。

设置 SSH 密钥口令可以更好地保护您的私钥安全。如果您的私钥文件被泄露,那么攻击者就可以使用您的私钥进行身份验证,访问您的远程服务器。如果您设置了密钥口令,那么攻击者就需要知道您的密钥口令才能解密您的私钥,进而访问您的远程服务器。因此,设置 SSH 密钥口令可以在一定程度上缓解攻击者滥用您的私钥进行恶意活动。

通过以下命令,您可以更改现有私钥的密码而无需重新生成密钥对:

$ ssh-keygen -p -f ~/.ssh/id_ed25519
> Enter old passphrase: [输入旧密码]
> Key has comment 'your_email@example.com'
> Enter new passphrase (empty for no passphrase): [输入新密码]
> Enter same passphrase again: [重复确认新密码]
> Your identification has been saved with the new passphrase.

使用 ssh-agent 可以避免重复输入私钥密码

ssh-agent 是一个守护进程,它可以管理您的 SSH 私钥,并且自动为您解密私钥。这样,您就不需要每次使用 SSH 命令时都输入私钥口令。但要注意的是 ssh-agent 仅仅将数据保存在内存中,程序重启后历史记录全部丢失,需要重新配置。

要使用 ssh-agent,首先需要启动它。通常,可以在命令行中输入以下命令来启动 ssh-agent

eval "$(ssh-agent -s)"

启动 ssh-agent 之后,您可以使用以下命令将您的 SSH 私钥添加到 ssh-agent 中:

ssh-add ~/.ssh/id_ed25519

您可以将多个私钥添加到 ssh-agent 中,这样就可以使用不同的私钥访问不同的远程服务器。

posted @ 2022-12-09 19:19  Gitea  阅读(6747)  评论(1编辑  收藏  举报