Running gpg-agent on Ubuntu

I recently made it so that all my git commits are automatically signed by my GPG key (yay!), but typing my passphrase for every commit got old very quickly.

Thankfully, you can run an agent (much like ssh-agent) to cache credentials for you.

# First, install gpg-agent
sudo apt-get install gnupg-agent

# Then, add the following to your .bashrc/.zshrc
# Invoke GnuPG-Agent the first time we login.
# Does `~/.gpg-agent-info' exist and points to gpg-agent process accepting signals?
if test -f $HOME/.gpg-agent-info && \
    kill -0 `cut -d: -f 2 $HOME/.gpg-agent-info` 2>/dev/null; then
    GPG_AGENT_INFO=`cat $HOME/.gpg-agent-info | cut -c 16-`
else
    # No, gpg-agent not available; start gpg-agent
    eval `gpg-agent --daemon --no-grab --write-env-file $HOME/.gpg-agent-info`
fi
export GPG_TTY=`tty`
export GPG_AGENT_INFO

Eventually, I’d like to have multiple different GPG identities and use something like ssh-ident to manage them, but for now this is working for me

Michael is a polyglot software engineer, committed to reducing complexity in systems and making them more predictable. Working with a variety of languages and tools, he shares his technical expertise to audiences all around the world at user groups and conferences. You can follow @mheap on Twitter

Thoughts on this post

Leave a comment?

Leave a Reply