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、登錄遠程庫查看是否推送成功