缘由
近日,在浏览GitHub时,发现有的Commit有“Verified”标签,十分好奇,故开始探究方法,经查,是通过添加GPG密钥到GitHub而实现的
Step1 生成GPG密钥
本文软件环境为CentOS7,使用Yum
首先运行yum install gpg
安装所需软件包并按照如下步骤生成密钥
- 运行
gpg --gen-key
,开始生成密钥 - 密钥类型选“RSA and RSA”
- “key size”为4096
- 过期时间写0(即永不过期),然后确认信息
然后根据个人信息填写下面的问题,最后的密码需要牢记,以后“Git commit”时会用到
本步可能遇到的问题与解决方案
在最后生成密钥时提示“We need to generate a lot of random bytes. It is a good idea to perform some other action (type on the keyboard, move the mouse, utilize the disks) during the prime generation; this gives the random number generator a better chance to gain enough entropy.”并长时间卡住
解决方案
新建一个SSH会话,并以此运行如下代码
yum install rng-tools
service rngd start
Step2 配置GPG密钥到GitHub上
在终端中运行gpg --list-keys
,查看GPG密钥ID,输出应该与下面内容相似
/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid Hubot
ssb 4096R/42B317FD4BA89E7A 2016-03-10
在该示例中,GPG ID为3AA5C34371567BD2
然后运行命令gpg --armor --export <你的GPG密钥ID>
,并复制输出内容
依次进入:官网 – Settings-SSH and GPG keys,也可通过点击链接直接进入。点击 “New GPG Key”,粘贴密钥内容,点击 “Add SSH Key” 即可
Step3 配置GPG密钥到Git
依次运行如下命令
git config --global user.signingkey <你的GPG密钥ID>
test -r ~/.bash_profile && echo 'export GPG_TTY=$(tty)' >> ~/.bash_profile
echo 'export GPG_TTY=$(tty)' >> ~/.profile
source ~/.bash_profile
并配置默认给全部储存库签名提交
git config --global commit.gpgsign true
此时,你运行git commit时应该会要求你输入密码,输入您生成GPG密钥时设定的密码即可,登录GotHub,您的Commit应该已经带有“Verified”标记
参考文献
结语
欢迎关注我们的微信公众号 “Lime Network” 来获取更多关于动态,如果喜欢,可以通过页面下方的赞赏码支持我