gpg — the key hierarchy, and every verb#
GnuPG 2.4.9 here. Pinentry is the GNOME dialog (NixOS programs.gnupg.agent), GPG_TTY is exported by core.zsh,
and gpgx (gpgx-cheat) is the no-flags wrapper for the daily lock/unlock/sign/verify. This card is the real thing underneath.
Terminal card: gpg-cheat — sections model setup keygen subkeys backup import sign verify encrypt decrypt edit revoke ssh git keyservers trust inspect offline agent fix mine.
FPR below = a full 40-hex fingerprint (gpg -K --with-colons | awk -F: '$1=="fpr"{print $10; exit}').
model — one primary key, several subkeys#
primary key [C] certify = the identity. Signs your own subkeys and user IDs, and other people's keys.
Keep it OFFLINE once the subkeys exist; it only comes out to add/revoke subkeys or UIDs.
subkey [S] sign = signs files, commits, mail. Rotate yearly without touching the identity.
subkey [E] encrypt = what others encrypt TO. Rotating it does not re-encrypt old mail; keep old ones to read it.
subkey [A] authenticate = SSH login (gpg-agent as the ssh agent).
user ID "Name <mail>" = one per address; the primary one is what people see first.
- Capabilities show in
gpg -Kas[SC],[E],[A]. A primary with[SC]signs with the identity key itself, which is fine but not rotatable. - The fingerprint (40 hex) is the key; the "key ID" is its last 16 (long) or 8 (short, collidable — never use).
sec= secret primary present.sec#= primary absent (offline), only subkeys here.ssb= secret subkey.- Ownertrust (how much you trust a key to vouch for others) is separate from validity (is this key really theirs).
[ultimate]is your own.
setup — ~/.gnupg and the agent#
# ~/.gnupg/gpg.conf (create it; sane modern defaults)
keyid-format 0xlong
with-fingerprint
with-subkey-fingerprints
default-key FPR
default-recipient-self # `gpg -e file` encrypts to you when no -r given
personal-cipher-preferences AES256 AES192 AES
personal-digest-preferences SHA512 SHA384 SHA256
cert-digest-algo SHA512
no-emit-version
no-comments
keyserver hkps://keys.openpgp.org
auto-key-locate local,wkd # find a key by mail address (WKD) before asking a keyserver
trust-model tofu+pgp # remember first-seen keys per address, warn on change
# ~/.gnupg/gpg-agent.conf
default-cache-ttl 3600 # seconds a passphrase stays cached after last use
max-cache-ttl 28800 # hard ceiling
# pinentry-program is set by NixOS (modules/hosts/laptop/configuration.nix: pinentryPackage = pinentry-gnome3)
chmod 700 ~/.gnupg # gpg refuses "unsafe permissions" otherwise
gpg-connect-agent reloadagent /bye # after editing gpg-agent.conf
gpgconf --kill gpg-agent # restart it entirely (it relaunches on demand)
gpg --version # algorithms available
keygen — a proper key, the modern way#
Certify-only primary, then three subkeys. Ed25519/Curve25519 throughout (fast, small, the 2.4 default).
gpg --quick-generate-key 'Your Name <you@example.com>' ed25519 cert 2y # primary: [C] only, 2-year expiry
FPR=$(gpg -K --with-colons 'you@example.com' | awk -F: '$1=="fpr"{print $10; exit}')
gpg --quick-add-key "$FPR" ed25519 sign 1y # [S]
gpg --quick-add-key "$FPR" cv25519 encr 1y # [E]
gpg --quick-add-key "$FPR" ed25519 auth 1y # [A] (for SSH)
gpg -K --with-subkey-fingerprints # see the four, check capabilities and expiries
- Expiry is a safety net, not a deadline: extend it any time with the primary key (see edit). A key that expires with its owner gone is self-cleaning.
- Interactive version:
gpg --full-generate-key --expert→(11) ECC (set your own capabilities)→ toggleSoff so onlyCertifyremains →Curve 25519. Then add subkeys with--quick-add-keyor in--edit-key→addkey. - A passphrase protects the secret key file; the agent caches it. Use a long one; the pinentry dialog asks.
- The revocation certificate is written automatically to
~/.gnupg/openpgp-revocs.d/FPR.rev— back it up with the key (see backup).
subkeys — add, rotate, drop#
gpg --quick-add-key FPR ed25519 sign 1y # new signing subkey (old one stays until you revoke it)
gpg --quick-set-expire FPR 1y '*' # extend EVERY subkey a year (`'*'` = all subkeys; a sub-fpr = that one)
gpg --quick-set-expire FPR 2y # extend the primary
gpg --edit-key FPR # interactive: `key 2` selects subkey 2 (the `*` marks it), then
# `expire` / `revkey` / `delkey` / `passwd` / `save`
gpg -K --with-colons | awk -F: '$1=="ssb"{print $12, $5}' # capability letters + key id of each subkey
Old encryption subkeys: keep them (revoked or expired, still in the ring) or you lose the ability to read what was sent to them.
backup — export, revocation, paper#
gpg --armor --export FPR > mykey.pub.asc # public key: share freely
gpg --armor --export-secret-keys FPR > mykey.SECRET.asc # everything: primary + subkeys (passphrase-protected)
gpg --armor --export-secret-subkeys FPR > mykey.subkeys.asc # subkeys only (what a laptop should carry; see *offline*)
cp ~/.gnupg/openpgp-revocs.d/FPR.rev mykey.rev # the revocation certificate
gpg --export-ownertrust > ownertrust.txt # your trust assignments
gpg --gen-revoke FPR > mykey.rev # make a fresh revocation cert by hand
nix shell nixpkgs#paperkey -c sh -c 'gpg --export-secret-keys FPR | paperkey > mykey.paper.txt' # printable secret bits
Where it goes: the vault (encrypted), never the dotfiles repo. The SECRET export + the .rev are the two files that matter. The secret export is still encrypted with your passphrase; the passphrase is the third thing to keep.
import — keys, trust, restore#
gpg --import mykey.pub.asc # someone's public key (or your own on a new machine)
gpg --import mykey.SECRET.asc # restore: asks for the passphrase
gpg --import-ownertrust < ownertrust.txt
gpg --edit-key FPR trust # set ownertrust: 5 = ultimate (yours), 4 = full, 3 = marginal
gpg --lsign-key FPR # "I checked this key": local signature, never exported
gpg --sign-key FPR # exportable certification (web of trust)
gpg --show-keys someone.asc # look at a key file WITHOUT importing it
gpg --delete-keys FPR # forget a public key; --delete-secret-keys for the secret part first
On a fresh machine: import the SECRET file, gpg --edit-key FPR trust → 5, done. Or import only the subkeys file for a laptop.
sign — files, text, commits#
gpg --detach-sign --armor file # file.asc beside it: the usual way to sign a release/file
gpg --detach-sign file # binary file.sig
gpg --clearsign message.txt # message.txt.asc: readable text with a signature block (mail, announcements)
gpg --sign file # file.gpg: compressed file + signature in one (recipient runs gpg -d)
gpg -u FPR --detach-sign file # pick the key (-u / --local-user); `FPR!` = that exact subkey
echo "text" | gpg --clearsign # from a pipe
gpg --sign --encrypt -r mail file # sign AND encrypt (see *encrypt*)
Signing uses the [S] subkey automatically (or the primary if it has S). The agent asks for the passphrase once per cache TTL.
verify — did this come from them, unchanged#
gpg --verify file.asc file # detached signature (.asc/.sig) + the file
gpg --verify file.sig # gpg finds `file` next to it
gpg --verify message.txt.asc # clearsigned text
gpg --decrypt file.gpg > file # inline-signed: extracts the file and prints the verification
gpg --verify --verbose file.asc file # which key, which subkey, when
Reading the result: Good signature from "Name <mail>" = cryptographically valid. The warning
This key is not certified with a trusted signature means you have not set ownertrust / signed their key
— the signature is still valid; the question is whether the key is really theirs. Compare the fingerprint
out-of-band once, then gpg --lsign-key FPR and the warning goes away.
BAD signature = the file changed or the signature is for another file. Exit code 0 only on Good.
encrypt — to people, to yourself, with a passphrase#
gpg --encrypt --recipient mail@example.com --armor file # file.asc, readable only by that key
gpg -e -r mail@example.com -r you@example.com file # several recipients (add yourself to read it later!)
gpg -e file # to yourself (default-recipient-self in gpg.conf)
gpg -se -r mail file # sign + encrypt: they know it is from you
gpg --symmetric --cipher-algo AES256 file # passphrase only, no keys (file.gpg); shares via a channel you trust
gpg -c --armor file # same, armored
gpg -o out.gpg -e -r mail file # choose the output name
tar cz directory | gpg -e -r mail > directory.tgz.gpg # a whole directory, through a pipe
gpg --hidden-recipient mail -e file # do not reveal who it is for (-R)
gpg --encrypt-to FPR # in gpg.conf: ALWAYS add this recipient (yourself) silently
--armor(-a) makes ASCII text you can paste into mail; without it the output is binary (smaller).- Encrypt uses the recipient's
[E]subkey. "No public key" = you have not imported theirs; "unusable public key" = theirs expired. - Symmetric + a strong passphrase is fine for backups and for sending to someone with no key.
decrypt — and what to do when it fails#
gpg --decrypt file.gpg > file # -d: to stdout
gpg -o file -d file.gpg # to a named file
gpg file.gpg # guesses: decrypts (or verifies) and writes `file`
gpg --decrypt-files *.gpg # many at once, each to its name without .gpg
gpg -d file.gpg | tar xz # straight into tar
gpg --list-packets file.gpg # WHO can decrypt this: the key IDs it was encrypted to, the algorithms
"decryption failed: No secret key" = it was not encrypted to any key you hold (check with --list-packets;
compare with gpg -K). On a laptop with subkeys only, that is fine as long as the [E] subkey is there.
edit — identities, passphrase, expiry#
gpg --quick-add-uid FPR 'Your Name <you@example.com>' # add a user ID (fix a name, add an address)
gpg --quick-set-primary-uid FPR 'Your Name <you@example.com>'
gpg --quick-revoke-uid FPR 'Old Name <you@example.com>' # retire a UID (keys cannot delete published UIDs; revoke them)
gpg --change-passphrase FPR # new passphrase for the secret key
gpg --quick-set-expire FPR 2y # primary expiry; `0` = never
gpg --quick-set-expire FPR 1y '*' # all subkeys
gpg --edit-key FPR # the interactive editor; `help` lists everything:
# uid N / key N select · adduid deluid revuid primary · addkey delkey revkey expire · passwd · trust · save / quit
Every change to UIDs or subkeys needs the primary secret key present (sec, not sec#). Then re-export the public key: others must re-import it to see the change.
revoke — when a key is lost or compromised#
gpg --import mykey.rev # the stored revocation certificate: marks YOUR key revoked locally
gpg --keyserver hkps://keys.openpgp.org --send-keys FPR # publish the revocation so others see it
gpg --edit-key FPR → key 2 → revkey → save # revoke ONE subkey (compromised laptop: revoke its subkeys, keep the primary)
gpg --quick-revoke-uid FPR 'uid string' # revoke an identity
Revoked ≠ deleted: the key stays in the ring so old signatures still verify (as "made with a revoked key") and old mail still decrypts. If the laptop is lost and you kept the primary offline: revoke its subkeys, make new ones, publish. The identity survives.
ssh — the [A] subkey as your SSH key#
# modules/hosts/laptop/configuration.nix: programs.gnupg.agent.enableSSHSupport = true; # gpg-agent becomes the ssh agent (SSH_AUTH_SOCK)
gpg -K --with-keygrip # the keygrip of the [A] subkey
echo KEYGRIP >> ~/.gnupg/sshcontrol # tell the agent to serve it
gpg --export-ssh-key FPR # the public key in authorized_keys format
gpg --export-ssh-key FPR >> ~/.ssh/authorized_keys_for_that_host # paste it where you log in
ssh-add -L # the agent now lists it
Your file key ~/.ssh/id_ed25519 keeps working next to it; ssh tries agent keys first. Use one or the other per host in ~/.ssh/config (IdentitiesOnly yes + IdentityFile).
git — signed commits and tags#
git config --global gpg.format openpgp
git config --global user.signingkey FPR # or a subkey fpr with `!` to force it
git config --global commit.gpgsign true # every commit
git config --global tag.gpgSign true
git commit -S -m "msg" # one-off when gpgsign is off
git tag -s v1.0 -m "release" # signed tag; git tag -v v1.0 verifies
git log --show-signature -3 # see who signed what
git verify-commit HEAD
gpg --armor --export FPR | wl-copy # paste into GitHub/GitLab → Settings → GPG keys, so they show "Verified"
jj: jj config set --user signing.behavior own + signing.backend "gpg" + signing.key FPR signs the commits jj makes.
keyservers — publishing and finding keys#
gpg --keyserver hkps://keys.openpgp.org --send-keys FPR # publish; keys.openpgp.org mails you to verify the address before it shows the UID
gpg --keyserver hkps://keys.openpgp.org --recv-keys FPR # fetch by fingerprint (the only safe way to fetch)
gpg --locate-keys someone@example.com # WKD: their domain serves the key (auto-key-locate in gpg.conf)
gpg --search-keys 'name' # interactive search; verify the fingerprint with them before trusting
gpg --refresh-keys # pull revocations/expiry updates for every key you hold
trust — validity versus ownertrust#
- A signature is valid when the key is valid. A key is valid when you certified it (
--sign-key/--lsign-key) or enough trusted people did. - Ownertrust (
--edit-key FPR trust, 1-5) is how much you let that person's certifications count for other keys. Give 5 only to yourself. trust-model tofu+pgp: the first key seen for an address is remembered; a different one later triggers a warning.gpg --tofu-policy good FPRto accept a change.gpg --check-signatures FPRlists who certified a key.gpg --update-trustdbrecomputes validity after trust changes.
inspect — what is this thing#
gpg -k # public keys (--list-keys); gpg -K = secret keys
gpg -k --with-subkey-fingerprints --with-keygrip FPR
gpg --fingerprint FPR # just the fingerprint, formatted for reading aloud
gpg --list-packets file.gpg # structure of any OpenPGP file: recipients, algorithms, signature dates
gpg --show-keys key.asc # describe a key file without importing
gpg -k --with-colons FPR # machine-readable (pub/sub/uid/fpr records)
gpg --card-status # a YubiKey/OpenPGP card, if one is plugged in
gpg --export FPR | gpg --list-packets | grep -A2 'signature packet' # certifications on your key
offline — primary key off the laptop#
The point of subkeys: the laptop carries only [S] [E] [A]; the primary (the identity) lives on encrypted offline storage.
gpg --armor --export-secret-keys FPR > mykey.SECRET.asc # 1. full backup first (vault, encrypted USB)
gpg --armor --export-secret-subkeys FPR > subkeys.asc # 2. the laptop's share
gpg --delete-secret-keys FPR # 3. remove everything secret here
gpg --import subkeys.asc # 4. put the subkeys back
gpg -K # shows `sec#` = primary absent, `ssb` present: correct
To add a subkey / extend expiry / sign someone's key later: import the SECRET backup in a temporary GNUPGHOME, do the change, export the public key and subkeys again, wipe the temp dir:
export GNUPGHOME=$(mktemp -d); gpg --import mykey.SECRET.asc; gpg --quick-set-expire FPR 2y '*'
gpg --armor --export FPR > pub.asc; gpg --armor --export-secret-subkeys FPR > subkeys.asc; rm -rf "$GNUPGHOME"; unset GNUPGHOME
gpg --import pub.asc subkeys.asc # back in the normal ring
A YubiKey does the same with the subkeys moved onto the card: gpg --edit-key FPR → key N → keytocard.
agent — passphrase caching, pinentry#
gpg-connect-agent 'KEYINFO --list' /bye # which keys are cached right now (the `1` column)
gpg-connect-agent reloadagent /bye # re-read gpg-agent.conf
gpgconf --kill gpg-agent # forget every cached passphrase now
gpg --pinentry-mode loopback -d file.gpg # type the passphrase in the terminal instead of the dialog (what gpgx does)
echo test | gpg --clearsign >/dev/null # cheap way to pre-unlock the key for a batch of operations
export GPG_TTY=$(tty) must be set for terminal pinentries (core.zsh does it). The dialog here is pinentry-gnome3.
fix — the usual errors#
Inappropriate ioctl for device→export GPG_TTY=$(tty)(or the session has no tty: use--pinentry-mode loopback).No secret keyon decrypt → not encrypted to you:gpg --list-packets file.gpgshows the key IDs it was made for.unusable public key/unusable secret key→ expired or revoked (sub)key:gpg -k FPR, then--quick-set-expire(yours) or ask for a fresh key (theirs).There is no assurance this key belongs to the named user→ ownertrust:gpg --lsign-key FPRafter checking the fingerprint, or--trust-model alwaysfor one command.gpg: WARNING: unsafe permissions on homedir→chmod 700 ~/.gnupg; chmod 600 ~/.gnupg/*.- Commands hang → a stale lock or a stuck daemon:
gpgconf --kill all,rm -f ~/.gnupg/*.lock ~/.gnupg/public-keys.d/*.lock. signing failed: No pinentry→ agent cannot reach a pinentry:gpgconf --kill gpg-agentand retry from a terminal withGPG_TTYset.- Git:
error: gpg failed to sign the data→ almost always the two above;GIT_TRACE=1 git commitshows gpg's own message. - Key shows
[expired]on the other side after you extended it → they need your re-exported public key (or--refresh-keys).
mine — the key this repo signs with#
- The laptop's identity (name, email, fingerprint) lives in one file,
modules/hosts/laptop/_identity.nix; git.nix and gpg.nix read it. On the generic host there is no key configured: make one (create) and set it in your own git config. - No revocation certificate stored yet?
gpg --gen-revoke FPR > mykey.rev, and keep it offline. - Optional upgrades that keep the identity: an expiry (
--quick-set-expire), a[S]signing subkey and an[A]auth subkey (--quick-add-key … rsa4096 sign|auth), the primary offline (offline). - Pinentry: GNOME dialog (modules/hosts/laptop/configuration.nix).
gpgxuses loopback so the passphrase is typed in the terminal instead. - Secrets in the NixDaemon repo are age, not gpg (
nix-cheat secrets). gpgx-cheatis the wrapper's card;gpg-cheat SECTIONprints one section of this one.