创建git密钥

前言

git使用https协议,每次pull,push都要输入密码,使用git协议,使用ssh秘钥,可以省去每次输密码

大概需要三个步骤:

一、本地生成密钥对;

二、设置github上的公钥;

三、修改git的remote url为git协议。


生成密钥

$ ssh-keygen -t rsa -C “your_email@youremail.com“
 
Creates a new ssh key using the provided email # Generating public/private rsa key pair.
 
Enter file in which to save the key (/home/you/.ssh/id_rsa):
 
直接Enter就行。然后,会提示你输入密码,如下(建议输一个,安全一点,当然不输也行):
 
Enter passphrase (empty for no passphrase): [Type a passphrase]
 
Enter same passphrase again: [Type passphrase again]

然后会提示你,你的密钥在哪

Your identification has been saved in /home/you/.ssh/id_rsa.
 
Your public key has been saved in /home/you/.ssh/id_rsa.pub.
 
The key fingerprint is: # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@youremail.com
]

添加公钥到你的github帐户

1.复制密钥

$ cat ~/.ssh/id_rsa.pub

如果你是windows用户,那么请在你的用户目录下,找到.ssh文件夹然后打开id_rsa.pub直接复制就可以了

2、登陆你的github帐户。然后 Account Settings -> 左栏点击 SSH Keys -> 点击 Add SSH key

3、然后你复制上面的公钥内容,粘贴进“Key”文本域内。 title域,你随便填一个都行。

4、完了,点击 Add key。

这样,就OK了。然后,验证下这个key是不是正常工作。

$ ssh -T git@github.com

会提示你:

Hi username! You’ve successfully authenticated, but GitHub does not # provide shell access.

这就大功告成!

posted @ 2020-02-21 17:24  huanhao  阅读(1186)  评论(0编辑  收藏  举报