Skip to main content

Linux and servers · Intermediate · 14 min

Add a third-party apt repository safely with signed-by keys

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · apt 3.2 · Trivy 0.76.2

You will add Aqua Security's Trivy repository to Ubuntu 26.04 with a fingerprint-checked key in /etc/apt/keyrings, a deb822 .sources file and a pin that limits it to its own package, then add and remove a PPA and clean everything up.

On this page

Every apt repository you add is a promise: from now on, root on your server installs whatever that repository signs. A careless repository setup turns that promise into "whatever anyone with any trusted key signs", and lets a vendor's repository replace Ubuntu's own packages without asking.

This tutorial adds one real repository the careful way, using Aqua Security's repository for Trivy, the vulnerability scanner. You will check the vendor's key against a fingerprint published somewhere else before trusting it, store it so it only vouches for that one repository, describe the repository in the deb822 .sources format, and pin it so it can only supply the trivy package. Then you will see how a Launchpad PPA differs, and remove both cleanly. The same pattern works for any vendor repository: Docker, Nginx, PostgreSQL, Node.js.

Step 1 · See how Ubuntu 26.04 trusts repositories

Older guides tell you to run apt-key add. That command put keys into /etc/apt/trusted.gpg or /etc/apt/trusted.gpg.d/, and apt trusted every key there for every repository. A key you added for one small vendor could sign a fake openssh-server for the Ubuntu archive, and apt would accept it.

apt 3.2, the version in Ubuntu 26.04, no longer ships apt-key at all. /etc/apt/trusted.gpg is no longer read, and apt warns about a .list source that has no signed-by option. Each repository names the one key file that may sign it, with Signed-By. Keys you add by hand go in /etc/apt/keyrings/; packaged keys, such as Ubuntu's own, live in /usr/share/keyrings/.

Look at what your server trusts now:

