引用自
設置 SSH 使用 hexo deploy 時免輸用戶名密碼
清除原有ssh密鑰方法
若重裝系統(tǒng)等導致密鑰丟失,先看這個??
請先設置用戶名&郵箱
git config --global user.name “yourname” #設置git全局用戶名
git config --global user.email “email@email.com ” #設置git全局郵箱名
然后再從以下開始
生成SSH公鑰
1.首先打開命令行工具
輸入
cd ~/.ssh
如下圖可見,會跳轉到我們所生成的.ssh文件夾中
[站外圖片上傳中...(image-3c513d-1709208684955)]
此時.ssh文件夾下是無文件的。
[站外圖片上傳中...(image-6442e7-1709208684955)]
2.然后使用ssh-keygen生成公鑰,在PowerShell當中輸入以下命令。
ssh-keygen -t rsa -C "your_email@example.com"
[站外圖片上傳中...(image-86ba2b-1709208684955)]
圖片中第三行為文件名,輸入任意字符按下三次回車,即可生成。
大致效果如圖中所示,生成好的密鑰在紅框標記的文件當中。(可用記事本等工具打開)
3.將公鑰添加到Github的項目中
進入GitHub
<img src="https://raw.githubusercontent.com/Suuuuulan/imagess/master/2021/image-20210111223627202.png" alt="image-20210111223627202" style="zoom: 25%;" />
在網(wǎng)頁的左側,找到我們需要應用的Repositories,點擊進去。
點擊Settings。
[站外圖片上傳中...(image-ebecfa-1709208684955)]
在下方,找到并點擊Deploy keys的選項卡,然后點擊右側的Add deploy key
[站外圖片上傳中...(image-f24cad-1709208684955)]
打開以后,把生成的密鑰文件(命令框截圖中的紅框內(nèi)容)以記事本的方式打開,復制文件中的全部內(nèi)容粘貼到Key下方的文本框。
Title的內(nèi)容自行輸入方便記憶的字符。
下方的Allow write access,并打勾?。
最后點擊 Add key 完成添加公鑰。
4.測試
在PowerShell當中輸入以下命令測試。
ssh -T git@github.com
測試成功
如果出現(xiàn)以下文本,則表示身份驗證成功!
Hi "username"! You've successfully authenticated, but GitHub does not provide shell access.
[站外圖片上傳中...(image-e61133-1709208684955)]
測試失敗
第一種情況(這是我在23年3月18日晚遇到的,隨即更新一下)
git@github.com: Permission denied (publickey).#在ssh -T之后提示這個的
-
PowerShell 執(zhí)行
ssh-agent -s - 若返回
unable to start ssh-agent service, error :1058,說明服務的啟動類型設置不正確。 - 使用管理員權限運行 PowerShell,然后執(zhí)行
Set-Service -Name ssh-agent -StartupType automatic。#啟動類型給它設為自動就可以了。 - 然后再次執(zhí)行
ssh-agent -s,若沒有提示任何東西則代表成功。 - 執(zhí)行
ssh -T git@github.com,輸入yes后,提示身份驗證成功。
5.設置博客的提交地址
1.修改_config.yml
因為我們使用ssh上傳的方式,原配置文件中依舊為為https,故須先做修改。
用記事本打開_config.yml,查找以下字段,將repo修改為git@github.com:名字替換/名字替換.github.io.git
deploy:
type: 'git'
repo: git@github.com:suuuuulan/suuuuulan.github.io.git
branch: master
2.修改傳輸協(xié)議
#切換至包含.git的文件夾
$ cd .\.deploy_git\
#查看當前的傳輸協(xié)議
$ git remote -v
# 修改 https 協(xié)議為 ssh 協(xié)議
$ git remote set-url origin git@github.com:USERNAME/REPOSITORY2.git
6.大功告成
$$hexo g
$$hexo d
git init
git config user.name "github用戶名"
git config user.email "注冊郵箱@example.com"
git remote add origin <remote-repository-URL>
ssh-keygen -t ed25519 -C "注冊郵箱@example.com"
ssh -T git@github.com
deploy:
type: git
repo: git@github.com:YourUsername/YourRepository.git
branch: [branch-name]
局部用戶設置
# 給目錄加權限(常用于重裝系統(tǒng)之后,用戶權限問題)
git config --global --add safe.directory "D:/Blog/.deploy_git"
# 設置局部用戶名稱
git config user.name "XXX"
# 設置局部用戶郵箱
git config user.email "CCC@hotmail.com"
# 生成ssh
ssh-keygen -t ed25519 -C "CCC@hotmail.com"
# 測試連接
ssh -T git@github.com