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
- List public keys:
gpg --list-keys - List secret keys:
gpg --list-secret-keys - Show subkey fingerprints as well:
gpg --list-keys --with-subkey-fingerprints gpg --list-secret-keys --with-subkey-fingerprints
The key ring location is normally shown on the first line of the output.
Use a different key ring
- For one command only:
gpg --homedir ~/.gnupg-alternate --list-keys - For the whole session:
export GNUPGHOME=/path/to/gnupg/ gpg --list-keys
Generate a key pair
gpg --full-generate-key
Export keys
- Export a single public or secret key (useful for backups):
gpg -a --export KEYID > public.asc gpg -a --export-secret-key KEYID > secret.asc - Export all keys:
gpg -a --export > public-all.asc gpg -a --export-secret-key > secret-all.asc
Exported secret keys are protected by the passphrase of the current secret key.
Import keys
- List the contents of a key file without importing it:
gpg --show-keys keys.asc - Show fingerprints and signatures:
gpg --with-subkey-fingerprint keys.asc gpg -v keys.asc - Import keys into the current key ring:
gpg --import keys.asc
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
- Remove a local signature:
gpg --edit-key KEYID gpg> delsig gpg> save - Revoke a published signature (a signature cannot be deleted):
gpg --edit-key KEYID gpg> revsig gpg> save - If needed, upload the key and the revocation certificate to a key server:
Or export the key with the revoked signature:gpg --send-key KEYIDgpg -a --export KEYID > exported-key.asc
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
- Encrypt a file for one recipient (default output file:
file.txt.gpg):gpg -e -r KEYID file.txt - Sign and encrypt:
gpg -s -e -r KEYID file.txt - Encrypt for several recipients:
gpg -e -r KEY1 -r KEY2 -r KEY3 file.txt - Specify the output file:
gpg -e -r KEYID -o OUTPUT INPUT - Compression is enabled by default. To disable it (faster for large files that are already compressed):
gpg -e -z 0 -r KEYID file.tar.gz - Encrypt from standard input:
cat "my secret message" | gpg -e -r KEYID > message.txt.gpg tar -jc /var/log/secret | gpg -z 0 -e -r KEYID > secret.tar.bz2.gpg - Symmetrically encrypt a file with a passphrase:
gpg -c file.txt
Create and verify signatures
- Sign a file without encrypting it, using a detached signature (writes
file.txt.asc):gpg -a -s file.txt - Clear-sign a file:
gpg --clear-sign file.txt - Sign with a non-default secret key (useful with several secret keys):
gpg --default-key KEYID -a -s file.txt - Verify a clear-signed or detached signature:
gpg --verify file.txt.asc
Decryption
- List the recipients of an encrypted file:
gpg --list-only FILE - Decrypt to a chosen output file:
gpg -d -o OUTPUT FILE - Decrypt to the default name (
file.txt.gpgbecomesfile.txt):gpg -d FILE
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.
- Send (upload) a key to a key server:
gpg --keyserver SERVER --send-key KEYID - Receive a key from a key server:
gpg --keyserver SERVER --recv-key KEYID - Search for keys:
gpg --keyserver SERVER --search-keys STRING - Without the
--keyserveroption, the default server is used. To see which one it is:gpgconf --list-options gpg
Common public key servers include pgp.mit.edu and pool.sks-keyservers.net.
Notes
- A local signature is only valid for you and is not included in exported keys.
- Reserve the "ultimate" trust level for keys you generated yourself.
- An exported secret key remains protected by its passphrase: keep it in a safe place.