SSH 无密码连接 GitHub,手把手带你配置本地密钥 SSH Keys

本文介绍如何使用SSH密钥连接GitHub,包括检查本地SSH密钥、生成新的SSH密钥、添加SSH密钥到GitHub账户及测试连接等步骤。
📖 官方文档:Connecting to GitHub with SSH

步骤:
🔍 检查本地是否存在 SSH 密钥

在生成 SSH 密钥之前,可以检查本地是否有以前的 SSH 密钥。

  1. 打开 Git Bash。
  2. 输入 ls -al ~/.ssh 查看本地是否存在 SSH 密钥
    $ ls -al ~/.ssh
    # 如果有的话会出现 .ssh 文件夹下的详细 SHH 密钥文件目录
    
  3. 检查目录列表,看看是否已经有一个公共 SSH 密钥。默认情况下,公钥的文件名为以下文件名之一(*.pub):
    • id_rsa.pub
    • id_ecdsa.pub
    • id_ed25519.pub
  4. 如果有现有的公钥和私钥对,并且你想用,那你可以将其添加到 SSH 代理 ,并复制相应的 SSH 公钥粘贴到你的 Github 账户上
  5. 如果没有现有的公钥和私钥对,或者不希望使用现存的密钥来连接到 GitHub,那么生成新的 SSH 密钥,并将其添加到 SSH 代理

返回步骤

🔑 生成新的 SSH 密钥,并将其添加到 SSH 代理

检查过本地的 SSH 密钥之后,可以选择生成一个新的 SSH 密钥,之后将其添加到 SSH 代理。

生成一个 SSH 密钥
  1. 打开 Git Bash。

  2. 复制粘贴下面的命令," ... " 内替换为你 GitHub 绑定的电子邮件地址:

    生成默认名字(id_rsa)的 sshkey 密钥对文件:

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    

    或者你想生成自定义名字(如:git_rsa)的 sshkey:

    ssh-keygen -t rsa -b 4096 -f ~/.ssh/git_rsa -C "your_email@example.com"
    

    这将创建一个新的 ssh 密钥,使用提供的电子邮件作为标签

    > Generating public/private rsa key pair.
    
  3. 当系统提示你 “Enter a file in which to save the key,” 时,请按 Enter 键,使用默认的文件位置:

    这时,如果你忘了自定义名字,仍然想改的话,依然可以在冒号后面输入:~/.ssh/git_rsa,再敲回车

    > Enter a file in which to save the key (~/.ssh/id_rsa):[敲回车]
    
  4. 在以下的提示符后面,输入你的密码(建议全部直接回车):

    建议直接回车,则以后 SSH 连接不需要密码。如果你执意输入密码,想要改回无密码,则参阅最后一步设置无密码 SSH 密钥连接

    > Enter passphrase (empty for no passphrase): [直接回车]
    > Enter same passphrase again: [直接回车]
    
将其添加到 SSH 代理
  1. 手动打开 ssh-agent ,确保它在后台运行:

    eval $(ssh-agent -s)
    > Agent pid 59566
    
  2. 将生成的 SSH 密钥添加到 SSH 代理:

    ssh-add ~/.ssh/id_rsa  # 或者你自定义的名字 git_rsa
    
  3. 如果是个自定义名字的 sshkey(如:git_rsa),而不是默认的 id_rsa,那么需要额外这个步骤:

    ~/.ssh/config 中加入:

    Host github.com git@github.com
        User your_email
        HostName ssh.github.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/git_rsa
        Port 443
    

返回步骤

🔒 将生成的 SSH 公钥粘贴到你的 Github 账户上

为了使用新的(或现有的)SSH 密钥连接到 GitHub ,还需要将其添加到你的 GitHub 帐户中。

  1. 将 SSH 公钥复制到剪贴板:

    clip < ~/.ssh/id_rsa.pub
    

    如果 clip 无效,可以自己找到 C 盘用户目录下的 .ssh 文件夹,在文本编辑器中打开 id_rsa.pub,然后复制里面的内容。

  2. GitHub 页右上角点击你的头像,进入 Settings
    userbar-account-settings

  3. 在侧边栏中选择 SSH and GPG keys
    settings-sidebar-ssh-keys

  4. 单机右上角 New SSH key
    ssh-add-ssh-key1

  5. Title 字段中,给你的新密钥取个名字,再把刚刚复制的公钥的内容粘贴到 Key 框内:
    ssh-key-paste

  6. 点击 Add SSH key
    ssh-add-key

  7. 弹出提示框,输入你的 Github 密码即可:
    sudo_mode_popup

返回步骤

🔔 测试 SSH 密钥连接是否成功

设置好 SSH 密钥并将其添加到 GitHub 帐户后,就可以测试连接了。

  1. 打开 Git Bash。
  2. 输入如下命令:
    ssh -T git@github.com
    
  3. 可能会出现如下的提示:
    > The authenticity of host 'github.com (IP ADDRESS)' can't be established.
    > RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
    > Are you sure you want to continue connecting (yes/no)?
    
    输入 yes 成功连接:
    > Hi username! You've successfully authenticated, but GitHub does not
    > provide shell access.
    
  4. 如果出现拒绝 ”permission denied“,参阅:Error: Permission denied (publickey)

返回步骤

📬 更改本地仓库对应的 remote’s URL

使用 HTTPs 协议的 URL 在对远程存储库进行 git clonegit fetchgit pullgit push 时,每次都要提供用户名和密码。配置好的 SSH 连接则不需要。

  1. 在某个本地仓库中右键点击 Git Bash here
  2. 通过 git remote -v 命令查看对应的远程仓库的 URL:
    $ git remote -v
    > origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
    > origin  https://github.com/USERNAME/REPOSITORY.git (push)
    
  3. 使用 git remote set-url 命令将 remote’s URL 从 HTTPS 更改为 SSH:
    $ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
    
  4. 验证 remote’s URL 是否已更改:
    $ git remote -v
    # Verify new remote URL
    > origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
    > origin  git@github.com:USERNAME/REPOSITORY.git (push)
    

返回步骤

🔨 设置无密码 SSH 密钥连接

你可以配置一个身份验证代理 SSH 密钥(即无密码),这样就不必每次使用 SSH 连接时都重新输入密码。

在 Git Bash 中键入以下命令,你可以更改现有密钥的密码,而无需重新生成密钥:

$ ssh-keygen -p
> Enter file in which the key is (/Users/you/.ssh/id_rsa): [敲回车]
> # 此时会让你输入之前设置的密码
> Key has comment '/Users/you/.ssh/id_rsa'
> Enter new passphrase (empty for no passphrase): [直接回车]
> Enter same passphrase again: [直接回车]
> Your identification has been saved with the new passphrase.

返回步骤

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Skr.B

WUHOOO~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值