引用自
设置 SSH 使用 hexo deploy 时免输用户名密码
清除原有ssh密钥方法
若重装系统等导致密钥丢失,先看这个👈
请先设置用户名&邮箱
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%;" />
在网页的左侧,找到我们需要应用的Repositories,点击进去。
点击Settings。
[站外图片上传中...(image-ebecfa-1709208684955)]
在下方,找到并点击Deploy keys的选项卡,然后点击右侧的Add deploy key
[站外图片上传中...(image-f24cad-1709208684955)]
打开以后,把生成的密钥文件(命令框截图中的红框内容)以记事本的方式打开,复制文件中的全部内容粘贴到Key下方的文本框。
Title的内容自行输入方便记忆的字符。
下方的Allow write access,并打勾✔。
最后点击 Add key 完成添加公钥。
4.测试
在PowerShell当中输入以下命令测试。
ssh -T git@github.com
测试成功
如果出现以下文本,则表示身份验证成功!
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 执行
ssh-agent -s - 若返回
unable to start ssh-agent service, error :1058,说明服务的启动类型设置不正确。 - 使用管理员权限运行 PowerShell,然后执行
Set-Service -Name ssh-agent -StartupType automatic。#启动类型给它设为自动就可以了。 - 然后再次执行
ssh-agent -s,若没有提示任何东西则代表成功。 - 执行
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.修改传输协议
#切换至包含.git的文件夹
$ cd .\.deploy_git\
#查看当前的传输协议
$ git remote -v
# 修改 https 协议为 ssh 协议
$ 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]
局部用户设置
# 给目录加权限(常用于重装系统之后,用户权限问题)
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