How can I restart gpg-agent?

69

13

Some changes in the GnuPG configuration demand a gpg-agent restart / reboot, but... How can I do that? I tried gpg-agent restart, service gpg-agent restart, but did not succeed.

Felipe

Posted 2016-05-11T00:08:39.827

Reputation: 921

Answers

105

With current GPG (2.1+), to stop gpg-agent you can use gpgconf --kill, like this:

gpgconf --kill gpg-agent

You shouldn’t need to manually restart it. GPG will restart it when it’s needed.

sideshowbarker

Posted 2016-05-11T00:08:39.827

Reputation: 1 571

7There are cases where you want to start the agent manually, e.g. when you use it with ssh support enabled. The agent will NOT automatically start when you attempt an ssh login. – hasufell – 2017-12-17T22:36:29.247

1If you ever delete the ~/.gnupg directory, you will need to restart the gpg agent manually. – Christopher Martin – 2018-02-24T21:16:11.110

25

My preferred way is with gpg-connect-agent reloadagent /bye. See gpg-connect-agent help /bye for a complete list of commands.

ben

Posted 2016-05-11T00:08:39.827

Reputation: 373

10

gpg-agent is not a system-wide service but started once per user (thus, it is not managed by service). Although sometimes invoked by user's dotfiles or at least in Debian and derivatives also when X11 is started (and gpg-agent is installed) in /etc/X11/Xsession.d/90gpg-agent (to make sure a common gpg-agent is used by all GnuPG calls, no matter whether from a terminal or GUI applications); it is also started automatically by GnuPG when required. From man gpg-agent:

The agent is automatically started on demand by gpg, gpgsm, gpgconf, or gpg-connect- agent. Thus there is no reason to start it manually. In case you want to use the included Secure Shell Agent you may start the agent using:

gpg-connect-agent /bye

Usually, a simple killall gpg-agent (from a non-root shell) should be fine for terminating gpg-agent. You'll likely observe a slight delay when using GnuPG the next time, as gpg-agent is started again.

Jens Erat

Posted 2016-05-11T00:08:39.827

Reputation: 14 141

2This changes in GPG 2.1.x and the process is handled through dirmngr. The commands to run then are dirmngr --shutdown followed by dirmngr --daemon and sometimes additional options (I also include a specific GPG homedir and the --use-tor flag). – Ben – 2016-05-22T04:45:17.927

1

In my experience there are some scenarios where gpg will fail to start a fresh gpg agent (importing a new key?).

Kill the old agent as so:

GNUPGHOME="${GNUPGHOME:-$HOME/.gnupg}" gpgconf --kill gpg-agent

and then start the new one:

gpg-agent --homedir "${GNUPGHOME:-$HOME/.gnupg}" --daemon

Setting the --homedir explicitly when starting assures your ps listing is clear when you have more than one homedir; and it's analagous to what gpg does when it starts it.

Setting the GNUPGHOME when stopping is not necessary, but it might make you or the code reviewer more comfortable.

Ben Hyde

Posted 2016-05-11T00:08:39.827

Reputation: 121