Git 使用 SSH 密钥身份验证

Git 使用 SSH 密钥身份验证

在 macOS、Linux 或 Windows 上通过 SSH 连接到 Git 存储库,以使用 HTTPS 身份验证安全连接。 在 Windows 上,我们建议使用 Git 凭据管理器个人访问令牌

❗重要

SSH URL 已更改,但旧的 SSH URL 将继续生效 。 如果已设置 SSH,则应将远程 URL 更新为新格式:

  • 通过在 Git 客户端中运行来 git remote -v 验证哪些远程程序正在使用 SSH。
  • 访问 Web 上的存储库,然后选择右上角的 “克隆 ”按钮。
  • 选择 SSH 并复制新的 SSH URL。
  • 在 Git 客户端中,运行: git remote set-url <remote name, e.g. origin> <new SSH URL> 或者,在 Visual Studio 中,转到 存储库设置并编辑远程。

SSH 密钥身份验证的工作原理

SSH 公钥身份验证适用于生成的加密密钥的非对称对。 公钥与 Azure DevOps 共享,用于验证初始 ssh 连接。 私钥在系统上保持安全。

设置 SSH 密钥身份验证

以下步骤介绍了在以下平台上配置 SSH 密钥身份验证:



使用命令行配置 SSH。 bash 是 Linux 和 macOS 上的常用 shell,Git for Windows 安装会在“开始”菜单中向 Git Bash 添加快捷方式。 其他 shell 环境将正常工作

步骤 1:创建 SSH 密钥

此处的命令将允许创建新的默认 SSH 密钥,覆盖现有的默认密钥。 在继续之前,请检查 ~/.ssh 文件夹 (例如 /home/jamal/.ssh 或 C:\Users\jamal.ssh) 并查找以下文件:

  • id_rsa
  • id_rsa.pub
  • image-20221108213754212

如果这些文件存在,则已创建 SSH 密钥。 可以使用以下命令覆盖密钥,或跳过此步骤

使用ssh-keygen``bash命令提示符创建 SSH 密钥。

ssh-keygen bash

ssh-keygen -t rsa -C “email@.com”

image-20221108213950501

此命令生成 SSH 身份验证所需的两个密钥:私钥 ( id_rsa) 和公钥 ( id_rsa.pub ) 。 请务必从不共享私钥的内容。 如果私钥遭到入侵,攻击者可以使用它欺骗服务器来认为连接来自你。

另请注意,存储在 Azure DevOps 中的 SSH 密钥会在五年后过期。 如果密钥过期,可以上传新密钥或相同的密钥,以继续通过 SSH 访问 Azure DevOps。

步骤 2:将公钥添加到 Azure DevOps Services/TFS

