I have a few backed up public and private key files. How can I check which public key file corresponds to which private key file?

I had generated 2048 byte public and private GnuPG key pairs using

gpg --gen-key

To backup the public key(s), I exported them using

gpg --armor --output ~/gpg_keys_backup/<Public Key Id>-public.key --export <Public Key Id>

To backup the private key(s), I exported them using

gpg --armor --output ~/gpg_keys_backup/<Private Key ID>-private.key --export-secret-keys <Private Key ID>

How do I determine which backed up keys belong to a pair?


By listing the secret key file's contents, you can query which public key a secret key belongs to.

gpg --list-packets [secret-key-file] | head
:secret key packet:
        version 4, algo 1, created 1356475387, expires 0
        pkey[0]: [8192 bits]
        pkey[1]: [17 bits]
        gnu-dummy S2K, algo: 0, simple checksum, hash: 0
        protect IV: 
        keyid: 4E1F799AA4FF2279

The last line quoted keyid contains the public key's long key ID.

  • you can use the same command on any encrypted file to find out which key you need to decrypt it. – Tapper Jun 12 '17 at 16:33

First of all, the public key ID and private key ID are the same, so your filenames should have matched up to begin with.

Second, you actually don't need to backup the public key. Just import the private key (which contains both the private and public key), and both the private and public keys will imported.

gpg --import private.key

But assuming the filenames don't match for some reason, then you can run

gpg yourfile.key , which will print something like:

pub  2048R/1234ABCD 2016-12-31
sub  2048R/5678EFGH 2016-12-31

Match the key IDs of the private/public keys (in this example 1234ABCD).

If you have a ton of keys, instead of matching them, it might be easier to simply ignore your exported public keys and regenerate them from your private keys.

gpg --import private.key
gpg --armor --output <Key Id>-public.key --export <Key Id>
  • Interesting information! I'll try this on my next reinstall. – PJ Singh Aug 4 '17 at 0:11

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.