GeekCoding101 make your way to geek

Build and Sign RPM Packages and Yum Repos

Build RPMs with rpmbuild, sign them with GPG, verify with rpm -K, and serve signed and unsigned yum repos with createrepo. Full spec files included.

8 min read
geekcoding101.com logo banner Pot Card
On this pageThe whole process at a glance

Signing an RPM:

  1. Generate a GPG key.
  2. Point %_gpg_name in ~/.rpmmacros at it.
  3. Build the package with rpmbuild -ba.
  4. Run rpm --addsign on the result.
  5. Import the public key with rpm --import so rpm -K reports the signature as OK.

Signing the yum repo:

  1. Run createrepo.
  2. Detach-sign repodata/repomd.xml with GPG.
  3. Set gpgcheck=1, repo_gpgcheck=1 and gpgkey= in the .repo file.

Building packages is something you can’t avoid for long on Linux, whether at work or while studying. Below is the full walkthrough with two demo packages: one unsigned and one signed, each served from its own local repo. Every spec file and config is included.

The commands were run as root on a yum-based, release-7 RPM system (that’s where the /var/lib/yum/repos/x86_64/7/ paths come from). On dnf-based systems the packaging and signing steps are the same, but cache paths differ.

The whole process at a glance

StepUnsigned packageSigned package
1. Build tree~/rpmbuild/{BUILD,BUILDROOT,SOURCES,SRPMS,RPMS,SPECS}same
2. Spec + source tarballrpm-no-sig.spec, rpm-no-sig-1.0.tar.gzrpm-with-sig.spec, rpm-with-sig-1.0.tar.gz
3. Buildrpmbuild -barpmbuild -ba, then rpm --addsign
4. Verifyrpm -Kv (digests only)rpm --import the key, then rpm -Kv
5. Repo metadatacreaterepo --databasecreaterepo --database, then sign repomd.xml
6. Repo filegpgcheck=0, repo_gpgcheck=0gpgcheck=1, repo_gpgcheck=1, gpgkey=

What you need installed

  • rpm-build: provides rpmbuild.
  • rpm-sign: provides rpm --addsign / rpmsign on newer releases.
  • createrepo: builds repo metadata.
  • gnupg2: provides gpg.

Part 1: Build an unsigned RPM

Create the build folder structure

If you don’t set %_topdir in ~/.rpmmacros (rpm’s per-user config file), rpmbuild uses ~/rpmbuild by default.

Terminal window
cd ~
mkdir rpmbuild
cd rpmbuild
mkdir BUILD BUILDROOT SOURCES SRPMS RPMS SPECS

Write the spec file

Save this as ~/rpmbuild/SPECS/rpm-no-sig.spec:

