Git 使用 SSH 密钥身份验证

这篇具有很好参考价值的文章主要介绍了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 密钥身份验证:


  • Linux
  • 至少运行 Leopard (10.5) 的 macOS
  • 运行适用于 Windows 的 Git 的 Windows 系统

使用命令行配置 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
  • Git 使用 SSH 密钥身份验证

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

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

ssh-keygen bash

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

Git 使用 SSH 密钥身份验证

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

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

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

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

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

    Git 使用 SSH 密钥身份验证

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

    Git 使用 SSH 密钥身份验证

  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.

Git 使用 SSH 密钥身份验证

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

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

Git 使用 SSH 密钥身份验证

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

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

通过命令提示符运行 git clone

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

Git 使用 SSH 密钥身份验证

$ 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 密钥身份验证文章来源地址https://www.toymoban.com/news/detail-403869.html

到了这里,关于Git 使用 SSH 密钥身份验证的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • GitHub SSH 身份验证原理

    HTTPS和SSH是两种不同的协议,用于连接到远程Git仓库。它们的主要区别在于身份验证方式和连接方式: HTTPS:使用用户名和密码或者个人访问令牌(Personal Access Token)进行身份验证。每次推送或拉取代码时,都需要输入密码或令牌(除非你配置了密码缓存)。HTTPS通常在所有系

    2024年01月23日
    浏览(31)
  • git生成ssh密钥详细步骤 git如何生成ssh密钥

    首先右键点击电脑桌面,点击选择\\\"Git Bash Here\\\",打开git命令窗口; 2. 在git命令窗口配置用户,输入命令:git config --global user.name “blkj”。其中“blkj”是你自己要填的用户名; 3. 接着进行邮箱配置,输入命令:git config --global user.email “blkj@boranet.com.cn”。\\\"blkj@boranet.com.cn\\\"就是填

    2024年02月11日
    浏览(53)
  • Git 配置SSH密钥

    一、设置账号和邮箱 二、生成密钥 三、保存密钥 最后 复制生成的密钥,添加到码云对应的SSH Keys处即可

    2024年02月16日
    浏览(35)
  • Git的SSH密钥配置

    使用Git有一段时间了,之前也分享过一些相关文章记录,但一直都想对初始设置做一篇记录,毕竟 push 和 pull 操作经常使用,也都熟能生巧了。但初始设置用的次数一般都很少,忘记再查也是很麻烦,这里就根据我自己的经验和之前查到的资料做一个简单记录,希望对初学者

    2024年02月04日
    浏览(42)
  • 配置Git的SSH密钥

    要配置Git的SSH密钥,请按照以下步骤进行: 打开终端并运行以下命令生成密钥: 其中, your_email@example.com  是您的电子邮件地址。 稍后会提示您输入文件名和密码短语。您可以按Enter键接受默认值,或按需要自定义它们。 在终端中运行以下命令,将新生成的SSH密钥添加到s

    2024年02月12日
    浏览(44)
  • Ubuntu git SSH密钥添加

    本文介绍Ubuntu git SSH密钥添加方法。 1.生成密钥的命令 密钥是Ubuntu和github或者gitee的联系的纽带,用于验证。 ssh-keygen -C “10xxxxxxxx@qq.com” -t rsa 格式:ssh-keygen -C “git或者gitee账户绑定的邮箱” -t rsa 2.查看密钥 密钥在~/.ssh目录下的id_rsa.pub文件中, cat ~/.ssh/id_rsa.pub 3.往git仓库

    2024年02月13日
    浏览(82)
  • Git SSH密钥生成与删除

    SSH(安全外壳协议)密钥是远程服务器身份验证的常用方法。它们比用户名和密码更安全,因为它们提供了加密的身份验证机制。以下是如何生成和删除 SSH 密钥的步骤。 生成 SSH 密钥 启动终端 :打开您的计算机上的终端应用程序。 生成新密钥 :输入以下命令,其中的电子

    2024年02月11日
    浏览(34)
  • 【GIT】GitHub添加ssh密钥

    在想要克隆GitHub项目到本地时,通常开源通过https和ssh两种方式,由于GitHub的服务器步数在海外,https连接受网络波动的影响较大,经常会出现连接中断的情况,相比之下,用ssh方式进行克隆显得更加方便和高效。 GitHub 的 SSH 密钥用于对你的 GitHub 账号进行身份验证,以便你能

    2024年02月12日
    浏览(73)
  • git生成ssh密钥详细步骤

    首先右键点击电脑桌面,点击“git bash here”,打开git命令窗口 如果git用户名和邮箱等已经完成配置,则跳过此步骤,直接操作第3条;假如没有配置,继续如下操作: (1)、在命令窗口配置用户,输入命令:git config --global user.name \\\"blalalala\\\"。其中” blalalala”是你自己的用户名;

    2023年04月21日
    浏览(73)
  • Git 本地配置SSH Key(密钥)

    Git配置 打开Git Bash 配置你的用户名和邮箱 git config --global user.name “zhangsan” git config --global user.email “zhangsan@xxxx.com” 生成SSH key ssh-keygen -t rsa -C “zhangsan@xxxx.com” 获取SSH Key 根据命令行提示,获取以ssh-rsa的字符串(包括ssh-rsa) 按键”回车键“ 生成了SSH Key(密钥)如下图所示

    2024年04月27日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包