通过SSH访问Github设置

使用SSH密钥,您可以在每次访问时无需提供用户名或密码即可连接到GitHub。

尝试使用ssh访问Github

ssh -T git@github.com

在设置ssh授权之前,会提示:can’t be established.

Administrator@GOS-01709151419 MINGW64 ~/.ssh
$ ssh -T git@github.com
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCsiRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
git@github.com: Permission denied (publickey).

检查本地是否存在ssh keys

  1. 打开Git Bash,输入以下命令:

    ls -al ~/.ssh

如果ssh文件夹内有类似id_rsa.pub的文件,代表你的本地已经拥有了ssh keys,那么你可以直接跳过生成ssh keys的步骤。

生成一个新的ssh key

  1. 打开Git Bash,输入以下命令:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

此处需要你填写的邮箱,这个邮箱地址是个注释(标识),没有任何实质作用。建议填写个人邮箱作为注释。

​ 会有三个提示,直接按回车即可。最终信息类似如下:

Administrator@GOS-11709151419 MINGW64 ~
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:UDZ6Tpa1DZgBgCM+rT94d41+tfgFWdAdJcRqcrhAuNU your_email@example.com
The key's randomart image is:
+---[RSA 4096]----+
|   ....o==o..o+.+|

+----[SHA256]-----+
  1. 复制id_rsa.pub中内容,将作为ssh key粘贴到GitHub上。

将SSH密钥加入你的GitHub账户中

  1. 登录GitHub,点击右上角的头像图标,点击settings,再点击左侧栏里面的SSH and GPG keys,然后点击右侧“New SSH key”
  1. 把复制的id_rsa.pub中内容粘贴到Key中。Title为标识,可以任意填写。如:
  1. 添加后,再次尝试使用ssh访问GitHub则显示为:
Administrator@GOS-01709151419 MINGW64 ~/.ssh
$ ssh -T git@github.com
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCsiRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Hi YourGitHubName! You've successfully authenticated, but GitHub does not provide shell access.

此时,完成SSH访问GitHub设置