设置 SSH 使用 hexo deploy 时免输用户名密码

设置 SSH 使用 hexo deploy 时免输用户名密码

转自我的博客,转载请声明走啊走的记录:设置 SSH 使用 hexo deploy 时免输用户名密码git


此篇博客日期 2016-05-14,使用 hexo v0.2.0。介绍清楚这个使用背景是由于网上的有些教程已通过时,至少当我彻底按照那些步骤来设置的时候并无成功。SSH 验证就是咱们键入命令 hexo deploy 之后,当那些须要提交的内容显示完成事后会弹出的一个小窗口提示咱们输入用户名、密码。我参考了网上的一些教程,如今给出完整可行的教程,let's do it!github

生成 SSH 密钥

首先打开命令行工具,一般咱们能够直接右键鼠标任意位置打开 Git Bash Here,而后输入如下命令shell

$ cd ~/.ssh
# Checks to see if there is a directory named ".ssh" in your user directory

使用 ssh-keygen 生成密钥json

$ ssh-keygen -t rsa -C "your_email@example.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,接下来显示以下hexo

Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

按提示输入密码便可(简化流程的话在键入命令 ssh-keygen -t rsa -C "your_email@example.com" 之后的三次提示输入均可以直接回车跳过)。ssh

密钥生成成功ide

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:
…………………此处是密钥内容…………………… your_email@example.com

设置 Github 项目的 Deploy keys

能够选择是在用户设置中添加密钥(Settings --> SSH GPG keys)仍是单个项目中添加密钥,在这里我就在咱们的博客项目中添加密钥,此处以个人博客为例。
chengjianhua.github.io 的首页菜单栏中点击 Settings --> Deploy keys --> Add deploy key,而后将生成的 id_rsa.pub 中的内容全选复制到 key 输入框中,而后点击 Add key 完成添加公钥。工具

接下来测试配置是否成功测试

$ ssh -T git@github.com

若是出现如下内容即表示配置完成而且成功!fetch

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

设置博客的提交地址

到了这一个步骤和你们在网上搜到的其余教程就不同了,网上的这一步就是设置

# 切入到包含 .git 文件夹的文件夹
$ cd .deploy/

# 查看当前的传输协议
$ git remote -v

origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
origin  https://github.com/USERNAME/REPOSITORY.git (push)

# 修改 https 协议为 ssh 协议
$ git remote set-url origin git@github.com:USERNAME/REPOSITORY2.git

可是我根据网上的并无用。更别提个人 blog 文件夹中包含 .git 文件夹的文件夹(好绕啊……)名字是 .deploy_git 了。还有我输入 $ git remote -v 时的显示结果为空,我尝试过本身添加 $ git remote add origin git@github.com:chengjianhua/chengjianhua.github.io.git,而后 $ hexo deploy 仍是须要输入密码。

解决办法出来了!

hexo deploy 的控制台输出结果中我看到了连接仍然是 https 的连接,这时我想到了 hexo 的配置文件 _config.yml 中的 deploy 属性。

deploy:
  type: git
  repo: git@github.com:chengjianhua/chengjianhua.github.io.git
  branch: master

我将其中的 repo修改为了git@github.com:chengjianhua/chengjianhua.github.io.git,这个时候我再尝试了 $ hexo deploy 果真成功了,就不用输入密码了!

到此个人解决方法已经完成了,但愿能帮到使用 hexo 的同窗们~

相关文章
相关标签/搜索