Git生成ssh公钥、免密登录
Git支持https和ssh两种传输协议,Github分享链接时会有两种协议可选:
- Clone with SSH
- Clone with HTTPS
Git在使用https协议的时候,每次pull、push都会提示要输入密码
使用ssh协议,这样可以免去在执行pull和push的时候都要输入密码的麻烦
使用ssh协议大致可以分为是三个步骤
- 生成密钥对
- 设置远程仓库上的公钥
- 把git的remote url修改为ssh协议
一、生成密钥对
绝大多数的git服务器都会选择SSH公钥来进行授权;
系统中的每一个用户都必须提供一个公钥用于授权,没有的话就必须要生成一个;
生成公钥的过程在所有的操作系统上的方式都大致一样;
首先确定自己本地是否已经有了公钥;
SSH公钥默认存储在用户主目录下的.ssh 目录中
1、查看文件夹下的文件是否包含id_rsa和id_rsa.push(或者是id_das和id_das.pub一类成对的文件),其中有.pub后缀的文件就是公钥,另一个对应的就是密钥
[root@localhost huashan]# cd ~/.ssh
[root@localhost .ssh]# ls
known_hosts
2、如果没有这些文件,甚至连.ssh目录都没有,可以用ssh-keygen来创建
[root@localhost .ssh]# ssh-keygen -t rsa -C 邮箱 ///创建密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): ///回车默认值
Enter passphrase (empty for no passphrase): ///回车默认值
Enter same passphrase again: ///回车默认值
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bsr9ADxExWgPCuS9W3dAedpF9irrGem1fuHVHFIklYw 邮箱
The key's randomart image is:
+---[RSA 2048]----+
| .. .+o. .o.=o.|
| ... .+.o ...E.+ |
| ...o.o.+ . o |
| .+ o.. o . |
| . = S o . ..o|
| o = . + . +|
| . + + .. o |
| . + + + .o |
| o ..=.o. |
+----[SHA256]-----+
3、此时已经生成密钥和公钥
[root@localhost .ssh]# ls
id_rsa id_rsa.pub known_hosts
二、设置远程仓库上的公钥
1、在终端上查看生成的公钥
- 命令:cat ~/.ssh/id_rsa.pub
[root@localhost .ssh]# ls
id_rsa id_rsa.pub known_hosts
[root@localhost .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDgbWkq3j8iXx9qQvwNmsjbxCszF+GP5/RFzk3pAS7AOFtF
p8+LX3WDaTTvVcvZcgcqUtcwxtBXH/MbzTcT01r9UXgb03nKeU8U4pGABTxmAngvKaBTh9ves8Co
NTtt8SgLi9R7kOkbyFcBNSBqL0PMEFJG+MIdthKJdTzyBYv25GFF9viO72zSy9wNqZLJCksw0/63
0AZuaj4n9e5KzRmbxriBfLw+vIw+ZJZ0ADKq7wbmxCfo5VNpDMXej5sZifup7402m6ljrtG9nd8R
ekNCUeLTgsDhOHkAPiRyn5g5qIMcEbzaFpAq59Bu+5kWUffMlOYgYQ5+lLKCbWmRC4RF
2、在Github远程库中设置公钥
- 登录Github官网 — 头像 — Setting设置 — ssh密钥
- 将上述生成的公钥粘贴进去
- 添加成功
三、修改Git的remote url
1、使用 git remote -v 命令查看当前 url
- 如果是https:打头的地址是https协议,git打头的则是git协议
[root@localhost huashan]# git remote -v
origin https://github.com/Yuebuqun-666/huashan.git (fetch)
origin https://github.com/Yuebuqun-666/huashan.git (push)
2、进入到Github远程库中,获取git ssh密钥
3、将该ssh密钥绑定至本地库中
- git remote add origin_ssh [ssh地址]
[root@localhost huashan]# git remote add origin_ssh git@github.com:Yuebuqun-666/huashan.git
4、使用git remote -v 命令查看当前所有协议地址
- 如果是https:打头的地址是https协议,git打头的则是git协议
[root@localhost huashan]# git remote -v
origin https://github.com/Yuebuqun-666/huashan.git (fetch)
origin https://github.com/Yuebuqun-666/huashan.git (push)
origin_ssh git@github.com:Yuebuqun-666/huashan.git (fetch)
origin_ssh git@github.com:Yuebuqun-666/huashan.git (push)
四、此时推送文件至远程库,不再需要输入密码
1、修改"jianfa"文本
[root@localhost huashan]# cat jianfa.txt
华山剑法、填下第一!!!
令说:令狐冲才是最厉害的!!!
令说:我会孤独剑法!!!
大家:真厉害 ///新增
2、提交至版本库
[root@localhost huashan]# git commit -m "zhenlihai" jianfa.txt ///可跨过暂存区
位于分支 master
您的分支领先 'origin/master' 共 1 个提交。
(使用 "git push" 来发布您的本地提交)
无文件要提交,干净的工作区
3、推送至远程库
- 此时不再需要输入邮箱和密码
- git push origin_ssh master
[root@localhost huashan]# git push origin_ssh master
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5: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)? yes ///输入yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
对象计数中: 3, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (3/3), 完成.
写入对象中: 100% (3/3), 323 bytes | 0 bytes/s, 完成.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:Yuebuqun-666/huashan.git
be7d909..c5d596f master -> master
4、登录远程库查看是否推送成功