• Home »
  • Insights »
  • Generate A GPG Key To Sign RPM Packages Created Using rpmbuild On CentOS Linux

Generate A GPG Key To Sign RPM Packages Created Using rpmbuild On CentOS Linux

I have been playing around with building RPM packages today on CentOS because I needed to upgrade curl and the latest version available on CentOS didn’t have the features I needed to use. One of the things that had come h up during the RPM build process and the RPM package installation process using yum was the fact that your RPM packages should be signed. Signing RPM packages gives them some validity and will allow others to install them without having to modify their yum.conf file. Below I describe how to generate a GPG key to be used to sign RPM packages created using the rpmbuild command. First below is a message that will be received when attempting to install unsigned packages using yum with the default yum configuration.

Possible Error When Attempting To Install Unsigned Packages Using Yum Example:

Package curl-7.20.0-1.i386.rpm is not signed

You can get around the above message by disabling the GPG checking built into yum. The option in the yum.conf file that disables checking for GPG signatures in RPM packages that are being installed is called gpgcheck. If gpgcheck is set to 1 then yum will check for GPG signatures but if gpgcheck is set to 0 then yum will not check for GPG signatures in RPM packages installed using yum.

Generate GPG Signature To Use For Signing RPM Packages Built With rpmbuild:

  1. Verify GPG Installed: First you need to verify that GPG is installed on the computer where you are building RPM packages with rpmbuild by issuing the below command.

    Use Which To Verify GPG Installed:


    1. [root@dev ~]# rpm -qf `which gpg`
    2. gnupg-1.4.5-13

    The output in the above example shows that gnupg is installed. If no results are returned then you can easily install gnupg using yum with the below command.

    Install gnupg On CentOS Linux Using Yum Package Manager:


    1. yum install gnupg
  2. Generate GPG Key: Now you need to issue the below command to generate the GPG key which might remind you of generating a SSL certificate.

    Use GPG To Generate A GPG Key:


    1. gpg --gen-key

    Below is an example output of using the gpg command to generate a GPG key that can be used to sign RPM packages built with the rpmbuild command. You should not issue the below command as root but instead you should issue the below command as the user that you also build RPM packages with.

    Example GPG Signature Generation On CentOS Linux:


    1. [alextest@dev ~]$ gpg --gen-key
    2. gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.
    3. This program comes with ABSOLUTELY NO WARRANTY.
    4. This is free software, and you are welcome to redistribute it
    5. under certain conditions. See the file COPYING for details.
    7. gpg: directory `/home/alextest/.gnupg' created
    8. gpg: new configuration file `/home/alextest/.gnupg/gpg.conf' created
    9. gpg: WARNING: options in `/home/alextest/.gnupg/gpg.conf' are not yet active during this run
    10. gpg: keyring `/home/alextest/.gnupg/secring.gpg' created
    11. gpg: keyring `/home/alextest/.gnupg/pubring.gpg' created
    12. Please select what kind of key you want:
    13.   (1) DSA and Elgamal (default)
    14.   (2) DSA (sign only)
    15.   (5) RSA (sign only)
    16. Your selection? 1
    17. DSA keypair will have 1024 bits.
    18. ELG-E keys may be between 1024 and 4096 bits long.
    19. What keysize do you want? (2048) 1024
    20. Requested keysize is 1024 bits
    21. Please specify how long the key should be valid.
    22.         0 = key does not expire
    23.      <n>  = key expires in n days
    24.      <n>w = key expires in n weeks
    25.      <n>m = key expires in n months
    26.      <n>y = key expires in n years
    27. Key is valid for? (0) 0
    28. Key does not expire at all
    29. Is this correct? (y/N) y
    31. You need a user ID to identify your key; the software constructs the user ID
    32. from the Real Name, Comment and Email Address in this form:
    33.    "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"
    35. Real name: Joe User
    36. Email address: joeuser@question-defense.com
    37. Comment: Example GPG Key Signature For RPM Packages
    38. You selected this USER-ID:
    39.    "Joe User (Example GPG Key Signature For RPM Packages) <joeuser@question-defense.com>"
    41. Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
    42. You need a Passphrase to protect your secret key.
    44. We need to generate a lot of random bytes. It is a good idea to perform
    45. some other action (type on the keyboard, move the mouse, utilize the
    46. disks) during the prime generation; this gives the random number
    47. generator a better chance to gain enough entropy.
    48. ++++++++++++++++++++.++++++++++.++++++++++++++++++++++++++++++++++++++++.+++++.++++++++++.+++++++++++++++++++++++++++++++++++..++++++++++>++++++++++....>+++++..........+++++
    50. Not enough random bytes available.  Please do some other work to give
    51. the OS a chance to collect more entropy! (Need 278 more bytes)
    52. We need to generate a lot of random bytes. It is a good idea to perform
    53. some other action (type on the keyboard, move the mouse, utilize the
    54. disks) during the prime generation; this gives the random number
    55. generator a better chance to gain enough entropy.
    56. +++++++++++++++.++++++++++++++++++++++++++++++.+++++++++++++++++++++++++.+++++.+++++++++++++++.+++++++++++++++++++++++++..+++++++++++++++.................>+++++...............................+++++^^^
    57. gpg: /home/alextest/.gnupg/trustdb.gpg: trustdb created
    58. gpg: key C169539E marked as ultimately trusted
    59. public and secret key created and signed.
    61. gpg: checking the trustdb
    62. gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
    63. gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
    64. pub   1024D/C169539E 2010-03-04
    65.      Key fingerprint = 91D0 CFD2 4DE2 BA85 81AD  2319 F834 AF3B C169 539E
    66. uid                  Joe User (Example GPG Key Signature For RPM Packages) <joeuser@question-defense.com>
    67. sub   1024g/85369CDD 2010-03-04

    During the process of key generation you will be required to select the type of key you want, the length of the GPG key itself, the GPG key expiration date, your name, your email address, a comment associated to the key, and a passphrase that you will need to remember as it is required each time you generate a signed RPM package. After entering in all of the information the computer will generate the GPG key and depending on the computer it may take awhile. In fact sometimes you might get a message as noted in the example above the computer may need to collect more entropy. You can read this article regarding what entropy means and how to generate more entropy for the OS. This process will create a .gnupg directory in the home directory of the user that issued the gpg command which will have numerous files in it after completing the GPG key generation.

  3. Configure RPM Macros: After generating the GPG key you should configure some RPM macros so the rpmbuild command knows where to locate the correct GPG key to use for signing RPM packages. The .rpmmacros may already exist but if it doesn’t create it with your favorite text file in the same user who builds the RPM files home directory and add the below lines to it. Make sure to replace each variable with the proper content.

    Add RPM Macros To The .rpmmacros File In The Users Home Directory:


    1. %_signature gpg
    2. %_gpg_path /home/alextest/.gnupg
    3. %_gpg_name Joe User (Example GPG Key Signature For RPM Packages) <joeuser@question-defense.com>
    4. %_gpgbin /usr/bin/gpg

    If there are already other RPM macros in the .rpmmacros file just add the above four macros to the end of the file. Again make sure to update each value with the correct input for your scenario. You can locate the gpg location by typing “which gpg” from the command line of the computer but typically on CentOS gpg will be installed in the /usr/bin directory.

  4. rpmbuild Command To Sign Packages: Last but not least you will need to test this out by building a RPM package with the rpmbuild command. An example rpmbuild command is below and you will know that you are successful when you are asked for the passphrase you entered during the GPG key creation.

    Generate A Signed RPM Package With The rpmbuild Command:


    1. rpmbuild -v -ba --sign --clean SPECS/curl.spec

    The above example command was issued from the rpmbuild directory located inside the users home directory. You will obviously need to have the proper source file(s) and have created a spec file already.

Once the package is built you can test if the signature was built in properly by attempting to install the RPM on your server. If the yum gpgcheck configuration setting is still the default setting (1) then the RPM installation will fail because you do not have the GPG key imported into RPM. Below is a command you can use to not only import the GPG key using rpm but also to generate the public ASCII GPG key version you will share for others to import using rpm before installing any packages you provide them.

Generate An ASCII Public Version Of Your GPG Key:


  1. gpg --armor --output JOEUSER-GPG-KEY --export 'Joe User'

You will issue the above command while logged in as the same user you build RPM packages with and you generated the GPG key with. This will generate a ASCII version of your GPG key that can be imported using the rpm command as shown below.

Import A GPG Key Using RPM To Install Signed RPM Packages:


  1. rpm --import JOEUSER-GPG-KEY

After importing the JOEUSER-GPG-KEY using the rpm command you will now be able to use yum to install the signed RPM package you created earlier.

Linux Security Cookbook (Paperback)

List Price: $39.99 USD
New From: $13.39 USD In Stock
Used from: Too low to display In Stock

PGP & GPG: Email for the Practical Paranoid (Paperback)

List Price: $24.95
New From: $20.52 USD In Stock
Used from: $9.19 USD In Stock