Git-设置SSH Key

        GitHub上连接已有仓库时的认证,是通过使用SSH的公开密钥认证方式进行的。我们首先来创建SSH key,并将其添加到GitHub上。

前提是要初始化设置姓名和邮箱地址

$ git config --global user.name "GraceSi"
$ git config --global user.email "[email protected]"


1、创建SSH Key

$ ssh-keygen -t rsa -C "[email protected]"

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/zhangsisi/.ssh/id_rsa): 按回车键
/c/Users/zhangsisi/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 输入密码
Enter same passphrase again:再次输入密码 

输入密码后结果:

Your identification has been saved in /c/Users/zhangsisi/.ssh/id_rsa.
Your public key has been saved in /c/Users/zhangsisi/.ssh/id_rsa.pub.
The key fingerprint is:
fingerprint值 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|       ....o   +.|
|    . .   . + = o|
|     o o   o = + |
|    o =    .+ * +|
|   o B .S .+.+ =.|
|    o = ....*o+  |
|     o o o.. +oE |
|      * .     .+ |
|    .+        ..o|
+----[SHA256]-----+
id_rsa 文件时私有密钥   id_rsa.pub是公有密钥

2、获取公有密钥

$ cat ~/.ssh/id_rsa.pub
ssh-rsa 公开密钥内容 [email protected]

3、GitHub添加公开密钥

点击GitHub有上角账户设置按钮(Setting),选择SSH and GPG keys,点击add ssh Key,出现如下内容。

Git-设置SSH Key_第1张图片

4、测试认证成功

$ ssh -T [email protected]
The authenticity of host 'github.com (52.74.223.119)' 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,52.74.223.119' (RSA) to the list of known   hosts.
Enter passphrase for key '/c/Users/zhangsisi/.ssh/id_rsa':

出现下面结果证明成功

Hi GraceSi! You've successfully authenticated, but GitHub does not provide shell access.


连接成功后,接下来每次连接github通过密钥进行认证,不需要再输入用户名和密码,简单方便。


你可能感兴趣的:(Git-设置SSH Key)