LLI Wiki — My Digital Knowledge Base

GnuPG (PGP) cheat sheet

GnuPG commands to manage keys, encrypt, sign and verify files.

A GnuPG (PGP) quick reference for managing keys, encrypting and signing files. Commands use KEYID for a key identifier.

List keys

The key ring location is normally shown on the first line of the output.

Use a different key ring

Generate a key pair

gpg --full-generate-key

Export keys

Exported secret keys are protected by the passphrase of the current secret key.

Import keys

Sign a key

After confirming that the key is authentic by checking its fingerprint, sign it:

gpg --fingerprint KEYID
gpg --sign-key KEYID

Or with the key editor:

gpg --edit-key KEYID
gpg> fpr
gpg> sign
gpg> save

Optionally, export the signed key again:

gpg -a --export KEYID > signed-key.asc

Signing a key automatically sets its trust level to full.

A local signature (--lsign-key) is not included in the key exported to others: it is only valid for you.

gpg --lsign-key KEYID

Remove a key signature

Add or remove a UID

gpg --edit-key KEYID
gpg> adduid
(enter details as prompted)
gpg> list
gpg> save
gpg> quit

Change the primary UID

If the key has several UIDs:

gpg --edit-key KEYID
gpg> list
gpg> uid X
gpg> primary
gpg> save
gpg> quit

Edit key trust

gpg --edit-key KEYID
gpg> trust
gpg> (enter trust level)
gpg> save
Level Meaning
1 I don't know or won't say
2 I do NOT trust
3 I trust marginally
4 I trust fully
5 I trust ultimately
m Back to the main menu

Use "ultimate" only for keys you generated yourself.

Revocation certificate

gpg -a --gen-revoke KEYID > revoke.asc

Renew an expired subkey

To change the expiry date:

gpg --edit-key KEYID
gpg> expire
gpg> key 1
gpg> expire
gpg> list
gpg> save

For other subkeys, use key 2, key 3, and so on. list shows the expiry dates. Without key N, the primary key is edited (the first key in the list, also selectable with key 0).

Then export the new public key and generate a new revocation certificate to keep. The secret key does not change.

gpg -a --export KEYID > public.asc
gpg -a --gen-revoke KEYID > revoke.asc

Encryption

Create and verify signatures

Decryption

Batch encryption and decryption

Encrypt all *.jpg files in the current directory for two recipients, without compression:

find . -maxdepth 1 -type f -name "*.jpg" -exec gpg -z 0 -e -r KEY1 -r KEY2 -o {}.gpg {} \;

Decrypt all *.gpg files in the current directory (without --output, file.txt.gpg is written to file.txt):

gpg --decrypt-files *.gpg

The same with a shell script:

#!/bin/bash
read -rsp "Enter passphrase: " PASSPHRASE
for FILE in *.*.gpg; do
    echo "Extracting $FILE to ${FILE%.gpg}."
    echo "$PASSPHRASE" | gpg --passphrase-fd 0 --batch -d --output "${FILE%.gpg}" "$FILE"
done

Decrypt with a passphrase read from standard input:

echo "passphrase" | gpg --passphrase-fd 0 --batch -d -o file.txt file.txt.gpg

Key servers

KEYID can be the last 8 hexadecimal characters of the fingerprint, for example ABCD1234.

Common public key servers include pgp.mit.edu and pool.sks-keyservers.net.

Notes

Mastodon LLIMastodon SocialFosstodon