使用 SSH 连接到 GitHub

官方文档地址: https://docs.github.com/cn/free-pro-team@latest/github/authenticating-to-github/connecting-to-github-with-ssh

1 检查现有 SSH 密钥

  1. 用管理员权限(防止出现什么问题), 打开 Git Bash。

  2. 输入 ls -al ~/.ssh 以查看是否存在现有 SSH 密钥:

    XXX@MY-PC MINGW64 /d/github/ssh (main)
    $ ls -al ~/.ssh
    ls: cannot access '/c/Users/XXX/.ssh': No such file or directory
    
    

    以上为不存的的输出

    以下为存在的情况下的输出

    XXX@MY-PC MINGW64 /d/github/ssh (main)
    $ ls -al ~/.ssh
    total 26
    drwxr-xr-x 1 XXX 197121   0 11月 14 15:53 ./
    drwxr-xr-x 1 XXX 197121   0 11月 14 15:55 ../
    -rw-r--r-- 1 XXX 197121 411 11月 14 16:12 id_ed25519
    -rw-r--r-- 1 XXX 197121  98 11月 14 15:25 id_ed25519.pub
    
    

    注意:

    如果存在的话, 公钥的文件名肯定会有其中的一种

    • id_rsa.pub

    • id_ecdsa.pub

    • id_ed25519.pub

2 生成新 SSH 密钥

如果以上检查时, 存在公钥的话, 就不用生成新的了

  1. 通过命令 *** ssh-keygen -t ed25519 -C “你的github登录邮箱” *** 生成公钥
XXX@MY-PC MINGW64 /d/github/ssh (main)
$ ssh-keygen -t ed25519 -C "770268977@qq.com"

  1. 提示您 *** Enter a file in which to save the key(/c/Users/XXX/.ssh/id_ed25519)***时,

    按 Enter 键。

  2. 在提示时输入安全密码。

    *** 如果这里输入了密码, 每次提交时都需要输入这个密码 ***

    *** 如果不想每次都输入密码时, 则什么都不要输入, 直接回车***

    Enter passphrase (empty for no passphrase):
    

  3. 完成后输出

    Generating public/private ed25519 key pair.
    Enter file in which to save the key (/c/Users/XXX/.ssh/id_ed25519):
    Created directory '/c/Users/XXX/.ssh'.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /c/Users/XXX/.ssh/id_ed25519.
    Your public key has been saved in /c/Users/XXX/.ssh/id_ed25519.pub.
    The key fingerprint is:
    SHA256:DofI+ynJZCHm8XWwmBaPRsvoTZj+Es+zWpg5zds23a4 770268977@qq.com
    The key's randomart image is:
    +--[ED25519 256]--+
    |                 |
    |    o .          |
    |   * B o         |
    |  B.@.+..        |
    | = Oooo.S        |
    |  =*=. +         |
    |  =O=. ...       |
    |  .oO+o.. .      |
    |  .oo=+.Eo.      |
    +----[SHA256]-----+
    

    3 将 SSH 密钥添加到 ssh-agent

    如果已安装GitHub的客户端, 文档上说无需SSH密钥, 我没试过

    接下来继续无客户端的情况

    1. 启动 ssh-agent 正在运行

      XXX@MY-PC MINGW64 /d/github/ssh (main)
      $ eval $(ssh-agent -s)
      Agent pid 6748
      
      

    2. 将 SSH 私钥添加到 ssh-agent

      ssh-add ~/.ssh/你自己的名称

      *** 如果你的密钥名称不是id_ed25519, 则换成你自己的名称 ***

      XXX@MY-PC MINGW64 ~
      $ ssh-add ~/.ssh/id_ed25519
      Enter passphrase for /c/Users/XXX/.ssh/id_ed25519:
      Identity added: /c/Users/XXX/.ssh/id_ed25519 (770268977@qq.com)
      
      

4 新增 SSH 密钥到 GitHub 帐户

  1. 复制密钥到剪贴板

    XXX@MY-PC MINGW64 ~
    $ clip < ~/.ssh/id_ed25519.pub
    

    也可以用记事本打开/c/Users/XXX/.ssh/id_ed25519.pub文件, 直接复制

    注意:

    一定是 *** .pub *** 后缀的文件

  2. 在浏览器中登录上你的github, 打开https://github.com/settings/keys 链接, 设置 SSH keys

或者

从从右上角个人头像下拉菜单 > Settings > SSH and GPG keys

打开对应页面

  1. 点击 New SSH key 或者 Add SSH key
  2. 在 TItle 输入框中输入一个你自己的标题, 随便输入建议是英文, 如 “my MacBook Git key”
  3. 在 key 输入框中, 粘贴刚刚复制出来的密钥
  4. 按提示步骤, 确认完成就可以了

5 测试 SSH 连接

  1. 输入 *** ssh -T git@github.com *** 测试连接是否正确

    XXX@MY-PC MINGW64 ~
    $ ssh -T git@github.com
    The authenticity of host 'github.com (13.229.188.59)' can't be established.
    RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
    Hi shijue98! You've successfully authenticated, but GitHub does not provide shell access.
    
    
  2. 当看到提示 *** Are you sure you want to continue connecting (yes/no)? *** 输入 *** yes *** 即可

  3. 通过后, 会像上面一样提示信息

6 去除 passphrase 安全密码

当你提示输入安全密码时, 输入了安全密码, 每次提交时都需要输入密码, 如何去除呢?

Enter passphrase (empty for no passphrase): 输入过密码了

用ssh-keygen -p 去除

XXX@MY-PC MINGW64 ~
$ ssh-keygen -p
Enter file in which the key is (/c/Users/XXX/.ssh/id_rsa): /c/Users/XXX/.ssh/id_ed25519
Enter old passphrase:
Key has comment '770268977@qq.com'
Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.

注意:

  1. 提示 *** Enter file in which the key is (/c/Users/XXX/.ssh/id_rsa): *** 时

    默认是 /c/Users/XXX/.ssh/id_rsa 地址, 而开始创建的名字不是*** id_rsa ***

    则需要输入自己的地址 如: *** /c/Users/XXX/.ssh/id_ed25519 ***

  2. 提示 *** Enter new passphrase (empty for no passphrase): *** 时,

不要输入任何字符了,

直接回车,

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