January 16, 2023

GitHub SSH密钥创建与连接

前文

因为之前都是在笔记本上写这些,最近刚刚换到台式机上,重新与GitHub绑定SSH密钥又成了问题,所以有了今天这个教程来记录,防止之后类似情况出现再次捉急o_O


步骤

  1. 打开Terminal输入以下代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    # 只需要复制这行代码来生成ssh key
    ssh-keygen -t rsa -C "你要连接的GitHub邮件地址"

    # Terminal输出
    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/andy/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase): # 输入GitHub账户的密码,按回车确认。密码不会被显示
    Enter same passphrase again: # 再次输入密码验证,按回车确认。密码不会被显示

    # 密钥已生成,保存在以下位置
    Your identification has been saved in /Users/andy/.ssh/id_rsa
    Your public key has been saved in /Users/andy/.ssh/id_rsa.pub

    # 密钥fingerprint为
    The key fingerprint is:
    SHA256: # fingerprint in SHA256
    The key's randomart image is:
    # randomart image
    +---[RSA 3072]----+
    | # |
    | # |
    | # |
    | # |
    | # |
    | # |
    | # |
    | # |
    | # |
    +----[SHA256]-----+

    密钥在这里就已经生成完毕了,地址在上面所说的地方,以我为例地址为:/Users/andy/.ssh/id_rsa

  2. 去到刚刚生成密钥的路径,可以看到有三个文件

    • id_rsa

    • id_rsa.pub

    • known_hosts

      打开id_rsa.pub文件并复制其中全部内容

  3. 打开GitHub > 点击头像 > Settings > 选择左边SSH and GPG keys > 选择New SSH

  4. Title可以随便填写,但建议填写相关内容方便之后查询,比如”16/01/2023 Macintosh密钥”。Key type选择Authentication Key。

  5. 把刚刚在id_rsa.pub中复制的密钥复制进去到Key部分,点击Add SSH key。


    这样即可完成换电脑后,重新与GitHub绑定密钥的过程。


Reference

About this Post

This post is written by Andy, licensed under CC BY-NC 4.0.

#GitHub