Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I recently lost my .gnupg folder. Luckily I had a backup of my private keys, however I'd like to reimport the public keys which have signed my key. How do I easily import all those - preferably at once?

pub   2048R/52AE9CEE 2012-01-20 [expires: 2015-02-09]
uid                  Thom Wiggers <antispamsnip>
sig 3        52AE9CEE 2012-01-20  Thom Wiggers <antispamsnip>
sig          2EF8C903 2012-12-04  [User ID not found]
sig 1        7E8162D1 2012-12-01  [User ID not found]
sig 2        EC362321 2012-11-29  [User ID not found]
share|improve this question

migrated from security.stackexchange.com Mar 11 '13 at 16:14

This question came from our site for Information security professionals.

add comment

2 Answers 2

gpg --list-sigs | grep 'ID not found' | perl -nwe '/([0-9A-F]{8})/ && print "$1\n"' | xargs gpg --recv-keys

This should do what you want. It finds each missing user ID, then extracts the key ID and builds a list of public keys to fetch from the keyserver.

share|improve this answer
add comment

To import a public key in your keyring, you need to have that public key. Since you lost your .gnupg folder, you also lost the local copies of the public keys you want to reimport, so you have to retrieve them "elsewhere". Public keys of other people are usually found on their Web pages, attached to signed emails they sent you, or on public key servers.

If the public keys you are after are indeed on key servers, then use gpg --recv-keys to retrieve them and import them in your keyring. E.g.:

$ gpg --recv-keys 2EF8C903
gpg: requesting key 2EF8C903 from hkp server pool.sks-keyservers.net
gpg: key 2EF8C903: public key "Luuk Scholten <antispamsnip>" imported
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   7  signed:   4  trust: 0-, 0q, 0n, 0m, 0f, 7u
gpg: depth: 1  valid:   4  signed:   0  trust: 3-, 0q, 0n, 0m, 1f, 0u
gpg: next trustdb check due at 2013-09-23
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

As you see, this command found exactly one matching key for the specified ID, and imported it in my keyring.

(There is no security issue in importing public keys from other people in your keyring. Importing does not mean trusting.)

share|improve this answer
    
I know I can do it like this, but is there a way to do this automatically? –  Thom Wiggers Mar 10 '13 at 20:20
    
You can put several key IDs after gpg --recv-keys so I would say it is a matter of some simple scripting. I mean, do you have hundreds of keys to reimport ? How often do you expect to destroy your own .gnupg folder and have to rebuild it ? –  Tom Leek Mar 10 '13 at 20:22
    
I thought that perhaps there was a simpler tool/command for this that I was missing –  Thom Wiggers Mar 11 '13 at 1:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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