Navigate back to the homepage

How to backup and restore your PGP keys with GPG ?

Ashutosh Varma
August 20th, 2020 · 1 min read

Backup the public and secret keyrings and trust database

For minimal backup you only need to save your private key, assuming your public key is posted on keyservers like keyserver.ubuntu.com. But for complete backup of your keys you need to export both keys and ownertrust db.

To export your keys and ownertrust db, run the following commands on your terminal

1gpg --armor --export > pgp-public-keys.asc
2gpg --armor --export-secret-keys > pgp-private-keys.asc
3gpg --export-ownertrust > pgp-ownertrust.asc

Done! Make sure to keep your private key secured even if its protected with passphrase, revealing your private key reduces the security of your key to just passphrase.

Also while your are at it, you may as generate a revocation certificate:

For that you need you note you key ID in the second column after the slash: From below output, Key ID is 41650358D9338845.

1$ gpg --list-secret-keys --keyid-format LONG
2/home/oreki/.gnupg/pubring.kbx
3------------------------------
4sec rsa4096/41650358D9338845 2020-08-19 [SC]
5 ECE48C71E23CDFDC517165E841650358D9338845
6uid [ultimate] Ashutosh Varma (Ashutosh Varma's PGP Key) <ashutoshvarma11@live.com>
7ssb rsa4096/CA77E77152576307 2020-08-19 [E]

After that, generate the cerificate. You will be prompted several times and most probably you want to choose ”1 = Key has been compromised“.

1$ gpg --armor --gen-revoke 41650358D9338845 > pgp-revocation.asc
2
3sec rsa4096/41650358D9338845 2020-08-19 Ashutosh Varma (Ashutosh Varma's PGP Key) <ashutoshvarma11@live.com>
4
5Create a revocation certificate for this key? (y/N) y
6Please select the reason for the revocation:
7 0 = No reason specified
8 1 = Key has been compromised
9 2 = Key is superseded
10 3 = Key is no longer used
11 Q = Cancel
12(Probably you want to select 1 here)
13Your decision? 1
14Enter an optional description; end it with an empty line:
15> Key Compromised
16>
17Reason for revocation: Key has been compromised
18Key Compromised
19Is this okay? (y/N) y
20Revocation certificate created.
21
22Please move it to a medium which you can hide away; if Mallory gets
23access to this certificate he can use it to make your key unusable.
24It is smart to print this certificate and store it away, just in case
25your media become unreadable. But have some caution: The print system of
26your machine might store the data and make it available to others!

Restore your keys

1gpg --import pgp-public-keys.asc
2gpg --import pgp-private-keys.asc
3gpg --import-ownertrust pgp-ownertrust.asc

Revoke your key with revocation cerificate

If your key is compromised, you can revoke it in the same way

1gpg --import pgp-revocation.asc

Make sure to upload your revocation certificate to keyservers where your public keys are posted.

Where should I backup my PGP key ?

One great method is to print it as a QR code, but printing the plain text files generated above is also reasonable.

Uploading to a cloud service is the most convenient, but you are obviously implicitly trusting that provider with your private keys.

Or the most recommended way, store your keys in OpenGPG comapatible hardware tokens like Yubikey which might be the most secure way to save your secret keys.

Join our email list and get notified about new content

Be the first to receive our latest content with the ability to opt-out at anytime. We promise to not spam your inbox or share your email with any third parties.

More articles from Ashutosh Varma

Running Puppeteer in WSL2 ClearLinux

Recently gatsby-plugin-prefetch-google-fonts one of the Gatsby plugin that I have been using in this blog to pre-fetch google fonts…

December 20th, 2020 · 1 min read

Timeout on function call in Python

For the past few days I was working on dfit , a python module which find the best probability distribution for your observations. Luckily…

December 16th, 2020 · 3 min read
© 2020 Ashutosh Varma
Link to $https://github.com/ashutoshvarmaLink to $https://www.linkedin.com/in/varmaashutosh/Link to $https://stackoverflow.com/users/13944127/Link to $mailto:ashutoshvarma11@live.com