GitLab and SSH keys

GitLab and SSH keys

Git 是一个分布式版本控制系统,这意味着您可以在本地工作. 此外,您还可以将更改共享或”推送”到其他服务器. GitLab 支持使用 SSH 密钥在 Git 及其服务器之间进行安全通信.

SSH 协议提供了这种安全性,并允许您向 GitLab 远程服务器进行身份验证,而无需每次都提供用户名或密码.

该页面可以帮助您配置安全的 SSH 密钥,这些密钥可用于帮助保护与 GitLab 存储库的连接.

  • 如果您需要有关创建 SSH 密钥的信息,请从我们开始.
  • 如果您有专用于 GitLab 帐户的 SSH 密钥,则可能对使用非默认 SSH 密钥对路径感兴趣.
  • 如果您已经有了 SSH 密钥对,则可以转到 .

为了支持 SSH,GitLab 需要安装 OpenSSH 客户端,该客户端已预安装在 GNU / Linux 和 macOS 上,但未预先安装在 Windows 上.

确保您的系统包括 SSH 6.5 或更高版本,因为它不包括现在不安全的 MD5 签名方案. 以下命令返回系统上安装的 SSH 版本:

尽管 GitLab 不支持在 Microsoft Windows 上进行安装 ,但是您可以设置 SSH 密钥以将 Windows 设置 .

Options for SSH keys

GitLab 支持使用 RSA,DSA,ECDSA 和 ED25519 密钥.

  • GitLab 在 GitLab 11.0 中已 DSA 密钥.
  • Go 实用密码术中所述,与 DSA 相关的安全性问题也适用于 ECDSA.

提示:现有文档表明 ED25519 更安全. 如果使用 RSA 密钥,则美国国家科学技术研究院建议密钥大小至少为 2048 位.

因此,我们的文档集中在 ED25519 和 RSA 密钥的使用上.

管理员可以限制应允许的密钥及其最小长度 .

Review existing SSH keys

如果您已有 SSH 密钥,则可以使用它们来帮助保护与 GitLab 存储库的连接. 默认情况下,Linux 和 macOS 系统上的 SSH 密钥存储在用户的主目录中的子目录中. 下表包括每种 SSH 密钥算法的默认文件名:

有关建议,请参阅SSH 密钥的选项 .

Generating a new SSH key pair

如果要创建:

The book Practical Cryptography With Go suggests that keys are more secure and performant than RSA keys.

随着 OpenSSH 6.5 在 2014 年引入 ED25519 SSH 密钥,它们应该在任何当前操作系统上都可用.

您可以使用以下命令创建和配置 ED25519 密钥:

  1. ssh-keygen -t ed25519 -C "<comment>"

-C标志(带引号的注释,例如电子邮件地址)是标记 SSH 密钥的可选方法.

您将看到类似于以下内容的响应:

  1. Generating public/private ed25519 key pair.
  2. Enter file in which to save the key (/home/user/.ssh/id_ed25519):

要获得指导,请继续执行常见步骤 .

RSA SSH keys

如果您将 RSA 密钥用于 SSH,则美国国家标准技术研究院建议您使用至少 2048 位的密钥大小. 缺省情况下, ssh-keygen命令创建一个 1024 位 RSA 密钥.

您可以使用以下命令创建和配置 RSA 密钥,如果需要,可以使用建议的最小密钥大小2048代替:

  1. ssh-keygen -t rsa -b 2048 -C "email@example.com"

-C标志(带引号的注释,例如电子邮件地址)是标记 SSH 密钥的可选方法.

您将看到类似于以下内容的响应:

  1. Generating public/private rsa key pair.
  2. Enter file in which to save the key (/home/user/.ssh/id_rsa):

要获得指导,请继续执行 .

无论是创建ED25519还是创建密钥,您都从ssh-keygen命令开始. 此时,您将在命令行中看到以下消息(用于 ED25519 键):

  1. Generating public/private ed25519 key pair.
  2. Enter file in which to save the key (/home/user/.ssh/id_rsa):

如果您还没有 SSH 密钥对并且没有生成部署密钥 ,请接受建议的文件和目录. 您的 SSH 客户端将使用生成的 SSH 密钥对,而无需其他配置.

或者,您可以将新的 SSH 密钥对保存在其他位置. 您可以分配您选择的目录和文件名. 您还可以将 SSH 密钥对专用于 .

分配文件以保存 SSH 密钥后,您将有机会为 SSH 密钥设置密码

  1. Enter passphrase (empty for no passphrase):
  2. Enter same passphrase again:

