Skip to content

Instantly share code, notes, and snippets.

@AriTheElk
Last active June 4, 2022 11:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AriTheElk/29cab22251d1913eee9556ff5b286052 to your computer and use it in GitHub Desktop.
Save AriTheElk/29cab22251d1913eee9556ff5b286052 to your computer and use it in GitHub Desktop.
Backup/Restore GPG key

The following is the procedure I use on UNIX systems:

First, export all public certificates into a public keyring:

$ gpg --armor --export > pub.asc

Second, export all secret certificates into a secret keyring:

$ gpg --armor --export-secret-keys > priv.asc

Third, export ownertrust values and save those:

$ gpg --armor --export-ownertrust > trust.asc

Fourth, copy all the *.conf files in ~/.gnupg into your current

directory:

$ cp ~/.gnupg/*.conf .

Fifth, put these, and all your GnuPG .conf files, all into a

single archive:

$ tar cJf gpg-backup.txz pub.asc priv.asc trust.asc *.conf

Copy gpg-backup.txz to the new machine. Once you've done that,

uncompress it on the new machine:

$ tar xJf gpg-backup.txz

Import your secret certificates:

$ gpg --import < priv.asc

Import your public certificates:

$ gpg --import < pub.asc

Import your ownertrust values:

$ gpg --import-ownertrust < trust.asc

Make sure your ~/.gnupg directory exists. If it doesn't, run gpg

with no arguments and hit Ctrl-C to break out of it.

$ gpg

Copy your .conf files into ~/.gnupg:

$ cp *.conf ~/.gnupg

... And at that point you should be done. This technique should

work regardless of whether you're migrating from 1.4 to 2.0, 1.4 to

2.1, 2.0 to 1.4, 2.0 to 2.1, 2.1 to 2.0, or 2.1 to 1.4. No matter

which you're doing, you're covered.

@Donaldson7
Copy link

Bad tools

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