GitHub配置SSH key

Mac重装了系统,本地没有SSHKey了,所以把配置SSHKey的步骤记录下来,方便以后查看,以下内容适用于Mac用户

  • 打开终端

  • 粘贴以下命令,替换邮箱地址为你的GitHub注册的的邮箱地址

    1
    $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • 然后就会在本机生产一对密钥对

    1
    $ Generating public/private rsa key pair.
  • 然后按回车把密钥保存在本机

    1
    Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
  • 然后输入两遍密码,这个密码用来加密私钥,也就是以后使用私钥的时候要输这个密码

    1
    2
    Enter passphrase (empty for no passphrase): [Type a passphrase]
    Enter same passphrase again: [Type passphrase again]
  • 打开SSH密钥所在的目录

    1
    $ cd ~/.ssh
  • 用vim 打开目录里面的公钥:id_rsa.pub,并把里面的内容复制出来

  • 登陆GitHub进入设置,右边选择 SSH and GPG keys 选择New SSH Key,设置一个title,把上面公钥复制的内容粘贴到Key

  • 使用以下命令测试SSH密钥是否添加成功,我的Mac是 macOS 10.12.4,可能会出现以下提示,并且需要输入上面加密私钥的密码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $ ssh -T git@github.com
    The authenticity of host 'github.com (192.30.252.1)' 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)?
    The authenticity of host 'github.com (192.30.252.1)' can't be established.
    RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
    Are you sure you want to continue connecting (yes/no)?
    Enter passphrase for key '/User/yourmacusername/.ssh/id_rsa':
    Hi username! You've successfully authenticated, but GitHub does not
    provide shell access.
  • 看到上面的提示内容就可以在GitHub上提交代码了

提示:如果终端每次 Git 提交,都要输入密码使用私钥,可以在终端输入以下命令

1
echo -e "AddKeysToAgent yes\nUseKeychain yes" >> ~/.ssh/config

参考文章:Generating a new SSH key and adding it to the ssh-agent

本人刚开始写博客,主要是为了给自己的知识点做一个笔记,方便自己以后查阅,如果能让别人有所启发也是荣幸之至!如有错误,欢迎指正!