Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MohammedEsafi/6768ab29e3de4075f3dbb1406df6adef to your computer and use it in GitHub Desktop.

Select an option

Save MohammedEsafi/6768ab29e3de4075f3dbb1406df6adef to your computer and use it in GitHub Desktop.
Instructions to export, archive, and restore GPG keys

Managing your GPG keys: export and import guide

Exporting GPG keys


1. List your GPG keys

To identify the keys you want to export, run:

gpg --list-secret-keys --keyid-format=long

Example output:

sec   rsa4096/ABCDEFGH12345678 2023-01-01 [SC]
      Key fingerprint = 1234 5678 9ABC DEFG HIJK LMNO PQRS TUVW XYZA BCDE
uid           [ultimate] Your Name <your@email.com>
ssb   rsa4096/IJKLMNOP12345678 2023-01-01 [E]

2. Export the private key

Replace ABCDEFGH12345678 with your key ID and run:

gpg --export-secret-keys --armor ABCDEFGH12345678 > gpg-private-key.asc

3. Export the public key

gpg --export --armor ABCDEFGH12345678 > gpg-public-key.asc

4. Export the trust database

gpg --export-ownertrust > gpg-trust.txt

5. Archive the exported files

To keep your backup organized and easy to transfer, you can compress the exported files into a single archive:

tar -czvf gpg-keys-backup.tar.gz gpg-private-key.asc gpg-public-key.asc gpg-trust.txt

Importing and restoring GPG keys


If you backed up your keys as an archive, extract them first:

tar -xzvf gpg-keys-backup.tar.gz

Then import the keys:

gpg --import gpg-public-key.asc
gpg --import gpg-private-key.asc
gpg --import-ownertrust < gpg-trust.txt

Verify your keys:

gpg --list-secret-keys

Now you're ready to use your GPG keys again! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment