Sub topic 11
Passphrases, agents, and hardware-backed keys
This chapter assumes you can already create a GPG or SSH signing key and have set commit.gpgsign and gpg.format, as covered in earlier chapters. You should also know what a private key is and why it must stay secret.
Signing every commit is only practical if the key is protected and also easy to use. A key with no passphrase is easy to use but dangerous if the file is copied. A key with a passphrase that you type for every commit is safe but tiresome, so you will turn signing off. Agents and hardware keys resolve this tension. This chapter covers how each works, how to configure them, and what goes wrong.
Why the private key needs protection
A signature proves that someone with access to the private key made the commit. If an attacker copies your private key file, they can sign commits as you, and nothing in the signature will show the difference. Two defenses exist:
- A passphrase encrypts the private key file on disk. A stolen file is useless without it.
- A hardware-backed key keeps the private key inside a device. The key cannot be copied, only used while the device is attached.
An agent sits between you and the passphrase so you do not retype it constantly.
How the agent model works
An agent is a background process that holds a decrypted private key in memory. When Git needs a signature, it asks the signing program, which asks the agent. The agent signs the data and returns only the signature. Your private key never leaves the agent's memory, and you unlock it once per session or per cache period.
The two backends use different agents, so you configure them differently.
GPG: gpg-agent and pinentry
GnuPG starts gpg-agent automatically on first use. When a passphrase is needed, the agent launches a helper called pinentry, which shows the prompt. Pinentry comes in several forms: a terminal version (pinentry-curses), and graphical versions for GNOME, KDE, and macOS.
Setting GPG_TTY
The terminal version of pinentry needs to know which terminal to draw in. GnuPG learns this from the GPG_TTY environment variable. Git runs gpg as a child process with its standard input redirected, so gpg cannot detect the terminal on its own. Without GPG_TTY, you usually see:
error: gpg failed to sign the data
fatal: failed to write commit objectAdd this line to your shell startup file, such as ~/.bashrc or ~/.zshrc:
export GPG_TTY=$(tty)Test it outside Git so you can separate GnuPG problems from Git problems:
export GPG_TTY=$(tty)
echo "test" | gpg --clearsignIf this prints a signed message, GnuPG works and the problem is in your Git configuration. If it fails, fix GnuPG first.
Controlling the cache time
gpg-agent remembers an unlocked key for a limited time. You set this in ~/.gnupg/gpg-agent.conf:
default-cache-ttl 3600
max-cache-ttl 28800default-cache-ttl is the number of seconds the key stays unlocked after its last use. Each signature resets this timer. max-cache-ttl is a hard limit, counted from the first unlock, after which the agent asks again regardless of activity. The values above give a one-hour idle timeout and an eight-hour maximum.
Reload the agent after editing the file:
gpgconf --kill gpg-agentThe agent restarts the next time something needs it.
Choosing a pinentry program
If you work in a graphical session, a graphical pinentry avoids the GPG_TTY issue and shows a dialog. Name the program in gpg-agent.conf:
pinentry-program /usr/bin/pinentry-gnome3The path differs by system. On macOS with Homebrew it is often /opt/homebrew/bin/pinentry-mac. Use which pinentry-mac or your package manager to find it.
SSH: ssh-agent
SSH signing relies on ssh-agent, the same agent used for SSH logins. Git calls ssh-keygen -Y sign, which uses the agent if one is running and holds the key. If the key has a passphrase and is not loaded, ssh-keygen prompts for it directly.
Start an agent and load the key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_signingssh-add asks for the passphrase once. The key stays loaded until the agent exits, or until the timeout if you set one:
ssh-add -t 4h ~/.ssh/id_ed25519_signingThe -t 4h option removes the key after four hours. List what is loaded with ssh-add -l.
A quirk with the signing key setting
When you set user.signingkey to a path ending in .pub, Git passes that public key to ssh-keygen, which looks for the matching private key in the agent. This works well with agents, because the private file does not need to be readable at that path. You can also give a literal public key with the key:: prefix:
git config --global user.signingkey "key::ssh-ed25519 AAAAC3Nza... [email protected]"If you have several keys loaded and want to be sure Git uses the intended one, point at its .pub file or use this literal form.
Persisting the agent across sessions
Starting a fresh agent in every terminal means retyping the passphrase each time. Options by platform:
| Platform | Approach |
|---|---|
| macOS | Add `AddKeysToAgent yes` and `UseKeychain yes` to `~/.ssh/config`. The Keychain stores the passphrase. |
| Linux desktop | The desktop's keyring usually provides an agent. Check `echo $SSH_AUTH_SOCK`. |
| Linux, systemd user session | Enable a user `ssh-agent` service and export its socket. |
| Windows | Run the OpenSSH Authentication Agent service, then use `ssh-add`. |
A minimal config entry that loads keys on first use:
Host *
AddKeysToAgent yesThis setting loads a key into the agent the first time it is used. A signing operation does not always trigger it, so run ssh-add once explicitly if signing still prompts.
Hardware-backed keys
A hardware key stores the private key on a device such as a YubiKey or a smartcard. The device performs the signing internally. Malware on your computer can ask the device to sign while it is plugged in, but it cannot extract the key for later use elsewhere.
FIDO2 security keys with SSH
OpenSSH 8.2 and later support ed25519-sk keys, backed by FIDO2 devices. Generate one with:
ssh-keygen -t ed25519-sk -C "[email protected]" -f ~/.ssh/id_ed25519_sk_signingThe -sk types require you to touch the device when signing. Git then works as usual:
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519_sk_signing.pubEach commit now waits for a touch. This is the main benefit, since a remote attacker cannot sign without physical access. It is also the main annoyance when you rebase many commits, because each re-signed commit needs a touch.
You can add the -O resident option to ssh-keygen to store a key handle on the device itself, which lets you recover the key on another computer with ssh-keygen -K. Adding -O no-touch-required removes the touch requirement, but many hosting platforms and servers reject such keys, and it removes the protection you wanted.
OpenPGP smartcards
GnuPG can use the OpenPGP application on a YubiKey or a dedicated smartcard. The usual workflow is to create the key on a trusted machine, move the signing subkey to the card with gpg --edit-key and the keytocard command, and then delete the local copy. Check the result with:
gpg --list-secret-keys --keyid-format=longA secret key stored on a card is shown with ssb> instead of ssb. The > means only a stub remains on disk. When you sign, gpg-agent asks for the card's PIN, not a passphrase. The PIN is a separate secret with a retry limit, and the card locks after too many wrong attempts.
Choosing an approach
| Approach | Protection | Convenience | Main risk |
|---|---|---|---|
| No passphrase | None if the file leaks | Highest | Anyone with the file can sign as you |
| Passphrase with agent | Good against file theft | High after first unlock | Malware can use the unlocked key in memory |
| FIDO2 key (`ed25519-sk`) | Key cannot be copied; touch required | Touch for each signature | Lost device; many signatures in a rebase |
| OpenPGP smartcard | Key cannot be copied; PIN required | PIN caching is possible | Setup complexity; lost card needs a backup plan |
For most developers, a passphrase plus an agent is a reasonable default. Choose hardware keys if your threat model includes a compromised laptop or if your organization requires them.
Common failures
failed to sign the datawith GnuPG. CheckGPG_TTY, then run thegpg --clearsigntest shown earlier. If a graphical pinentry is configured but you are on SSH into a server, switch topinentry-curses.- Passphrase prompts keep appearing. The cache time is too short, or the agent restarts between commands. Check
default-cache-ttland make sure nothing kills the agent on each shell. Couldn't load public keyorNo such filefor SSH. The path inuser.signingkeyis wrong or points to a file that does not exist. Confirm it withls.Load key ... incorrect passphrase supplied. The agent does not hold the key, and the passphrase typed was wrong. Runssh-add -lto see what is loaded.- Signing hangs with a hardware key. The device is waiting for a touch, or it is not detected. Watch for a blinking light on the device.
- Works in the terminal, fails in an editor or GUI client. The graphical program does not inherit
SSH_AUTH_SOCKorGPG_TTY. Start it from a shell that has them set, or use a graphical pinentry.
Exercise
You set up an SSH signing key with a passphrase. Every git commit in a new terminal asks for the passphrase again, even though you typed it a few minutes ago in another terminal. Name the cause and describe two ways to fix it.
Show answer
Each terminal starts its own ssh-agent, or none at all, so the unlocked key is not shared between them. One fix is to use a single long-lived agent shared by all terminals, such as the desktop keyring agent, a systemd user service, or the macOS Keychain with UseKeychain yes and AddKeysToAgent yes in ~/.ssh/config. The other is to load the key once with ssh-add into an agent whose SSH_AUTH_SOCK is exported in your shell startup file, so every new terminal connects to the same agent.
Exercise
A teammate's git commit fails with gpg failed to sign the data, but gpg --list-secret-keys shows the right key. What is the first test you ask them to run, and what does each outcome tell you?
Show answer
Run export GPG_TTY=$(tty) and then echo "test" | gpg --clearsign. If it prints a signed message, GnuPG works, and the original failure was probably a missing GPG_TTY or a Git setting such as user.signingkey. If it still fails, the problem is in GnuPG itself, such as a missing or broken pinentry, an expired key, or an agent that is not running, and Git configuration is not the cause.