Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

ssh-agent is very easy to use, I start it and add keys using ssh-add keyfile. After killing the process ssh-agent, all files are gone.

How can I get the same behavior with gpg-agent? The closest program I found was gpg-preset-passphrase. But looking at the manual page of gpg-agent, it seems that a directory is created for storing private keys.

I could be wrong, so I'm wondering how I can setup gpg-agent in such a way that no files/ directories are created? If it's not possible, other suggestions to make gpg-agent work like ssh-agent + ssh-add would be welcome too. I'm not looking for GUI solutions like Seahorse.

share|improve this question
    
have you checked out gpg-connect-agent? –  Smithamax Jan 17 '12 at 1:51
    
@Smithamax nope, but it seems to use the same functionality from gpg-preset-passphrase. I ran gpg-connect-agent, got a shell and executed setkey IDOFMYPRIVATEKEYHERE followed by preset_passphrase and id resulted in "ERR 67108924 Unsupported <GPG-agent> - no --allow-preset-passphrase" –  Lekensteyn Jan 17 '12 at 10:04
add comment

1 Answer

up vote 5 down vote accepted

I decided to have a look at this again and found out how it works. GPG uses the terminology "cache" for storing passwords. Two constraints can be imposed on the maximum storage time:

  • The time to keep a passphrase since the key was initially added.
  • The time to keep a passphrase since it was last accessed.

In addition, two variations exist for both constraints, one for GPG keys and one for SSH keys (if support was enabled).

The relevant manual page entries from gpg-agent(1):

   --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).

Passphrases are always cached (in memory, not on disk! Verified with a git repo of $HOME), so there is no explicit need for ssh-add. For example, signing dummy data triggers the cache already:

$ echo | gpg -s >/dev/null
(passphrase requested
$ echo | gpg -s >/dev/null
(signing proceeds without asking for passphrase)

To make permanent changes to the cache settings of gpg-agent, edit ~/.gnupg/gpg-agent.conf` and add something like:

default-cache-ttl  60     # Expire GPG keys when unused for 1 minute
max-cache-ttl     600     # Expire GPG keys after 10 minutes since addition

I have tried to enable SSH agent support by specifying enable-ssh-support, but this makes the gpg-agent ask you for another key to encrypt the key, and then stores your private key in ~/.gnupg/private-keys.d/. No go for me, I'll stick to a dual ssh-agent / gpg-agent approach then.

Some bonus tips:

  • SSH agent's equivalent of max-cache-ttl-ssh can be specified when adding the key, for example: ssh-add -t 600 ~/.ssh/id_rsa
  • To prevent storing the GPG passphrase in the agent, disable the agent. In newer GPG versions the option --no-use-agent is ignored, but you can prevent the agent from being used by clearing the related environment-variable. Some ways to do so:

    echo | GPG_AGENT_INFO= gpg -s         # temporary
    export GPG_AGENT_INFO=; echo | gpg -s # until the current shell is closed
    
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.