mah*_*ood 9 git gnupg gpg-agent
使用git version 2.20.1官方指南,我运行以下命令来生成 pgp 密钥
$ gpg --full-generate-key
...
$ gpg --list-secret-keys --keyid-format LONG
gpg: checking the trustdb
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
/home/mahmood/.gnupg/pubring.kbx
--------------------------------
sec rsa4096/CFEFE6D58A392624 2020-09-08 [SC]
26XX594XXXE2BAXXXE40AXXXCFXXX6D5XXXXX624
uid [ultimate] mahmood <EMAIL>
ssb rsa4096/3B138A448B277FD9 2020-09-08 [E]
Run Code Online (Sandbox Code Playgroud)
现在我可以使用以下命令查看公钥:
$ gpg --armor --export CFEFE6D58A392624
-----BEGIN PGP PUBLIC KEY BLOCK-----
mQINBF9XdKoBEACyQjVUlBYjOLSqv7YRIIq0+iJ9A0UzkItUoWBnDrHmTdnH+UeK
...
=WCOk
-----END PGP PUBLIC KEY BLOCK-----
Run Code Online (Sandbox Code Playgroud)
然后我根据这个官方页面复制了网站中的密钥。
现在,当我想提交时,我收到密钥签名错误:
$ git commit -S -m "...."
error: gpg failed to sign the data
fatal: failed to write commit object
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
更新:
导出以下变量将解决该问题。
export GPG_TTY=$(tty)
Run Code Online (Sandbox Code Playgroud)
我是如何做到这一点的?首先我检查~/.gitconfig以确保该[user]部分正确。然后我运行了以下测试命令,这给了我一个 ioctl 错误
$ echo "test" | gpg --clearsign
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
test
gpg: signing failed: Inappropriate ioctl for device
gpg: [stdin]: clear-sign failed: Inappropriate ioctl for device
Run Code Online (Sandbox Code Playgroud)
搜索该错误导致export GPG_TTY=$(tty)然后测试命令正常。因此,提交命令现在可以了。
man gpg-agent,
You should always add the following lines to your .bashrc or whatever
initialization file is used for all shell invocations:
GPG_TTY=$(tty)
export GPG_TTY
It is important that this environment variable always reflects the out-
put of the tty command. For W32 systems this option is not required.
Run Code Online (Sandbox Code Playgroud)
根据我的使用经验,GPG_TTY需要环境变量来gpg-agent检测哪个 tty/window/shell 处于活动状态并弹出密码输入提示。
您还需要不时更新此信息。否则,密码短语提示可能不会在您的工作 shell 中弹出,而是在另一个 shell 中弹出。
大多数时候,出口GPG_TTY就足够了。如果您也使用 gpg-agent 作为 ssh 代理。还需要更新 tty 信息以获得 gpg-agent 的ssh 支持。这是我在 ZSH 中为 gpg-agent 的 ssh 支持所做的事情。
# Updates the gpg-agent TTY before every command since
# there's no way to detect this info in the ssh-agent protocol
function _gpg-agent-update-tty {
gpg-connect-agent UPDATESTARTUPTTY /bye &>/dev/null
}
autoload -Uz add-zsh-hook
add-zsh-hook preexec _gpg-agent-update-tty
Run Code Online (Sandbox Code Playgroud)