Keeping a GPG primary key offline Jump to heading
A GPG identity is only as safe as its primary key. Whoever holds it can create new subkeys, extend expiry dates, add user IDs and sign other peopleโs keys โ everything needed to impersonate you indefinitely. Yet for day-to-day Git signing, the primary is never used: a signing subkey does the work. Keeping the primary on the laptop therefore buys no convenience and exposes the one key whose loss is unrecoverable. This page moves it offline and documents the brief, infrequent procedure for using it. It assumes the subkey layout from setting up GPG commit signing with subkeys and is part of protecting and rotating signing keys.
When to use this approach Jump to heading
- You sign with GPG and your primary keyโs secret part is currently on an everyday machine.
- Your key is published, cross-signed, or trusted by a release process, so replacing it would be disruptive.
- You can tolerate a short manual step once or twice a year for rotation and expiry extension.
- If your key is personal, unpublished and trivially replaceable, the effort may not be worth it; SSH signing with per-device keys is simpler, as in using a separate signing key per device.
Step 1 โ Confirm the layout before moving anything Jump to heading
You need a certify-only primary and at least one signing subkey. If the primary also has the sign capability and Git is pointed at it, fix that first, or removing the primary will break signing.
gpg --list-secret-keys --keyid-format long --with-subkey-fingerprints
# sec ed25519/0xA1B2C3D4E5F60718 [C] <- certify only: good
# ssb ed25519/0x1122334455667788 [S] <- signing subkey: good
git config --get user.signingKey # must be the [S] subkey, with ! Step 2 โ Export everything to offline storage Jump to heading
Export the full secret key, the public key and a revocation certificate. These three files are the recovery kit. Write them to encrypted removable media โ two copies, stored separately.
FPR=A1B2C3D4E5F60718... # full primary fingerprint
umask 077
mkdir -p /media/offline/gpg-$(date +%F)
cd /media/offline/gpg-$(date +%F)
gpg --armor --export-secret-keys "$FPR" > secret-primary-and-subkeys.asc
gpg --armor --export "$FPR" > public.asc
gpg --output revoke.asc --gen-revoke "$FPR"
sha256sum *.asc > SHA256SUMS # Verification: the export restores into an empty keyring
GNUPGHOME=$(mktemp -d) sh -c 'gpg --import secret-primary-and-subkeys.asc && gpg -K --keyid-format long' Do not skip that verification. The next step deletes the primary from the laptop, and an untested backup is not a backup.
Step 3 โ Remove the primary secret from the laptop Jump to heading
GnuPG has no single command to delete only the primaryโs secret part, so the standard method is to delete all secret keys for the identity and re-import only the subkeys.
gpg --armor --export-secret-subkeys "$FPR" > /tmp/subkeys.asc
gpg --delete-secret-keys "$FPR" # confirms interactively โ read the prompt
gpg --import /tmp/subkeys.asc
shred -u /tmp/subkeys.asc
gpg --list-secret-keys --keyid-format long
# sec# ed25519/0xA1B2C3D4E5F60718 [C] <- '#' means the secret part is absent โ ๏ธ SAFETY WARNING:
gpg --delete-secret-keyspermanently removes secret material from this keyring. Run it only after Step 2โs restore test passed. If you delete with a broken backup, the identity cannot be recovered: you would have to revoke it with the revocation certificate (if that copy works) and start again. Keep the media mounted until you have confirmed thesec#output and a successful test commit.
# Verification: signing still works with only the subkey present
git commit --allow-empty -m "post-offline signing test" && git log -1 --format='%G? %GK' Step 4 โ Use the primary when you need it, then put it away Jump to heading
You need the primary to add or revoke subkeys, extend expiry, add a user ID or certify someone elseโs key. Do it in a throwaway keyring, ideally on a machine that is offline for the duration.
export GNUPGHOME=$(mktemp -d)
gpg --import /media/offline/gpg-*/secret-primary-and-subkeys.asc
gpg --quick-set-expire "$FPR" 2y # extend the primary
gpg --quick-add-key "$FPR" ed25519 sign 1y # add next year's subkey
gpg --armor --export "$FPR" > public-updated.asc
gpg --armor --export-secret-subkeys "$FPR" > subkeys-updated.asc
gpg --armor --export-secret-keys "$FPR" > /media/offline/gpg-$(date +%F)-secret.asc
rm -rf "$GNUPGHOME"; unset GNUPGHOME Then, on the laptop, import subkeys-updated.asc, point user.signingKey at the new subkey, and upload public-updated.asc to your forge.
Step 5 โ Write the procedure down where you will find it Jump to heading
The ceremony happens so rarely that nobody remembers it. Keep a one-page note with the offline media: what the files are, the fingerprint, the commands above, and where the second copy is. Put a calendar reminder a month before the subkey expires, as described in setting expiry and renewal reminders for signing keys.
Validation checklist Jump to heading
Frequently Asked Questions Jump to heading
Can I use a hardware key instead of offline media? Jump to heading
Yes. Moving the subkeys to a hardware token and keeping the primary on offline media is the strongest common setup. Some people also keep the primary on a second token, which avoids storing a file but makes the backup a device rather than a document.
What if I lose both offline copies? Jump to heading
The primary is gone, but signing with existing subkeys continues until they expire. Use that time to create a new identity and publish the revocation certificate for the old one, if you still have it. This is why the revocation certificate is worth storing in more places than the secret key.
Does the forge need the primary key to verify signatures? Jump to heading
No. The forge needs only the public key, which contains the primaryโs public part and the subkeys. Verification never touches any secret material.
Related Jump to heading
- Protecting & Rotating Signing Keys โ the parent topic.
- Revoking a GPG Key and Publishing the Revocation โ what the revocation certificate in the kit is for.
- Backing Up Signing Keys Without Creating a Leak โ choosing where those offline copies go.
- Rotating a Compromised Commit-Signing Key โ the emergency version of the ceremony.