Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've read up a little on systemd, but still can't quite figure out how to use it to automatically start gpg-agent on login. I gather I'd use systemctl --user, but constructing the gpg-agent.service has me stumped. Any and all pointers are welkcome.

share|improve this question

The gpg-agent command starts a daemon, but programs that use it expect certain environment variables (GPG_AGENT_INFO and GPG_TTY) to be set so they know how to communicate with the agent. You have to somehow propagate these from the service script to your shells. The gpg-agent MAN page contains a snippet that starts the daemon and writes a shell code fragment to a file in the user's home

gpg-agent --daemon --write-env-file "${HOME}/.gpg-agent-info"

You can put this line into as shell script and call it from your service file


The .gpg-agent-info file has to be sourced from every shell. The MAN page recommends

if [ -f "${HOME}/.gpg-agent-info" ]; then
  . "${HOME}/.gpg-agent-info"

export GPG_TTY

in your .profile file to do this. Information on how to write systemd service files can be found in the systemd.service MAN page.

share|improve this answer
Yes, that much is clear from reading the man page, but isn't there some way of getting gpg-agent started so that its environment variables makes it into the login session? – Magnus Jun 6 '14 at 8:44
@Magnus I think you can do what you want by sourcing .gpg-agent-info from $HOME/.xprofile I use bash as my session command processor (and interactive shell), thus I source my .bashrc from both my .profile and .xprofile files. Then in my .bashrc I have a division: all that should go into the session environment comes first, then I add this test "[[ $- != *i* ]] && return" (no quotes) that detects if it is a non-interactive shell and exits, and all the settings that should go into an interactive environment go at the end of the file. – vorbote Jun 7 '14 at 20:16

Your Answer


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.