Name: rpm-no-sig
Version: 1.0
Release: 1
Summary: This is an unsigned rpm.
Vendor: Geekcoding101
License: Copyright (c) 2021
BuildArch: noarch
BuildRoot: %{_tmppath}/%{name}-%{version}
Packager: Geekcoding101
Source0: %{name}-%{version}.tar.gz
%define INSTALL_DIR /usr/lib/unsigned-rpm/
%define INSTALL_FILE rpm-helper-unsigned.py
%description
This package provides an rpm gpgcheck demo.
%prep
%setup -q
%install
rm -rf %{buildroot}
install --directory %{buildroot}/%{INSTALL_DIR}
install -m 0755 %{INSTALL_FILE} %{buildroot}/%{INSTALL_DIR}
%clean
rm -rf %{buildroot}
%files
%defattr(-,root,root,-)
%dir %{INSTALL_DIR}
%{INSTALL_DIR}/%{INSTALL_FILE}
%exclude %{INSTALL_DIR}/*.pyc
%exclude %{INSTALL_DIR}/*.pyo
%changelog
* Thu Jan 21 2021 Geekcoding101 - 1.0-1
- Initial commit.

Two lines differ from the version first published here:

  • %dir %{INSTALL_DIR}: Listing the bare directory packages everything inside it. Listing the file again after that triggers warning: File listed twice. %dir makes the package own only the directory itself.
  • * Thu Jan 21 2021: rpm checks that the weekday matches the date. 21 January 2021 was a Thursday. The original Sun produced warning: bogus date in %changelog.

Create a dummy source file and tarball

The file to be packed into the rpm, rpm-helper-unsigned.py:

#!/usr/bin/env python
import sys
import os
import re
def main():
print("This is from unsigned rpm.")
if __name__ == '__main__':
main()

The source folder must be named <rpm-name>-<version>, because that is what %setup expects to find inside the tarball:

Terminal window
cd ~
mkdir rpm-no-sig-1.0
mv rpm-helper-unsigned.py rpm-no-sig-1.0/
tar czf rpm-no-sig-1.0.tar.gz rpm-no-sig-1.0
mv rpm-no-sig-1.0.tar.gz ~/rpmbuild/SOURCES/

(tar czf does the same as tar cf followed by gzip.) When you build, rpmbuild finds this tarball in SOURCES and extracts it automatically.

Build the package

Terminal window
cd ~/rpmbuild
rpmbuild -ba SPECS/rpm-no-sig.spec

-ba builds both the binary RPM and the source RPM. Here are the lines that matter from the original build, trimmed. Both warnings come from the old spec and are fixed in the version above.

warning: bogus date in %changelog: Sun Jan 21 2021 - Geekcoding101
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.ut6Q5z
...
Processing files: rpm-no-sig-1.0-1.noarch
warning: File listed twice: /usr/lib/unsigned-rpm/rpm-helper-unsigned.py
Provides: rpm-no-sig = 1.0-1
...
Wrote: /root/rpmbuild/SRPMS/rpm-no-sig-1.0-1.src.rpm
Wrote: /root/rpmbuild/RPMS/noarch/rpm-no-sig-1.0-1.noarch.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.w4KHSg
...
+ exit 0

The package lands at RPMS/noarch/rpm-no-sig-1.0-1.noarch.rpm.

Check digests with rpm -K

rpm -Kv <rpm file> checks the package’s digests, and its signatures if it has any:

# rpm -Kv RPMS/noarch/rpm-no-sig-1.0-1.noarch.rpm
RPMS/noarch/rpm-no-sig-1.0-1.noarch.rpm:
Header SHA1 digest: OK (84bd6662874a27ccd5cd3247ef7a4107c1919f54)
MD5 digest: OK (198ab02bd5765c383c57dbe113551af0)

No signature lines appear because this package isn’t signed. Only the digests are checked.


Part 2: Build a signed RPM

Write the spec file

Copy the first spec and change four lines:

Terminal window
cp SPECS/rpm-no-sig.spec SPECS/rpm-with-sig.spec
LineUnsignedSigned
Name:rpm-no-sigrpm-with-sig
Summary:This is an unsigned rpm.This is a signed rpm.
INSTALL_DIR/usr/lib/unsigned-rpm//usr/lib/signed-rpm/
INSTALL_FILErpm-helper-unsigned.pyrpm-helper-signed.py

Create the source file and tarball

Reuse the Python file from Part 1 as rpm-helper-signed.py and change the print message to "This is from signed rpm.". Then package it the same way:

Terminal window
cd ~
mkdir rpm-with-sig-1.0
mv rpm-helper-signed.py rpm-with-sig-1.0/
tar czf rpm-with-sig-1.0.tar.gz rpm-with-sig-1.0
mv rpm-with-sig-1.0.tar.gz ~/rpmbuild/SOURCES/

The two tarballs have different names, so both can stay in SOURCES.

Generate a GPG key for signing packages

Terminal window
gpg --gen-key # on GnuPG 2.1+, gpg --full-generate-key lets you pick key type and size
gpg --fingerprint
gpg --list-keys --keyid-format short

Tell rpm which key to sign with. Add this line to ~/.rpmmacros, using the key ID (the last 8 hex digits of the fingerprint) or the key’s user ID:

%_gpg_name <key ID or user ID>

Build and sign

Terminal window
cd ~/rpmbuild
rpmbuild -ba SPECS/rpm-with-sig.spec
rpm --addsign RPMS/noarch/rpm-with-sig-1.0-1.noarch.rpm # or: rpmsign --addsign ...

Verify the signature: NOKEY, then OK

Right after signing, rpm -Kv shows the signature but reports NOKEY:

# rpm -Kv ../rpm-with-sig-1.0-1.noarch.rpm
../rpm-with-sig-1.0-1.noarch.rpm:
Header V4 RSA/SHA1 Signature, key ID 1ddb39c6: NOKEY
Header SHA1 digest: OK (64fc89cf3eb3054e6316a77f4b22c183221ab13d)
V4 RSA/SHA1 Signature, key ID 1ddb39c6: NOKEY
MD5 digest: OK (fadae5f41bb0c939edbe865a974fce4c)

NOKEY means the package is signed, but the public key isn’t in rpm’s database yet. Export the key and import it:

Terminal window
gpg --export -a <package key ID> > ~/PUB_KEY_SIGNING_RPM
rpm --import ~/PUB_KEY_SIGNING_RPM

Check again:

# rpm -Kv ../rpm-with-sig-1.0-1.noarch.rpm
../rpm-with-sig-1.0-1.noarch.rpm:
Header V4 RSA/SHA1 Signature, key ID 1ddb39c6: OK
Header SHA1 digest: OK (64fc89cf3eb3054e6316a77f4b22c183221ab13d)
V4 RSA/SHA1 Signature, key ID 1ddb39c6: OK
MD5 digest: OK (fadae5f41bb0c939edbe865a974fce4c)

Part 3: Create an unsigned yum repo

Build the repo metadata

Terminal window
cd ~
mkdir unsigned_repo_with_rpm_no_sig
cp ~/rpmbuild/RPMS/noarch/rpm-no-sig-1.0-1.noarch.rpm unsigned_repo_with_rpm_no_sig/
createrepo --database unsigned_repo_with_rpm_no_sig/

Output:

Spawning worker 0 with 1 pkgs
Spawning worker 1 with 0 pkgs
Spawning worker 2 with 0 pkgs
Spawning worker 3 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

Add the repo file

Create /etc/yum.repos.d/rpm-no-sig.repo. If you aren’t working as root, change /root to your home path.

[RPM-NO-SIG]
name=rpm no sig repository
baseurl=file:///root/unsigned_repo_with_rpm_no_sig
enabled=1
gpgcheck=0
localpkg_gpgcheck=0
repo_gpgcheck=0
skip_if_unavailable=1

Part 4: Create a signed yum repo

Signing the repo protects the metadata (repomd.xml). Signing the package protects the RPM itself. The signed repo below does both.

Build the repo metadata

Terminal window
cd ~
mkdir signed_repo_with_rpm_with_sig
cp ~/rpmbuild/RPMS/noarch/rpm-with-sig-1.0-1.noarch.rpm signed_repo_with_rpm_with_sig/
createrepo --database signed_repo_with_rpm_with_sig/

Sign repomd.xml

This walkthrough uses a second key for the repo. You can also reuse the package key.

Terminal window
gpg --gen-key
gpg --detach-sign --armor --local-user <repo key ID> signed_repo_with_rpm_with_sig/repodata/repomd.xml
gpg --export -a <repo key ID> > ~/PUB_KEY_SIGNING_REPO

This writes signed_repo_with_rpm_with_sig/repodata/repomd.xml.asc.

Every time you rerun createrepo, repomd.xml is regenerated. Sign it again, or yum will reject the repo.

Add the repo file

Create /etc/yum.repos.d/rpm-with-sig.repo. The gpgkey= line tells yum where to find the public keys for the package signature and the repo signature.

[RPM-WITH-SIG]
name=rpm with sig repository
baseurl=file:///root/signed_repo_with_rpm_with_sig
enabled=1
gpgcheck=1
localpkg_gpgcheck=1
repo_gpgcheck=1
gpgkey=file:///root/PUB_KEY_SIGNING_RPM file:///root/PUB_KEY_SIGNING_REPO
skip_if_unavailable=1

gpgcheck vs repo_gpgcheck

OptionWhat it checksFails when
gpgcheck=1The signature on each RPMThe package is unsigned or signed by an unknown key
repo_gpgcheck=1The signature on repodata/repomd.xmlrepomd.xml.asc is missing, stale, or signed by an unknown key
localpkg_gpgcheck=1Signatures on local RPM files you install by pathThe local package is unsigned or its key is unknown

Test both repos

Terminal window
yum clean all
yum repolist
yum install rpm-no-sig
yum install rpm-with-sig

On first use of the signed repo, yum may ask you to confirm importing the repo key.


How RPM and YUM keep GPG keys

RPM-based systems use two separate places for keys:

  • RPM’s keyring is used to verify signatures on RPM packages. Keys added with rpm --import are stored in the rpm database as gpg-pubkey pseudo-packages, not in your personal ~/.gnupg. List them with:

    Terminal window
    rpm -q gpg-pubkey --qf '%{NAME}-%{VERSION}-%{RELEASE}\t%{SUMMARY}\n'
  • YUM’s per-repo keyrings are used to verify signatures on repository metadata. Each repo with repo_gpgcheck=1 gets its own keyring. After yum has scanned the repo (for example with yum repolist), you’ll find it at /var/lib/yum/repos/x86_64/7/<your_repo_name>/gpgdir. Use gpg --homedir to list or delete keys there:

    Terminal window
    sudo gpg --homedir /var/lib/yum/repos/x86_64/7/<your_repo_name>/gpgdir --list-keys
    sudo gpg --homedir /var/lib/yum/repos/x86_64/7/<your_repo_name>/gpgdir --delete-key <keyid>

Clean up YUM completely

yum clean all doesn’t remove everything. For a real clean of one repo, for example after changing its keys:

Terminal window
yum clean all
rm -fr /var/lib/yum/repos/x86_64/7/<your_repo_name>
rm -fr /var/cache/yum/x86_64/7/<your_repo_name>

Yum command reference

CommandWhat it does
yum install <rpm name>Install a package from enabled repos
yum install ./<file>.rpmInstall a local RPM file by path
yum clean allClear cached packages and metadata
yum clean metadataClear cached metadata only
yum-config-managerShow current yum configuration
yum-config-manager --enable <repo id>Enable a repo (--disable to turn it off)

FAQ

What does NOKEY mean in rpm -K output?

The package is signed, but rpm doesn’t have the matching public key. Export it with gpg --export -a <key ID> and add it with rpm --import. The signature lines will then show OK.

Why does rpmbuild say “bogus date in %changelog”?

The weekday in the changelog line doesn’t match the date. Use the format * Thu Jan 21 2021 Name - version-release with the correct day.

Why does rpmbuild warn “File listed twice”?

The %files section lists a directory and then a file inside it. Listing a directory packages its whole contents. Use %dir /path/ to own only the directory, then list the files separately.

I can’t remove keys from RPM because of duplicate entries. What now?

You might see duplicate entries with the same fingerprint in rpm -qa gpg-pubkey*, and rpm -e gpg-pubkey-xxxx removes none of them. Remove them all at once with:

Terminal window
rpm -e --allmatches gpg-pubkey-xxxx

Do I need to re-sign the repo after adding a package?

Yes. createrepo rewrites repodata/repomd.xml, so run the gpg --detach-sign step again afterwards.

That’s all for today. Hope you find it useful!

[keep: comments]

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.

Keep reading

All posts