Skip to content

Instantly share code, notes, and snippets.

@dragonly
Forked from AriTheElk/gpg_key_backup.md
Created March 11, 2022 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dragonly/56a8eb457a7d569c2517276344b3a43d to your computer and use it in GitHub Desktop.
Save dragonly/56a8eb457a7d569c2517276344b3a43d 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.

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