我决定再次研究一下,并找出它的工作原理。GPG使用术语“缓存”来存储密码。可以对最大存储时间施加两个限制:
- 自密钥初始添加以来保留密码短语的时间。
- 自上次访问密码短语以来保留的时间。
此外,对于这两个限制,存在两种变体,一种用于GPG密钥,另一种用于SSH密钥(如果启用了支持)。
相关的手册页面条目来自:
--default-cache-ttl n
Set the time a cache entry is valid to n seconds. The default
is 600 seconds.
--default-cache-ttl-ssh n
Set the time a cache entry used for SSH keys is valid to n sec‐
onds. The default is 1800 seconds.
--max-cache-ttl n
Set the maximum time a cache entry is valid to n seconds. After
this time a cache entry will be expired even if it has been
accessed recently. The default is 2 hours (7200 seconds).
--max-cache-ttl-ssh n
Set the maximum time a cache entry used for SSH keys is valid to
n seconds. After this time a cache entry will be expired even
if it has been accessed recently. The default is 2 hours (7200
seconds).
口令短语始终被缓存(在内存中,而不是在磁盘上!通过
$HOME
的git仓库进行验证),因此没有明确需要使用
ssh-add
。例如,签署虚拟数据已经触发了缓存:
$ echo | gpg -s >/dev/null
(passphrase requested
$ echo | gpg -s >/dev/null
(signing proceeds without asking for passphrase)
要对 gpg-agent 的缓存设置进行永久更改,请编辑 ~/.gnupg/gpg-agent.conf 并添加类似以下内容的内容:
default-cache-ttl 60 # Expire GPG keys when unused for 1 minute
max-cache-ttl 600 # Expire GPG keys after 10 minutes since addition
我尝试通过指定
enable-ssh-support
来启用SSH代理支持,但这会导致gpg-agent要求您使用另一个密钥来加密密钥,然后将您的私钥存储在
~/.gnupg/private-keys.d/
中。对我来说不行,我还是坚持使用双重ssh-agent / gpg-agent方法。
一些额外的提示:
gpg-connect-agent
? - Smithamaxgpg-preset-passphrase
相同的功能。我运行了gpg-connect-agent
,获得了一个 shell,并执行了setkey IDOFMYPRIVATEKEYHERE
,然后是preset_passphrase
,结果显示为 "ERR 67108924 Unsupported <GPG-agent> - no --allow-preset-passphrase"。 - Lekensteyn