Posted by chris on Tue 28 Mar 2006 at 08:00
After going through the article on Gnu Privacy Guard (GPG) you've got gpg up and running. But - every time you need to encrypt, decrypt or sign, you need to enter your passphrase.
Wouldn't it be nice to have something similar to how ssh-agent handles ssh passphrases for you?
Well - you have - introducing gpg-agent!
gpg-agent is an application that can run as a daemon and remember your passphrase for you.
The best way to use it is to configure it to start at X login so that it is valid for any X apps/terminals on your desktop.
Configuration is straightforward once it has been installed:
apt-get install gnupg-agent pinentry-gtk
First - we need to tell gpg to use it
Edit the GPG configuration file ~/.gnupg/gpg.conf to either uncomment or add the line:
use-agent
Now - create a new file ~/.gnupg/gpg-agent.conf
Mine looks like:
pinentry-program /usr/bin/pinentry-x11 no-grab default-cache-ttl 1800
Here the pinentry-program specifies which program should be invoked to receive your passphrase the first time. There are several packages and programs available, which you can see by running:
skx@itchy:~$ apt-cache search ^pinentry pinentry-curses - curses-based PIN or pass-phrase entry dialog for GnuPG pinentry-doc - documentation for pinentry packages pinentry-gtk - GTK+-based PIN or pass-phrase entry dialog for GnuPG pinentry-gtk2 - GTK+-2-based PIN or pass-phrase entry dialog for GnuPG pinentry-qt - Qt-based PIN or pass-phrase entry dialog for GnuPG
Here we've installed the gtk variant, the curses version will work nicely for console access.
You can set your preferred ones using Debian's alternatives mechanism - but - I always use this under X - so I just linked directly to the x11 binary.
Now - we can test it - open a terminal - and then run:
eval "$(gpg-agent --daemon)"
This will set some environment variables. You can now try any gpg command that requires a passphrase - and gpg-agent will handle the passphrase request.
If things work the first time you run a command which would prompt for your GPG passphrase the pinentry variant we chose will be invoked to receive it, subsequent requests will proceed using the cached copy in memory.
Finally - to add this to start when you login to X - add the line to .xsession
eval "$(gpg-agent --daemon)"
KDE/Gnome users - you'll need to add this somewhere - rumours have reached me that you can try editing startkde or startgnome - but I don't really know. Hopefully some kind soul will add a comment to this article with the required info :)
Restart x and all should now be working.
If you use enigmail for thunderbird - don't forget to go into the preferences and check off for "Use gpg-agent" :)
...
if grep -qs '^use-agent' "$GNUPGHOME/gpg.conf" "$GNUPGHOME/options"
...
[ Parent ]
[ Parent ]
[ Parent ]
Very good question.
The reason I wrote this article is that I really struggled to find out how to get it working. So - I thought I'd share it.
I added the no-grab option because it was in every example I found online.
Googling for gpg-agent no-grab gives pages and pages of results where people just use it.
So - I've just downloaded (apt-get source gnupg-agent) the source pacakge where you will find:
{ oNoGrab, "no-grab" ,0, N_("do not grab keyboard and mouse")},
Without getting deep into the code I'm still not really any clearer - I'm guessing that it must be telling the agent not to read keyboard and mouse events (after all - we're using pinentry to handle entry of the passphrase)
[ Parent ]
Reading the output of "gpg-agent --help" gives a small clue:
--no-detach do not detach from the console
--no-grab do not grab keyboard and mouse
What that actually means is a little bit uncertain, as you're correct in saying the documentation is .. minimal.
[ Parent ]
[ Parent ]
[ Parent ]
You can use keychain to manage both gpg and ssh passwords/passphrases (it's just a set of scripts that manage ssh-agent and gpg-agent nicely).
Under KDE, I just have:
$HOME/.kde/env/keychain:
keychain ~/.ssh/id_dsa
. ~/.keychain/${HOSTNAME}-sh
. ~/.keychain/${HOSTNAME}-sh-gpg
(as of KDE 3.3-ish, you can put shell scripts in $HOME/.kde/env to have environment variables sourced for your entire KDE session, better than mucking with your .xsession)
With keychain picking up your passphrases as you need them, you can add the following to your .bash_profile to make sure that any console sessions also pick it up:
. ~/.keychain/${HOSTNAME}-sh
. ~/.keychain/${HOSTNAME}-sh-gpg
[ Parent ]