Signing an RPM:
- Generate a GPG key.
- Point
%_gpg_namein~/.rpmmacrosat it. - Build the package with
rpmbuild -ba. - Run
rpm --addsignon the result. - Import the public key with
rpm --importsorpm -Kreports the signature as OK.
Signing the yum repo:
- Run
createrepo. - Detach-sign
repodata/repomd.xmlwith GPG. - Set
gpgcheck=1,repo_gpgcheck=1andgpgkey=in the.repofile.
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
| Step | Unsigned package | Signed package |
|---|---|---|
| 1. Build tree | ~/rpmbuild/{BUILD,BUILDROOT,SOURCES,SRPMS,RPMS,SPECS} | same |
| 2. Spec + source tarball | rpm-no-sig.spec, rpm-no-sig-1.0.tar.gz | rpm-with-sig.spec, rpm-with-sig-1.0.tar.gz |
| 3. Build | rpmbuild -ba | rpmbuild -ba, then rpm --addsign |
| 4. Verify | rpm -Kv (digests only) | rpm --import the key, then rpm -Kv |
| 5. Repo metadata | createrepo --database | createrepo --database, then sign repomd.xml |
| 6. Repo file | gpgcheck=0, repo_gpgcheck=0 | gpgcheck=1, repo_gpgcheck=1, gpgkey= |
What you need installed
rpm-build: providesrpmbuild.rpm-sign: providesrpm --addsign/rpmsignon newer releases.createrepo: builds repo metadata.gnupg2: providesgpg.
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.
cd ~mkdir rpmbuildcd rpmbuildmkdir BUILD BUILDROOT SOURCES SRPMS RPMS SPECSWrite the spec file
Save this as ~/rpmbuild/SPECS/rpm-no-sig.spec:
Name: rpm-no-sigVersion: 1.0Release: 1Summary: This is an unsigned rpm.Vendor: Geekcoding101License: Copyright (c) 2021BuildArch: noarchBuildRoot: %{_tmppath}/%{name}-%{version}Packager: Geekcoding101Source0: %{name}-%{version}.tar.gz
%define INSTALL_DIR /usr/lib/unsigned-rpm/%define INSTALL_FILE rpm-helper-unsigned.py
%descriptionThis package provides an rpm gpgcheck demo.
%prep%setup -q
%installrm -rf %{buildroot}install --directory %{buildroot}/%{INSTALL_DIR}install -m 0755 %{INSTALL_FILE} %{buildroot}/%{INSTALL_DIR}
%cleanrm -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 triggerswarning: File listed twice.%dirmakes 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 originalSunproducedwarning: 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 pythonimport sysimport osimport 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:
cd ~mkdir rpm-no-sig-1.0mv rpm-helper-unsigned.py rpm-no-sig-1.0/tar czf rpm-no-sig-1.0.tar.gz rpm-no-sig-1.0mv 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
cd ~/rpmbuildrpmbuild -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 - Geekcoding101Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.ut6Q5z...Processing files: rpm-no-sig-1.0-1.noarchwarning: File listed twice: /usr/lib/unsigned-rpm/rpm-helper-unsigned.pyProvides: rpm-no-sig = 1.0-1...Wrote: /root/rpmbuild/SRPMS/rpm-no-sig-1.0-1.src.rpmWrote: /root/rpmbuild/RPMS/noarch/rpm-no-sig-1.0-1.noarch.rpmExecuting(%clean): /bin/sh -e /var/tmp/rpm-tmp.w4KHSg...+ exit 0The 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.rpmRPMS/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:
cp SPECS/rpm-no-sig.spec SPECS/rpm-with-sig.spec| Line | Unsigned | Signed |
|---|---|---|
Name: | rpm-no-sig | rpm-with-sig |
Summary: | This is an unsigned rpm. | This is a signed rpm. |
INSTALL_DIR | /usr/lib/unsigned-rpm/ | /usr/lib/signed-rpm/ |
INSTALL_FILE | rpm-helper-unsigned.py | rpm-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:
cd ~mkdir rpm-with-sig-1.0mv rpm-helper-signed.py rpm-with-sig-1.0/tar czf rpm-with-sig-1.0.tar.gz rpm-with-sig-1.0mv 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
gpg --gen-key # on GnuPG 2.1+, gpg --full-generate-key lets you pick key type and sizegpg --fingerprintgpg --list-keys --keyid-format shortTell 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
cd ~/rpmbuildrpmbuild -ba SPECS/rpm-with-sig.specrpm --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:
gpg --export -a <package key ID> > ~/PUB_KEY_SIGNING_RPMrpm --import ~/PUB_KEY_SIGNING_RPMCheck 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
cd ~mkdir unsigned_repo_with_rpm_no_sigcp ~/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 pkgsSpawning worker 1 with 0 pkgsSpawning worker 2 with 0 pkgsSpawning worker 3 with 0 pkgsWorkers FinishedSaving Primary metadataSaving file lists metadataSaving other metadataGenerating sqlite DBsSqlite DBs completeAdd 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 repositorybaseurl=file:///root/unsigned_repo_with_rpm_no_sigenabled=1gpgcheck=0localpkg_gpgcheck=0repo_gpgcheck=0skip_if_unavailable=1Part 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
cd ~mkdir signed_repo_with_rpm_with_sigcp ~/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.
gpg --gen-keygpg --detach-sign --armor --local-user <repo key ID> signed_repo_with_rpm_with_sig/repodata/repomd.xmlgpg --export -a <repo key ID> > ~/PUB_KEY_SIGNING_REPOThis 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 repositorybaseurl=file:///root/signed_repo_with_rpm_with_sigenabled=1gpgcheck=1localpkg_gpgcheck=1repo_gpgcheck=1gpgkey=file:///root/PUB_KEY_SIGNING_RPM file:///root/PUB_KEY_SIGNING_REPOskip_if_unavailable=1gpgcheck vs repo_gpgcheck
| Option | What it checks | Fails when |
|---|---|---|
gpgcheck=1 | The signature on each RPM | The package is unsigned or signed by an unknown key |
repo_gpgcheck=1 | The signature on repodata/repomd.xml | repomd.xml.asc is missing, stale, or signed by an unknown key |
localpkg_gpgcheck=1 | Signatures on local RPM files you install by path | The local package is unsigned or its key is unknown |
Test both repos
yum clean allyum repolistyum install rpm-no-sigyum install rpm-with-sigOn 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 --importare stored in the rpm database asgpg-pubkeypseudo-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=1gets its own keyring. After yum has scanned the repo (for example withyum repolist), you’ll find it at/var/lib/yum/repos/x86_64/7/<your_repo_name>/gpgdir. Usegpg --homedirto list or delete keys there:Terminal window sudo gpg --homedir /var/lib/yum/repos/x86_64/7/<your_repo_name>/gpgdir --list-keyssudo 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:
yum clean allrm -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
| Command | What it does |
|---|---|
yum install <rpm name> | Install a package from enabled repos |
yum install ./<file>.rpm | Install a local RPM file by path |
yum clean all | Clear cached packages and metadata |
yum clean metadata | Clear cached metadata only |
yum-config-manager | Show 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:
rpm -e --allmatches gpg-pubkey-xxxxDo 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]