Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

One can import a key with:

rpm --import /path/to/key

But how can you tell later if you have already imported this key? Trying to reimport it will fail with an error and I am trying to avoid this as I am using Puppet to install the key.

share|improve this question
up vote 7 down vote accepted

You can double check if a key is already imported using rpm -qi gpg-pubkey-<version>-<release>. If it is installed, rpm will give you all the information about it, if not, it'll just exit with a return value of 1, so you could add to your puppet recipe an unless parameter:

exec { "rpm --import /path/to/package":
  # ...
  unless => "rpm -qi gpg-pubkey-<version>-<release> > /dev/null 2>&1"
share|improve this answer
Better way through some code I found: KEYFILE=/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release; KEYID=$(echo $(gpg --throw-keyids < $KEYFILE)|cut -c11-18|tr [A-Z] [a-z]); rpm -q gpg-pubkey-$KEYID – lsd Feb 6 '13 at 13:43
The cut ties you to a specific layout, which does not work on my RHEL6 test system. And gpg now includes sub keys as well as the pub key. So, I ended up using KEYID=$(gpg --throw-keyids $KEYFILE | sed -n '/^pub/s|^[^/]*/\([[:alnum:]]*\) .*|\1|p' | tr [[:upper:]] [[:lower:]]) – dannysauer Feb 24 '15 at 23:10

Every key imported adds a rpmdb entry of gpg-pubkey-<left(hex(fingerprint), 8)>-<encoded import date>. Just check for that name (gpg-pubkey) and the appropriate version (the first 8 characters of the key fingerprint in hex) in the rpmdb.

share|improve this answer
rpm -qi gpg-pubkey-FINGERPRINT-\* > /dev/null 2>&1 (reference, including how to get the FINGERPRINT: robinbowes.com/article.php/20081107174415581 ) – Ted Zlatanov Apr 26 at 15:16

Your Answer


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.