Bash
command -v apt-key || echo "apt-key is not installed"
ls -l /etc/apt/sources.list.d/
grep -h '^Signed-By' /etc/apt/sources.list.d/*.sources

Check it worked

The first line prints apt-key is not installed. The listing shows ubuntu.sources, and the grep prints a line like Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg: the Ubuntu archive is tied to Ubuntu's keyring and nothing else. If the listing also shows old .list files from earlier installs, apt 3 can convert them to .sources files for you. Preview the change first, then run it for real:

Bash
sudo apt modernize-sources --simulate
sudo apt modernize-sources

Step 2 · Download the vendor key and check its fingerprint

A key downloaded over HTTPS proves only that it came from that web server. If the server or the vendor's website was tampered with, the key and the instructions next to it change together. So compare the key's fingerprint with one published through a separate channel before you trust it.

Install the tools, then download Trivy's key to a temporary file. Nothing trusts it yet:

Bash
sudo apt-get update
sudo apt-get install -y ca-certificates curl gpg
curl -fsSL https://aquasecurity.github.io/trivy-repo/deb/public.key -o /tmp/trivy-repo.asc
gpg --show-keys --with-fingerprint /tmp/trivy-repo.asc

gpg --show-keys reads the file without importing it into any keyring. You see one pub key, its creation and expiry dates, a 40-character fingerprint in groups of four and the user ID trivy-repo <oss@aquasec.com>.

Now find the fingerprint from a second source. Aqua's repository is built from the aquasecurity/trivy-repo project on GitHub, and its configuration file deb/conf/distributions names the signing key in a SignWith: line. On 26 November 2026 that line read 825AD9036F7C850E6A6FED4935B8ACA44FD9CA9F. Open the file on github.com yourself rather than copying the value from this page: the point of the check is that you look at the vendor's own record.

Compare the two with a command rather than by eye, so one wrong character cannot slip past. Replace the value of expected with the fingerprint you read from the vendor:

Bash
expected=825AD9036F7C850E6A6FED4935B8ACA44FD9CA9F
actual=$(gpg --show-keys --with-colons /tmp/trivy-repo.asc | awk -F: '$1 == "fpr" { print $10; exit }')
keys=$(gpg --show-keys --with-colons /tmp/trivy-repo.asc | grep -c '^pub')
if [ "$actual" = "$expected" ] && [ "$keys" = "1" ]; then echo "OK: one key, fingerprint matches"; else echo "STOP: fingerprint $actual does not match, or the file holds $keys keys"; false; fi

The script also counts the keys, because a file with a second key added would pass a check that looks only at the first.

Check it worked

The last line prints OK: one key, fingerprint matches. If it prints STOP, do not continue. Delete /tmp/trivy-repo.asc and find out why: vendors rotate keys, and a rotation should come with an announcement you can find. Trivy's key was replaced in April 2026, after a March 2026 incident in which a malicious Trivy release was published; that history is a good reason to make this check every time, not a one-off.

Step 3 · Store the key so it signs only this repository

Install the checked key into /etc/apt/keyrings/ with fixed permissions. apt reads ASCII-armoured keys directly as long as the file name ends in .asc, so there is no need to convert it with gpg --dearmor:

Bash
sudo install -d -m 0755 /etc/apt/keyrings
sudo install -m 0644 /tmp/trivy-repo.asc /etc/apt/keyrings/trivy-repo.asc
rm /tmp/trivy-repo.asc

install copies the file and sets its mode in one move. The key must be readable by everyone (0644), because apt's download methods run as the unprivileged _apt user. A key file that only root can read gives a NO_PUBKEY error later, even though the file is there.

Check it worked

Bash
ls -l /etc/apt/keyrings/

You see trivy-repo.asc, owned by root with -rw-r--r--. On its own the file does nothing: no repository refers to it yet, and apt does not trust keys just because they are in this folder.

Step 4 · Pin the repository before you enable it

A repository can carry any package name it likes. If Aqua's repository ever published a package called openssl or curl with a higher version number, a normal apt upgrade would install it in place of Ubuntu's. Prevent that before apt ever reads the repository, with a file in /etc/apt/preferences.d/:

Bash
sudo tee /etc/apt/preferences.d/trivy-repo.pref > /dev/null <<'EOF'
# Aqua's repository may supply the trivy package and nothing else.
Package: trivy
Pin: release o=aquasecurity.github.io/trivy-repo/deb
Pin-Priority: 500

Package: *
Pin: release o=aquasecurity.github.io/trivy-repo/deb
Pin-Priority: -1
EOF

o= matches the Origin: field in the repository's Release file, which for this repository is aquasecurity.github.io/trivy-repo/deb. A priority of -1 means apt never installs that version. The first record is specific to trivy, and apt checks records that name a package before the * record, so trivy keeps the normal priority of 500 and every other name the repository offers is ignored.

The Release file states its origin, so look before you trust a pin. You can read it once the source is added in the next step, with apt-cache policy, which prints o= for every repository.

Check it worked

Bash
cat /etc/apt/preferences.d/trivy-repo.pref

The file shows two records with priorities 500 and -1. The file name ends in .pref: apt ignores files in preferences.d with other extensions, apart from having none at all.

Step 5 · Describe the repository in a .sources file

Write the repository as a deb822 .sources file. Each field is on its own line, so it is easier to read and to change with configuration tools than the one-line .list format:

Bash
sudo tee /etc/apt/sources.list.d/trivy.sources > /dev/null <<EOF
Types: deb
URIs: https://aquasecurity.github.io/trivy-repo/deb
Suites: generic
Components: main
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/trivy-repo.asc
EOF
cat /etc/apt/sources.list.d/trivy.sources

The fields mean:

  • Types: deb asks for binary packages. deb-src would ask for source packages, which you do not need.
  • URIs is the repository's base address. Use HTTPS where the vendor offers it; the signature protects the content either way, but HTTPS hides which packages you fetch.
  • Suites is the distribution name inside the repository. Many vendors use your Ubuntu code name (resolute on 26.04). Aqua builds one set of packages for every release and calls it generic, as its installation guide says.
  • Components is the section of the suite, here main.
  • Architectures limits the download to your CPU type. The heredoc fills in amd64 or arm64.
  • Signed-By names the only key that may sign this repository.

Then read the repository:

Bash
sudo apt-get update

Each repository adds its own index files to every apt update. On a metered or prepaid connection, which is common for servers and laptops across Africa, a handful of unused repositories add up; keeping each one limited to your architecture and removing the ones you no longer use keeps updates small.

Check it worked

The apt-get update output has a line for https://aquasecurity.github.io/trivy-repo/deb generic InRelease and ends with no W: or E: lines. A NO_PUBKEY error means the key path in Signed-By is wrong or the key file is not readable. Now look at the pin in action:

Bash
apt-cache policy trivy

The Candidate: line shows a version such as 0.76.2, and in the version table that version has priority 500, from the trivy record. The repository line under it shows -1: that is the priority the * record gives everything else from this origin, so it is expected. Run apt-cache policy with no package name to see each repository with its o= origin, and confirm the Trivy one matches what you wrote in the pin.

Step 6 · Install the package and confirm where it came from

Bash
sudo apt-get install -y trivy
trivy --version

From now on sudo apt update && sudo apt upgrade keeps Trivy up to date with the rest of the system, which is the main reason to use a repository instead of a downloaded binary. Your unattended security updates do not include it by default, because they only allow Ubuntu's own origins; see the automatic updates tutorial linked at the end if you want them to.

Check it worked

trivy --version prints something like Version: 0.76.2. Run apt-cache policy trivy again: the Installed: and Candidate: lines match, and the *** marker in the version table sits next to the line from aquasecurity.github.io.

Step 7 · Add a PPA with add-apt-repository

A PPA (Personal Package Archive) is a repository hosted on Launchpad, Canonical's build service, and each PPA has its own signing key. add-apt-repository downloads that key from Launchpad over HTTPS and writes a .sources file with the key embedded in its Signed-By field, so the key again signs only that one archive.

As an example, add the HAProxy 3.4 PPA, which builds for Ubuntu 26.04:

Bash
sudo apt-get install -y software-properties-common
sudo add-apt-repository -y ppa:vbernat/haproxy-3.4
ls /etc/apt/sources.list.d/

The command adds the source and runs apt update. Read the new file:

Bash
cat /etc/apt/sources.list.d/vbernat-ubuntu-haproxy-3_4-*.sources
apt-cache policy haproxy

A PPA is not pinned, and that is often the point: this one exists to replace Ubuntu's haproxy with a newer version. Trust it only as much as you trust its owner, because it can replace any package it chooses to build. The Launchpad page of a PPA shows its signing key fingerprint, so you can check the embedded key in the same way as before.

Check it worked

The file name starts with vbernat-ubuntu-haproxy-3_4- and ends in .sources. Its Signed-By: field holds a whole -----BEGIN PGP PUBLIC KEY BLOCK----- instead of a path. apt-cache policy haproxy lists a 3.4 candidate from ppa.launchpadcontent.net above Ubuntu's own version.

Step 8 · Remove a repository and its key cleanly

When you stop using a repository, remove its source, key and pin together. A key left in /etc/apt/keyrings/ is harmless only until someone points a new source at it.

Remove the PPA first. Because its key lives inside the .sources file, removing the file removes the key:

Bash
sudo add-apt-repository -y --remove ppa:vbernat/haproxy-3.4

If you are moving on to the Trivy scanning tutorial, stop here and keep the Trivy repository. Otherwise, the next commands uninstall Trivy and delete the three files you created. Read the paths before you run them: rm -f does not ask.

Bash
sudo apt-get purge -y trivy
sudo rm -f /etc/apt/sources.list.d/trivy.sources /etc/apt/keyrings/trivy-repo.asc /etc/apt/preferences.d/trivy-repo.pref
sudo apt-get update

The apt-get purge removes the package and its configuration. Do this before you delete the source, so apt still knows where the package came from while it removes it. If you installed software-properties-common only for this tutorial, sudo apt-get autoremove --purge software-properties-common removes it too.

Check it worked

Bash
ls /etc/apt/sources.list.d/ /etc/apt/keyrings/
apt-cache policy trivy

/etc/apt/sources.list.d/ holds only ubuntu.sources (and whatever you had before), /etc/apt/keyrings/ is empty again, and apt-cache policy trivy prints nothing, because no repository offers the package any more.

Next steps

  • Scan images with the tool you just installed: /tutorials/scan-container-images-for-vulnerabilities-with-trivy.
  • Hold packages and pin by version, not only by origin: /tutorials/control-package-versions-with-apt-hold-and-pin.
  • See the same pattern applied to Docker's own repository: /tutorials/install-docker-engine-on-ubuntu-26-04.
  • Decide which origins unattended upgrades may install from: /tutorials/automatic-security-updates-ubuntu.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.