51

gnuPG 包 gpg 的加密工具在由普通用户调用时使用 GUI 对话框提示输入密码,但是当由 root 调用时,它会在 CLI 上提示。即使由普通用户调用,如何使其使用 CLI。版本:GnuPG 1.4.12

4

8 回答 8

21

On a debian box:

sudo apt install pinentry-tty
sudo update-alternatives --config pinentry

(and set it to pinentry-tty)

于 2018-12-05T21:32:37.670 回答
18

这种密码提示不是由 gpg 自己完成的,而是由 gpg-agent 完成的。

您可以配置您的 gpg-agent 应该使用哪个 pinentry 程序。在启动代理时和 gpg-agent 配置文件中都有选项——请看一下手册页。

所以我看到了两个选择:

  • 配置您的 gpg-agent 以使用所需的方法
  • 禁用 gpg 代理;您可以通过取消设置环境变量GPG_AGENT_INFO(如GPG_AGENT_INFO="" gpg .... gpg 曾经有一个--no-use-agent选项,但这已被标记为已弃用,并且在最近的 gpg 版本中没有任何功能。
于 2013-07-22T07:17:24.807 回答
11

我使用的是较新的 GPG (2.2.13),gpg-agent手册页内容如下:

您应该始终将以下行添加到您的.bashrc或用于所有 shell 调用的任何初始化文件中:

GPG_TTY=$(tty)
export GPG_TTY

重要的是,此环境变量始终反映tty命令的输出。...

这并没有解释“为什么”(我也不明白“为什么”),但是在我的.bashrc工作中设置它:它导致我gpg-agent通过pinentry-curses界面询问我的密码。我不需要设置任何其他配置(我什至没有~/.gnupg/gpg-agent.conf文件)。

编辑:这似乎在没有DISPLAY可用的情况下有效,例如在没有 X11 转发的 SSH 会话中。但是当我在桌面上并DISPLAY设置好后,它仍然会弹出 GUI 对话框。unset DISPLAY(BASH)在这种情况下使它使用curses而不是GUI。

于 2019-03-12T16:38:10.667 回答
10

在较新版本的 gpg >= 2.x 中,您可以使用它

stty -echo; gpg --passphrase-fd 0 --pinentry-mode loopback --decrypt filename; stty echo

stty 确保您的密码在您输入时不会被回显。如果您想要的只是一个快速的桌面工作站,那么比摆弄配置要顺畅一点。

但是,您将需要 pinentrysudo apt install pinentry-curses或可能sudo apt install pinentry

这与 gpg 1.x 的旧时代相去甚远,您所需要的只是--no-use-agent.

于 2019-03-17T22:06:52.167 回答
1

如果您在 MacOS 上并使用pinentry,则可以转到 > System Preferences> GPG Suite,然后将两者都禁用

Store in macOS keychain

Remember for ### seconds

并单击Delete stored OpenPGP passwords按钮。

删除 OpenPGP 密码部分非常重要,因为如果您的密码已经存储在钥匙串中,禁用其他 2 个选项是不够的!

我尝试了该线程中提到的大多数其他内容,包括添加export GPG_TTY=$(tty)~/.bash_profile、使用gpg --no-use-agent(收到已弃用的消息,但仍然无法正常工作)、查看~/.gnupg/gpg-agent.conf~/.gnupg/gpg.conf文件(两者都没有任何看起来会改变任何东西的东西),以及pinentry在我这样做之前,没有什么真正让 CLI GUI 提示输入密码。

希望这可以帮助将来遇到此问题的任何人!在我更新 gpg 并意外启用了这些选项之后,我花了几个月的时间才弄清楚如何将它们恢复回来。

澄清一下,如果您希望 pinentry 在您想要使用 PGP 密钥验证的任何时候提示您输入密码,请确保您没有启用记住密码的选项并确保您删除任何您可能存储在 macOS 钥匙串中的 PGP 密码。

编辑:在 Ask Different 上找到了类似的答案:How to use GUI pinentry program for GPG

于 2020-12-06T12:03:55.820 回答
0

您可以设置以下环境变量来禁用图形提示

export PINENTRY_USER_DATA="USE_CURSES=1"
于 2016-11-24T08:58:07.050 回答
0

我的要求是通过 SSH 连接在远程服务器中创建密钥。

我的要求不是编写所有内容

没有人提到这种方法。但它对我有用,并且满足我的要求:

  1. 在 X11 (UI) 可用的机器中创建密钥
gpg2 --gen-key

...output omitted...
Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) Enter
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) Enter
Key does not expire at all
Is this correct? (y/N) y

GnuPG needs to construct a user ID to identify your key.

Real name: Abd Tm
Email address: abd@example.com
Comment: Enter
You selected this USER-ID:
    "Abd Tm <abd@example.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
  1. 导出密钥:
# export private key
gpg2 --export-secret-keys --armor --output myfile abd@example.com

# export public key (same command above except --export arg)
gpg2 --export --armor --output myfile.pub abd@example.com

  1. 将 2 个文件复制到我真正要安装密钥的服务器:
scp myfile* user@remote-server:/home/user
  1. SSH 到远程服务器并导入密钥
ssh user@remote-server

[user@remote-server] $ gpg2 --import /home/user/myfile
[user@remote-server] $ gpg2 --import /home/user/myfile.pub

  1. 验证它是否有效:
[user@remote-server] $ gpg2 --list-keys

这篇文章的学分

于 2020-12-30T10:27:30.167 回答
-1

你可以这样做 :

gpg --batch --decrypt --passphrase-fd 0 gpgfile < passphrase-file
于 2013-10-23T06:55:10.157 回答