如果成功,您将看到有关ssh-keygen命令将标识和私钥保存在何处的确认.

需要时,可以使用以下命令更新密码:

  1. ssh-keygen -p -f /path/to/ssh_key

RSA keys and OpenSSH from versions 6.5 to 7.8

在 OpenSSH 7.8 之前,RSA 密钥的默认公共密钥指纹基于 MD5,因此不安全.

如果您的 OpenSSH 版本介于 6.5 至 7.8(含)之间,请使用-o选项运行ssh-keygen ,以更安全的 OpenSSH 格式保存您的私人 SSH 密钥.

如果您已经具有可用于 GitLab 的 RSA SSH 密钥对,请考虑对其进行升级以使用更安全的密码加密格式. 您可以使用以下命令进行操作:

或者,您可以使用以下命令以更安全的加密格式生成新的 RSA 密钥:

  1. ssh-keygen -o -t rsa -b 4096 -C "email@example.com"

注意:ssh-keygen手册页所述,ED25519 已将密钥加密为更安全的 OpenSSH 格式.

现在,您可以将创建的 SSH 密钥复制到您的 GitLab 帐户. 这样做,请按照下列步骤操作:

  1. 将您的公共 SSH 密钥复制到以文本格式保存信息的位置. 以下选项将 ED25519 键的信息保存到指定操作系统的剪贴板中:

    macOS:

    1. pbcopy < ~/.ssh/id_ed25519.pub

    Linux(需要 xclip 软件包):

    1. xclip -sel clip < ~/.ssh/id_ed25519.pub

    Windows 上的 Git Bash:

    1. cat ~/.ssh/id_ed25519.pub | clip

    如果您使用的是 RSA 密钥,请相应地进行替换.

  2. 导航到http://gitlab.com并登录.

  3. 选择右上角的头像,然后单击设置
  4. Click SSH 密钥.
  5. 将复制的公共密钥粘贴到” 密钥”文本框中.
  6. 确保您的密钥在” 标题”文本框中包含描述性名称,例如” 工作笔记本电脑”或” 家用工作站” .
  7. 在”过期时间”部分下包含密钥的(可选)过期日期. (在GitLab 12.9 中引入.)
  8. 单击添加键按钮.

使用此过程”过期”的 SSH 密钥在 GitLab 工作流程中仍然有效. 由于 GitLab 配置的到期日期不包含在 SSH 密钥本身中,因此您仍然可以根据需要导出公共 SSH 密钥.

注意:如果您手动复制了公共 SSH 密钥,请确保复制了整个密钥,以ssh-ed25519 (或 )开头,并以您的电子邮件地址结尾.

Testing that everything is set up correctly

要测试是否正确添加了 SSH 密钥,请在终端中运行以下命令(将gitlab.com替换为 GitLab 的实例域):

  1. ssh -T git@gitlab.com

The first time you connect to GitLab via SSH, you will be asked to verify the authenticity of the GitLab host that you’re connecting to. For example, when connecting to GitLab.com, answer yes to add GitLab.com to the list of trusted hosts:

  1. The authenticity of host 'gitlab.com (35.231.145.151)' can't be established.
  2. ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.
  3. Are you sure you want to continue connecting (yes/no)? yes

注意:对于 GitLab.com,请查阅SSH 主机密钥指纹一节,以确保您连接到正确的服务器. 例如,您会在链接的部分中看到上面显示的 ECDSA 密钥指纹.

一旦添加到已知主机列表中,将不再要求您再次验证 GitLab 主机的真实性. 再次运行以上命令,您将只收到欢迎使用 GitLab 的@username 信息.

  1. ssh -Tvvv git@gitlab.com

Working with non-default SSH key pair paths

如果您为 GitLab SSH 密钥对使用了非默认文件路径,请配置 SSH 客户端以指向 GitLab 私有 SSH 密钥.

要进行这些更改,请运行以下命令:

现在将这些设置保存到~/.ssh/config文件中. 这里显示了两个专用于 GitLab 的 SSH 密钥示例:

  1. # GitLab.com Host gitlab.com
  2. Preferredauthentications publickey
  3. IdentityFile ~/.ssh/gitlab_com_rsa
  4. # Private GitLab instance Host gitlab.company.com
  5. Preferredauthentications publickey
  6. IdentityFile ~/.ssh/example_com_rsa

公用 SSH 密钥对于 GitLab 必须是唯一的,因为它们将绑定到您的帐户. SSH 密钥是通过 SSH 推送代码时唯一的标识符,这就是为什么它需要唯一地映射到单个用户的原因.

