2

使用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]

现在我可以使用以下命令查看公钥:

$ gpg --armor --export CFEFE6D58A392624
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBF9XdKoBEACyQjVUlBYjOLSqv7YRIIq0+iJ9A0UzkItUoWBnDrHmTdnH+UeK
...
=WCOk
-----END PGP PUBLIC KEY BLOCK-----

然后我根据这个官方页面复制了网站中的密钥。

在此处输入图像描述

现在,当我想提交时,我收到一个密钥签名错误:

$ git commit -S -m "...."
error: gpg failed to sign the data
fatal: failed to write commit object

我该如何解决?

更新:

导出以下变量将解决问题。

export GPG_TTY=$(tty)

我是怎么做到的?首先,我检查~/.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

搜索该错误导致export GPG_TTY=$(tty)然后测试命令很好。因此,commit 命令现在可以了。

4

2 回答 2

3

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.

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
于 2020-09-15T03:01:50.050 回答
0

如果您在 macOS 中仍然遇到问题,请打开~/.gitconfig并将下面的所有内容更改[gpg]program = /usr/local/bin/gpg

于 2020-09-18T13:49:46.020 回答