gpg-agent 无法连接

在一个全新的Ubuntu 16.04安装中,我想使用gpg来加密我的数据库备份,但是我无法让gpg-agent工作。
postgres@db:~$ gpg -s test.txt

You need a passphrase to unlock the secret key for
user: "Nicolas Remond <nicolas@xxx.com>"
2048-bit RSA key, ID F5DECA47, created 2016-11-03

gpg: gpg-agent is not available in this session
Enter passphrase: 

另外,似乎有一个gpg-agent正在运行,因为我之前做了以下操作:

postgres@db:~$ gpg-agent
gpg-agent[1715]: no gpg-agent running in this session
postgres@db:~$ gpg-connect-agent /bye
gpg-connect-agent: no running gpg-agent - starting '/usr/bin/gpg-agent'
gpg-connect-agent: waiting for the agent to come up ... (5s)
gpg-connect-agent: connection to agent established
postgres@db:~$ pidof gpg-agent
1762

而且gpg配置中有"use-agent"设置:
postgres@dbmaster0:~$ cat ~/.gnupg/gpg.conf | grep use-agent
# For Ubuntu we now use-agent by default to support more automatic
use-agent

我错过了什么?

使用--log-file [myfile]启动代理,并查看日志中是否有任何错误信息。 - Raniz
我遇到的正确错误信息是gpg: gpg-agent在此会话中不可用。如果我按照您的建议记录日志,我所得到的只有:2016-12-19 13:39:39 gpg-agent[10957] gpg-agent (GnuPG) 2.1.11已启动 - n1r3
我尝试手动设置GPG_AGENT_INFO,但是我不知道应该设置什么...这就是当我遇到gpg: problem with the agent - disabling agent use时的情况。 - n1r3
作为root用户,设置export GPG_AGENT_INFO=/var/lib/postgresql/.gnupg/S.gpg-agent:0:1是有效的,但对于普通用户则不行。此外,看起来我需要手动执行这个操作,是吗? - n1r3
你是否按照“man gpg-agent”页面上的说明,在你的“.bashrc”文件中添加了这个“__GPG_TTY=$(tty) export GPG_TTY__”? - George Udosen
请问您能否通过 pgrep -xu "$USER" gpg-agent 命令检查一下您的用户是否已经运行了代理?GPG_AGENT_INFO 环境变量的值是多少(使用 echo "$GPG_AGENT_INFO" 命令进行检查)?根据结果,我会进一步提出问题。 - David Foerster
@George: 确实,我已经有了。 - n1r3
@DavidFoerster: pgrep -xu "$USER" gpg-agent 返回结果为 8572,而 GPG_AGENT_INFO 是空的,这让我感到困惑,不知道是为什么? - n1r3
好的,这是一个调查的良好开端。让我们确保gpg-agent可以正常工作。请终止现有的代理(pkill -xu "$USER" gpg-agent),启动一个新的,并让您的shell“来源于”它的输出:eval $(gpg-agent --daemon)。然后尝试使用代理,例如gpg -s some-file - David Foerster
相关链接:http://unix.stackexchange.com/questions/46960/how-to-configure-gpg-to-enter-passphrase-only-once-per-session - Elder Geek
2个回答

回答自己以防其他人遇到同样的问题。
在Ubuntu 16.04上,默认版本如下:
    # gpg --version
    gpg (GnuPG) 1.4.20

    # gpg-agent --version
    gpg-agent (GnuPG) 2.1.11

它们不兼容。必须使用GPG2。
    # gpg2 --version
    gpg (GnuPG) 2.1.11

解决方案是安装 gpgv2
sudo apt install gpgv2

这是因为Ubuntu自带的gpg-agent是2版本,它试图与安装了1版本的Ubuntu上的gpg进行连接。

3可能你指的是 gnupg2 - Dmitriusan
不,只有在安装了gpgv2之后才有效。我尝试过gnupg2,但它没有起作用。 - phoxd