Per-repository SSH keys

如果要根据正在使用的存储库使用不同的密钥,则可以在存储库中发出以下命令:

  1. git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/private-key-filename-for-this-repository -F /dev/null"

这将不使用 SSH 代理,并且至少需要 Git 2.10.

方法还适用于在单个 GitLab 实例中使用多个帐户.

或者,可以直接在~.ssh/config为主机分配别名. 如果在.ssh/config中的Host块之外设置了IdentityFile ,则 SSH 和作为扩展的 Git 将无法登录. 这是由于 SSH 组装IdentityFile条目的方式,因此不能通过将IdentitiesOnly设置为yes来更改. IdentityFile条目应指向 SSH 密钥对的私钥.

注意:私钥和公用密钥应仅由用户读取. 通过运行以下chmod 0400 ~/.ssh/<example_ssh_key>在 Linux 和 macOS 上完成此操作: chmod 0400 ~/.ssh/<example_ssh_key>chmod 0400 ~/.ssh/<example_sh_key.pub> .

  1. # User1 Account Identity Host <user_1.gitlab.com>
  2. Hostname gitlab.com
  3. PreferredAuthentications publickey
  4. IdentityFile ~/.ssh/<example_ssh_key1>
  5. # User2 Account Identity Host <user_2.gitlab.com>
  6. Hostname gitlab.com
  7. PreferredAuthentications publickey
  8. IdentityFile ~/.ssh/<example_ssh_key2>

注意:为提高效率和透明度,示例Host别名定义为user_1.gitlab.comuser_2.gitlab.com . 高级配置更难以维护. 使用这种别名使使用其他工具(如git remote子命令)时更容易理解. SSH 可以将任何字符串理解为Host别名,因此Tanuki1Tanuki2尽管提供了很少的上下文指向它们,也可以使用.

克隆gitlab存储库通常如下所示:

  1. git clone git@gitlab.com:gitlab-org/gitlab.git

要为user_1克隆它,请将gitlab.com替换为 SSH 别名user_1.gitlab.com

  1. git clone git@<user_1.gitlab.com>:gitlab-org/gitlab.git

使用git remote命令修复以前克隆的存储库.

下面的示例假定远程存储库被别名为origin .

  1. git remote set-url origin git@<user_1.gitlab.com>:gitlab-org/gitlab.git

Deploy keys

阅读的文档 .

Applications

如果使用的是EGit ,则可以 .

SSH on the GitLab server

GitLab 与系统安装的 SSH 守护程序集成,指定一个用户(通常名为git )来处理所有访问请求. 通过 SSH 连接到 GitLab 服务器的用户由其 SSH 密钥而不是其用户名标识.

在 GitLab 服务器上执行的 SSH 客户端操作将以该用户身份执行. 尽管可以修改此用户的 SSH 配置,例如提供专用 SSH 密钥来验证这些请求,但是这种做法不受支持 ,并且强烈建议这样做,因为这样做会带来重大的安全风险.

GitLab 检查过程包括对这种情况的检查,如果您的服务器配置如下,它将引导您进入本节,例如:

  1. $ gitlab-rake gitlab:check
  2. Git user has default SSH configuration? ... no
  3. Try fixing it:
  4. mkdir ~/gitlab-check-backup-1504540051
  5. sudo mv /var/lib/git/.ssh/id_rsa ~/gitlab-check-backup-1504540051
  6. sudo mv /var/lib/git/.ssh/id_rsa.pub ~/gitlab-check-backup-1504540051
  7. For more information see:
  8. doc/ssh/README.md in section "SSH on the GitLab server"
  9. Please fix the error above and rerun the checks.

尽快删除自定义配置. 这些自定义项明确不受支持,并且可能随时停止工作.

Options for Microsoft Windows

如果您运行的是 Windows 10, 适用于 Linux的及其最新的WSL 2版本,则支持安装不同的 Linux 发行版,其中包括 Git 和 SSH 客户端.

对于当前版本的 Windows,您还可以通过安装 Git 和 SSH 客户端.

替代工具包括:

如果在 Git 克隆上,系统会提示您输入密码,例如git@gitlab.com's password: SSH 设置有问题.

  • 确保您正确生成了 SSH 密钥对,并将公共 SSH 密钥添加到了 GitLab 配置文件
  • 尝试使用ssh-agent手动注册您的私有 SSH 密钥,如本文档前面所述
  • 尝试通过运行ssh -Tv git@example.com调试连接(将 GitLab 域替换为 )