将上一步中生成的公钥与用户 ID 相关联。

  1. 浏览到 Web 门户并选择用户界面右上角的头像,打开安全设置。 在显示的菜单中选择 SSH 公钥

    显示 SSH 公钥菜单项和Azure DevOps Services中选择的用户头像的屏幕截图。

  2. 选择“+ 新建密钥”。

    访问 Azure DevOps Services 中的安全配置

  3. 例如,复制公钥 (的内容,例如,id_rsa.pub) 生成的 公钥数据 字段。

    重要

    避免在密钥数据字段中添加空格或新行,因为它们可能会导致Azure DevOps Services使用无效公钥。 粘贴键时,通常会在末尾添加换行符。 如果发生此换行,请务必删除此换行符。

    ![

    ](https://learn.microsoft.com/zh-cn/azure/devops/repos/git/media/use-ssh-authentication/ssh_key_input.png?view=azure-devops)

  4. 运行以下命令来测试连接: ssh -T git@ssh.dev.azure.com 如果一切正常工作,你将收到一个回复,指出: remote: Shell access is not supported.

image-20221108213045498

步骤 3:使用 SSH 克隆 Git 存储库

  1. 从 Web 门户复制 SSH 克隆 URL。

image-20221108214827135

  1. 通过命令提示符运行 git clone

git clone git@ssh.dev.azure.com:v3/fabrikam-fiber/FabrikamFiber/FabrikamFiber

通过命令提示符运行 git clone

  1. 当系统询问是否要继续连接时,请键入 yes。 Git 将克隆存储库,并设置 origin 远程以使用 SSH 进行连接,以便将来使用 Git 命令进行连接。

image-20221108215123068

$ git clone git@ssh.dev.azure.com:v3/fabrikam-fiber/FabrikamFiber/FabrikamFiber
Cloning into 'FabrikamFiber'...
The authenticity of host 'ssh.dev.azure.com (65.52.8.37)' can't be established.
RSA key fingerprint is SHA256:********************************************
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ssh.dev.azure.com,65.52.8.37' (RSA) to the list of known hosts.
Enter passphrase for key '/c/Users/jamal/.ssh/id_rsa':
remote: Azure Repos
remote: Found 127 objects to send. (50 ms)
Receiving objects: 100% (127/127), 56.67 KiB | 2.58 MiB/s, done.
Resolving deltas: 100% (15/15), done.

问题和故障排除

问:运行 git clone后,我收到以下错误。 应采取何种操作?

复制

Host key verification failed. 
fatal: Could not read from remote repository.

答: 通过运行以下命令手动记录 SSH 密钥: ssh-keyscan -t rsa ssh.dev.azure.com >> ~/.ssh/known_hosts

问:如何让 Git 记住 Windows 上密钥的通行短语?

答: 运行 Git for Windows 中包含的以下命令,在 PowerShell 或 Windows 命令提示符中启动 ssh-agent 进程。 ssh-agent 将缓存通行短语,因此每次连接到存储库时都无需提供密码。

复制

start-ssh-agent.cmd

如果使用 Bash shell (包括 Git Bash) ,请使用以下命令启动 ssh-agent:

复制

eval `ssh-agent`

问:如何验证上传的公钥是否与本地密钥相同?

答: 可以使用命令行通过以下命令针对公钥运行, ssh-keygen 验证使用配置文件中显示的公钥上传的公钥 bash 的指纹。 如果不使用默认值,则需要更改路径和公钥文件名。

复制

ssh-keygen -l -E md5 -f ~/.ssh/id_rsa.pub

然后,可以将 MD5 签名与配置文件中的签名进行比较。 如果在将密钥添加到Azure DevOps Services时,存在连接问题,或者担心在公钥中错误地粘贴到密钥数据字段中,则此检查非常有用。

问:如何在当前正在使用 HTTPS 的存储库中使用 SSH?

答: 需要更新 origin Git 中的远程设备以从 HTTPS 更改为 SSH URL。 获得 SSH 克隆 URL 后,运行以下命令:

复制

git remote set-url origin git@ssh.dev.azure.com:v3/fabrikam-fiber/FabrikamFiber/FabrikamFiber

现在可以运行连接到 origin的任何 Git 命令。

问:如何使用非默认密钥位置,即不是 ~/.ssh/id_rsa 和 ~/.ssh/id_rsa.pub?

答: 若要在与默认值不同的位置使用创建的 ssh-keygen 密钥,请执行以下两项任务:

  1. 键必须位于只能读取或编辑的文件夹中。 如果文件夹具有更广泛的权限,则 SSH 将不会使用密钥。
  2. 必须让 SSH 知道密钥的位置。 通过命令通过 SSH 识别密钥 ssh-add ,从而提供私钥的完整路径。

复制

ssh-add /home/jamal/.ssh/id_jamal.rsa

在 Windows 上,在运行 ssh-add之前,需要从 Git for Windows 中运行以下命令:

复制

start-ssh-agent.cmd

此命令在 PowerShell 和命令提示符中运行。 如果使用 Git Bash,需要使用的命令是:

复制

eval `ssh-agent`

可以在 ssh-add Git for Windows 分发版中找到它,并在 Windows 上的任何 shell 环境中运行它。

在 macOS 和 Linux 上,还必须在运行之前运行ssh-agent``ssh-add,但这些平台上的命令环境通常负责启动ssh-agent

问:我有多个 SSH 密钥。 如何实现对不同的 SSH 服务器或存储库使用不同的 SSH 密钥?

答: 通常,如果为 SSH 客户端配置多个密钥并连接到 SSH 服务器,则客户端可以一次尝试一个密钥,直到服务器接受一个密钥。

但是,这不适用于 Azure DevOps,因为与 SSH 协议相关的技术原因以及 Git SSH URL 的结构。 Azure DevOps 会盲目接受客户端在身份验证期间提供的第一个密钥。 如果请求的存储库的密钥无效,则请求将失败并出现以下错误:

复制

remote: Public key authentication failed.
fatal: Could not read from remote repository.

对于 Azure DevOps,需要将 SSH 配置为显式使用特定的密钥文件。 例如,执行此操作以编辑 ~/.ssh/config 文件 (或 /home/jamal/.ssh``C:\Users\jamal\.ssh) ,如下所示:

复制

# The settings in each Host section are applied to any Git SSH remote URL with a
# matching hostname.
# Generally:
# * SSH uses the first matching line for each parameter name, e.g. if there's
#   multiple values for a parameter across multiple matching Host sections
# * "IdentitiesOnly yes" prevents keys cached in ssh-agent from being tried before
#   the IdentityFile values we explicitly set.
# * On Windows, ~/.ssh/your_private_key maps to %USERPROFILE%\.ssh\your_private_key,
#   e.g. C:\Users\<username>\.ssh\your_private_key.

# Most common scenario: to use the same key across all hosted Azure DevOps
# organizations, add a Host entry like this:
Host ssh.dev.azure.com
  IdentityFile ~/.ssh/your_private_key
  IdentitiesOnly yes

# This model will also work if you still use the older SSH URLs with a
# hostname of vs-ssh.visualstudio.com:
Host vs-ssh.visualstudio.com
  IdentityFile ~/.ssh/your_private_key
  IdentitiesOnly yes

# OpenSSL 8.7 has DEPRECATED RSA. IF using OpenSSL version > 8.6 you will need to 
# add the 'HostkeyAlgorithms' and 'PubkeyAcceptedAlgorithms' entries below. You can 
# check the version of OpenSSL/OpenSSH you're using by running the command 'ssh -v localhost'    
Host ssh.dev.azure.com
  IdentityFile ~/.ssh/id_rsa
  HostkeyAlgorithms +ssh-rsa
  PubkeyAcceptedAlgorithms +ssh-rsa   

# Less common scenario: if you need different keys for different organizations,
# you'll need to use host aliases to create separate Host sections.
# This is because all hosted Azure DevOps URLs have the same hostname
# (ssh.dev.azure.com), so SSH has no way to distinguish them by default.
#
# Imagine that we have the following two SSH URLs:
# * git@ssh.dev.azure.com:v3/Fabrikam/Project1/fab_repo
#   * For this, we want to use `fabrikamkey`, so we'll create `devops_fabrikam` as
#     a Host alias and tell SSH to use `fabrikamkey`.
# * git@ssh.dev.azure.com:v3/Contoso/Project2/con_repo
#   * For this, we want to use `contosokey`, so we'll create `devops_contoso` as
#     a Host alias and tell SSH to use `contosokey`.
#
# To set explicit keys for the two host aliases and to tell SSH to use the correct
# actual hostname, add the next two Host sections:
Host devops_fabrikam
  HostName ssh.dev.azure.com
  IdentityFile ~/.ssh/private_key_for_fabrikam
  IdentitiesOnly yes
Host devops_contoso
  HostName ssh.dev.azure.com
  IdentityFile ~/.ssh/private_key_for_contoso
  IdentitiesOnly yes
#
# Then, instead of using the real URLs, tell Git you want to use these URLs:
# * git@devops_fabrikam:v3/Fabrikam/Project1/fab_repo
# * git@devops_contoso:v3/Contoso/Project2/con_repo
#

# At the end of the file, you can put global defaults for other SSH hosts you
# may connect to.  Note that "*" also matches any hosts that match the sections
# above, and remember that SSH uses the first matching line for each parameter name.
Host *
参考文章:

使用 SSH 密钥身份验证

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值