🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# GitLab and SSH keys > 原文:[https://docs.gitlab.com/ee/ssh/README.html](https://docs.gitlab.com/ee/ssh/README.html) * [Requirements](#requirements) * [Options for SSH keys](#options-for-ssh-keys) * [Review existing SSH keys](#review-existing-ssh-keys) * [Generating a new SSH key pair](#generating-a-new-ssh-key-pair) * [ED25519 SSH keys](#ed25519-ssh-keys) * [RSA SSH keys](#rsa-ssh-keys) * [Common steps for generating an SSH key pair](#common-steps-for-generating-an-ssh-key-pair) * [RSA keys and OpenSSH from versions 6.5 to 7.8](#rsa-keys-and-openssh-from-versions-65-to-78) * [Adding an SSH key to your GitLab account](#adding-an-ssh-key-to-your-gitlab-account) * [Testing that everything is set up correctly](#testing-that-everything-is-set-up-correctly) * [Working with non-default SSH key pair paths](#working-with-non-default-ssh-key-pair-paths) * [Per-repository SSH keys](#per-repository-ssh-keys) * [Multiple accounts on a single GitLab instance](#multiple-accounts-on-a-single-gitlab-instance) * [Deploy keys](#deploy-keys) * [Applications](#applications) * [Eclipse](#eclipse) * [SSH on the GitLab server](#ssh-on-the-gitlab-server) * [Options for Microsoft Windows](#options-for-microsoft-windows) * [Troubleshooting](#troubleshooting) # GitLab and SSH keys[](#gitlab-and-ssh-keys "Permalink") Git 是一个分布式版本控制系统,这意味着您可以在本地工作. 此外,您还可以将更改共享或"推送"到其他服务器. GitLab 支持使用 SSH 密钥在 Git 及其服务器之间进行安全通信. SSH 协议提供了这种安全性,并允许您向 GitLab 远程服务器进行身份验证,而无需每次都提供用户名或密码. 该页面可以帮助您配置安全的 SSH 密钥,这些密钥可用于帮助保护与 GitLab 存储库的连接. * 如果您需要有关创建 SSH 密钥的信息,请从我们[的 SSH 密钥选项](#options-for-ssh-keys)开始. * 如果您有专用于 GitLab 帐户的 SSH 密钥,则可能对[使用非默认 SSH 密钥对路径](#working-with-non-default-ssh-key-pair-paths)感兴趣. * 如果您已经有了 SSH 密钥对,则可以转到[将 SSH 密钥添加到 GitLab 帐户的方法](#adding-an-ssh-key-to-your-gitlab-account) . ## Requirements[](#requirements "Permalink") 为了支持 SSH,GitLab 需要安装 OpenSSH 客户端,该客户端已预安装在 GNU / Linux 和 macOS 上,但未预先安装在 Windows 上. 确保您的系统包括 SSH 6.5 或更高版本,因为它不包括现在不安全的 MD5 签名方案. 以下命令返回系统上安装的 SSH 版本: ``` ssh -V ``` 尽管 GitLab 不[支持在 Microsoft Windows 上进行安装](../install/requirements.html#microsoft-windows) ,但是您可以设置 SSH 密钥以将 Windows 设置[为客户端](#options-for-microsoft-windows) . ## Options for SSH keys[](#options-for-ssh-keys "Permalink") GitLab 支持使用 RSA,DSA,ECDSA 和 ED25519 密钥. * GitLab 在 GitLab 11.0 中已[弃用](https://about.gitlab.com/releases/2018/06/22/gitlab-11-0-released/#support-for-dsa-ssh-keys) DSA 密钥. * 如[Go 实用密码术中](https://leanpub.com/gocrypto/read#leanpub-auto-ecdsa)所述,与 DSA 相关的安全性问题也适用于 ECDSA. **提示:**现有文档表明 ED25519 更安全. 如果使用 RSA 密钥,则美国国家科学技术研究院[出版物 800-57 第 3 部分(PDF)](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57Pt3r1.pdf)建议密钥大小至少为 2048 位. 因此,我们的文档集中在 ED25519 和 RSA 密钥的使用上. 管理员可以[限制应允许的密钥及其最小长度](../security/ssh_keys_restrictions.html) . ## Review existing SSH keys[](#review-existing-ssh-keys "Permalink") 如果您已有 SSH 密钥,则可以使用它们来帮助保护与 GitLab 存储库的连接. 默认情况下,Linux 和 macOS 系统上的 SSH 密钥存储在用户的主目录中的`.ssh/`子目录中. 下表包括每种 SSH 密钥算法的默认文件名: | Algorithm | 公钥 | 私钥 | | --- | --- | --- | | ED25519(首选) | `id_ed25519.pub` | `id_ed25519` | | RSA(至少 2048 位密钥大小) | `id_rsa.pub` | `id_rsa` | | DSA(已弃用) | `id_dsa.pub` | `id_dsa` | | ECDSA | `id_ecdsa.pub` | `id_ecdsa` | 有关建议,请参阅[SSH 密钥的选项](#options-for-ssh-keys) . ## Generating a new SSH key pair[](#generating-a-new-ssh-key-pair "Permalink") 如果要创建: * ED25519 密钥,请阅读[ED25519 SSH 密钥](#ed25519-ssh-keys) . * RSA 密钥,请阅读[RSA SSH 密钥](#rsa-ssh-keys) . ### ED25519 SSH keys[](#ed25519-ssh-keys "Permalink") The book [Practical Cryptography With Go](https://leanpub.com/gocrypto/read#leanpub-auto-chapter-5-digital-signatures) suggests that [ED25519](https://ed25519.cr.yp.to/) keys are more secure and performant than RSA keys. 随着 OpenSSH 6.5 在 2014 年引入 ED25519 SSH 密钥,它们应该在任何当前操作系统上都可用. 您可以使用以下命令创建和配置 ED25519 密钥: ``` ssh-keygen -t ed25519 -C "<comment>" ``` `-C`标志(带引号的注释,例如电子邮件地址)是标记 SSH 密钥的可选方法. 您将看到类似于以下内容的响应: ``` Generating public/private ed25519 key pair. Enter file in which to save the key (/home/user/.ssh/id_ed25519): ``` 要获得指导,请继续执行[常见步骤](#common-steps-for-generating-an-ssh-key-pair) . ### RSA SSH keys[](#rsa-ssh-keys "Permalink") 如果您将 RSA 密钥用于 SSH,则美国国家标准技术研究院建议您使用[至少 2048 位](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57Pt3r1.pdf)的密钥大小. 缺省情况下, `ssh-keygen`命令创建一个 1024 位 RSA 密钥. 您可以使用以下命令创建和配置 RSA 密钥,如果需要,可以使用建议的最小密钥大小`2048`代替: ``` ssh-keygen -t rsa -b 2048 -C "email@example.com" ``` `-C`标志(带引号的注释,例如电子邮件地址)是标记 SSH 密钥的可选方法. 您将看到类似于以下内容的响应: ``` Generating public/private rsa key pair. Enter file in which to save the key (/home/user/.ssh/id_rsa): ``` 要获得指导,请继续执行[常见步骤](#common-steps-for-generating-an-ssh-key-pair) . **注意:**如果您具有 7.8 或更低版本的 OpenSSH,请考虑与[编码](#rsa-keys-and-openssh-from-versions-65-to-78)相关的问题. ### Common steps for generating an SSH key pair[](#common-steps-for-generating-an-ssh-key-pair "Permalink") 无论是创建[ED25519](#ed25519-ssh-keys)还是创建[RSA](#rsa-ssh-keys)密钥,您都从`ssh-keygen`命令开始. 此时,您将在命令行中看到以下消息(用于 ED25519 键): ``` Generating public/private ed25519 key pair. Enter file in which to save the key (/home/user/.ssh/id_rsa): ``` 如果您还没有 SSH 密钥对并且没有生成[部署密钥](#deploy-keys) ,请接受建议的文件和目录. 您的 SSH 客户端将使用生成的 SSH 密钥对,而无需其他配置. 或者,您可以将新的 SSH 密钥对保存在其他位置. 您可以分配您选择的目录和文件名. 您还可以将 SSH 密钥对专用于[特定主机](#working-with-non-default-ssh-key-pair-paths) . 分配文件以保存 SSH 密钥后,您将有机会为 SSH 密钥设置[密码](https://www.ssh.com/ssh/passphrase/) : ``` Enter passphrase (empty for no passphrase): Enter same passphrase again: ``` 如果成功,您将看到有关`ssh-keygen`命令将标识和私钥保存在何处的确认. 需要时,可以使用以下命令更新密码: ``` ssh-keygen -p -f /path/to/ssh_key ``` ### RSA keys and OpenSSH from versions 6.5 to 7.8[](#rsa-keys-and-openssh-from-versions-65-to-78 "Permalink") 在 OpenSSH 7.8 之前,RSA 密钥的默认公共密钥指纹基于 MD5,因此不安全. 如果您的 OpenSSH 版本介于 6.5 至 7.8(含)之间,请使用`-o`选项运行`ssh-keygen` ,以更安全的 OpenSSH 格式保存您的私人 SSH 密钥. 如果您已经具有可用于 GitLab 的 RSA SSH 密钥对,请考虑对其进行升级以使用更安全的密码加密格式. 您可以使用以下命令进行操作: ``` ssh-keygen -o -f ~/.ssh/id_rsa ``` 或者,您可以使用以下命令以更安全的加密格式生成新的 RSA 密钥: ``` ssh-keygen -o -t rsa -b 4096 -C "email@example.com" ``` **注意:**如`ssh-keygen`手册页所述,ED25519 已将密钥加密为更安全的 OpenSSH 格式. ## Adding an SSH key to your GitLab account[](#adding-an-ssh-key-to-your-gitlab-account "Permalink") 现在,您可以将创建的 SSH 密钥复制到您的 GitLab 帐户. 这样做,请按照下列步骤操作: 1. 将您的**公共** SSH 密钥复制到以文本格式保存信息的位置. 以下选项将 ED25519 键的信息保存到指定操作系统的剪贴板中: **macOS:** ``` pbcopy < ~/.ssh/id_ed25519.pub ``` **Linux(需要 xclip 软件包):** ``` xclip -sel clip < ~/.ssh/id_ed25519.pub ``` **Windows 上的 Git Bash:** ``` cat ~/.ssh/id_ed25519.pub | clip ``` 如果您使用的是 RSA 密钥,请相应地进行替换. 2. 导航到`http://gitlab.com`并登录. 3. 选择右上角的头像,然后单击**设置** 4. Click **SSH 密钥**. 5. 将复制的公共密钥粘贴到" **密钥"**文本框中. 6. 确保您的密钥在" **标题"**文本框中包含描述性名称,例如" *工作笔记本电脑"*或" *家用工作站"* . 7. 在"过期时间"部分下包含密钥的(可选)过期日期. (在[GitLab 12.9 中](https://gitlab.com/gitlab-org/gitlab/-/issues/36243)引入.) 8. 单击**添加键**按钮. 使用此过程"过期"的 SSH 密钥在 GitLab 工作流程中仍然有效. 由于 GitLab 配置的到期日期不包含在 SSH 密钥本身中,因此您仍然可以根据需要导出公共 SSH 密钥. **注意:**如果您手动复制了公共 SSH 密钥,请确保复制了整个密钥,以`ssh-ed25519` (或`ssh-rsa` )开头,并以您的电子邮件地址结尾. ## Testing that everything is set up correctly[](#testing-that-everything-is-set-up-correctly "Permalink") 要测试是否正确添加了 SSH 密钥,请在终端中运行以下命令(将`gitlab.com`替换为 GitLab 的实例域): ``` 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: ``` The authenticity of host 'gitlab.com (35.231.145.151)' can't be established. ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'gitlab.com' (ECDSA) to the list of known hosts. ``` **注意:**对于 GitLab.com,请查阅[SSH 主机密钥指纹](../user/gitlab_com/index.html#ssh-host-keys-fingerprints)一节,以确保您连接到正确的服务器. 例如,您会在链接的部分中看到上面显示的 ECDSA 密钥指纹. 一旦添加到已知主机列表中,将不再要求您再次验证 GitLab 主机的真实性. 再次运行以上命令,您将只收到*欢迎使用 GitLab 的`@username` !* 信息. 如果未出现欢迎消息,则可以通过使用以下命令在详细模式下运行`ssh`来解决问题: ``` ssh -Tvvv git@gitlab.com ``` ## Working with non-default SSH key pair paths[](#working-with-non-default-ssh-key-pair-paths "Permalink") 如果您为 GitLab SSH 密钥对使用了非默认文件路径,请配置 SSH 客户端以指向 GitLab 私有 SSH 密钥. 要进行这些更改,请运行以下命令: ``` eval $(ssh-agent -s) ssh-add <path to private SSH key> ``` 现在将这些设置保存到`~/.ssh/config`文件中. 这里显示了两个专用于 GitLab 的 SSH 密钥示例: ``` # GitLab.com Host gitlab.com Preferredauthentications publickey IdentityFile ~/.ssh/gitlab_com_rsa # Private GitLab instance Host gitlab.company.com Preferredauthentications publickey IdentityFile ~/.ssh/example_com_rsa ``` 公用 SSH 密钥对于 GitLab 必须是唯一的,因为它们将绑定到您的帐户. SSH 密钥是通过 SSH 推送代码时唯一的标识符,这就是为什么它需要唯一地映射到单个用户的原因. ## Per-repository SSH keys[](#per-repository-ssh-keys "Permalink") 如果要根据正在使用的存储库使用不同的密钥,则可以在存储库中发出以下命令: ``` git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/private-key-filename-for-this-repository -F /dev/null" ``` 这将不使用 SSH 代理,并且至少需要 Git 2.10. ## Multiple accounts on a single GitLab instance[](#multiple-accounts-on-a-single-gitlab-instance "Permalink") [每个存储库](#per-repository-ssh-keys)方法还适用于在单个 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>` . ``` # User1 Account Identity Host <user_1.gitlab.com> Hostname gitlab.com PreferredAuthentications publickey IdentityFile ~/.ssh/<example_ssh_key1> # User2 Account Identity Host <user_2.gitlab.com> Hostname gitlab.com PreferredAuthentications publickey IdentityFile ~/.ssh/<example_ssh_key2> ``` **注意:**为提高效率和透明度,示例`Host`别名定义为`user_1.gitlab.com`和`user_2.gitlab.com` . 高级配置更难以维护. 使用这种别名使使用其他工具(如`git remote`子命令)时更容易理解. SSH 可以将任何字符串理解为`Host`别名,因此`Tanuki1`和`Tanuki2`尽管提供了很少的上下文指向它们,也可以使用. 克隆`gitlab`存储库通常如下所示: ``` git clone git@gitlab.com:gitlab-org/gitlab.git ``` 要为`user_1`克隆它,请将`gitlab.com`替换为 SSH 别名`user_1.gitlab.com` : ``` git clone git@<user_1.gitlab.com>:gitlab-org/gitlab.git ``` 使用`git remote`命令修复以前克隆的存储库. 下面的示例假定远程存储库被别名为`origin` . ``` git remote set-url origin git@<user_1.gitlab.com>:gitlab-org/gitlab.git ``` ## Deploy keys[](#deploy-keys "Permalink") 阅读[有关部署密钥](../user/project/deploy_keys/index.html)的[文档](../user/project/deploy_keys/index.html) . ## Applications[](#applications "Permalink") ### Eclipse[](#eclipse "Permalink") 如果使用的是[EGit](https://www.eclipse.org/egit/) ,则可以[将 SSH 密钥添加到 Eclipse](https://wiki.eclipse.org/EGit/User_Guide#Eclipse_SSH_Configuration) . ## SSH on the GitLab server[](#ssh-on-the-gitlab-server "Permalink") GitLab 与系统安装的 SSH 守护程序集成,指定一个用户(通常名为`git` )来处理所有访问请求. 通过 SSH 连接到 GitLab 服务器的用户由其 SSH 密钥而不是其用户名标识. 在 GitLab 服务器上执行的 SSH *客户端*操作将以该用户身份执行. 尽管可以修改此用户的 SSH 配置,例如提供专用 SSH 密钥来验证这些请求,但是这种做法**不受支持** ,并且强烈建议这样做,因为这样做会带来重大的安全风险. GitLab 检查过程包括对这种情况的检查,如果您的服务器配置如下,它将引导您进入本节,例如: ``` $ gitlab-rake gitlab:check Git user has default SSH configuration? ... no Try fixing it: mkdir ~/gitlab-check-backup-1504540051 sudo mv /var/lib/git/.ssh/id_rsa ~/gitlab-check-backup-1504540051 sudo mv /var/lib/git/.ssh/id_rsa.pub ~/gitlab-check-backup-1504540051 For more information see: doc/ssh/README.md in section "SSH on the GitLab server" Please fix the error above and rerun the checks. ``` 尽快删除自定义配置. 这些自定义项*明确不受支持,*并且可能随时停止工作. ### Options for Microsoft Windows[](#options-for-microsoft-windows "Permalink") 如果您运行的是 Windows 10, [适用于 Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10)的[Windows 子系统(WSL)](https://docs.microsoft.com/en-us/windows/wsl/install-win10)及其最新的[WSL 2](https://docs.microsoft.com/en-us/windows/wsl/install-win10#update-to-wsl-2)版本,则支持安装不同的 Linux 发行版,其中包括 Git 和 SSH 客户端. 对于当前版本的 Windows,您还可以通过[Git for Windows](https://gitforwindows.org)安装 Git 和 SSH 客户端. 替代工具包括: * [Cygwin](https://www.cygwin.com) * [PuttyGen](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html) ## Troubleshooting[](#troubleshooting "Permalink") 如果在 Git 克隆上,系统会提示您输入密码,例如`git@gitlab.com's password:` SSH 设置有问题. * 确保您正确生成了 SSH 密钥对,并将公共 SSH 密钥添加到了 GitLab 配置文件 * 尝试使用`ssh-agent`手动注册您的私有 SSH 密钥,如本文档前面所述 * 尝试通过运行`ssh -Tv git@example.com`调试连接(将 GitLab 域替换为`example.com` )