Sub topic 4
Refreshing metadata before you look at updates
This chapter assumes you can use a shell and sudo, and that you know a package repository is a server holding packages plus an index of them. It also assumes you can read systemctl status output for services and timers, and that you have a Debian/Ubuntu or RHEL-family test machine. It covers what "metadata" is, how apt update and dnf refresh it, how the two tools differ on freshness, and the errors you will meet.
What metadata is and why it goes stale
Neither apt nor dnf asks the repository "what is new?" when you query for updates. Both read a local copy of the repository's index, called metadata. It lists every package, its version, its dependencies, its checksum and where to download it.
When you ask "what can be upgraded?", the tool compares your installed packages with that local copy. If the copy is a week old, you get a week-old answer. A server can look fully patched while a security fix has been sitting in the repository for days.
The rule for the rest of this tutorial: refresh metadata first, then review, then apply.
Refreshing on Debian and Ubuntu
On Debian and Ubuntu, apt update is the refresh step. It never installs or removes anything. It downloads the index files for every source in /etc/apt/sources.list and /etc/apt/sources.list.d/, and stores them in /var/lib/apt/lists/.
sudo apt updateTypical output looks like this:
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB]
Get:3 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB]
Fetched 257 kB in 1s (310 kB/s)
Reading package lists... Done
Building dependency tree... Done
23 packages can be upgraded. Run 'apt list --upgradable' to see them.The line prefixes tell you what happened:
| Prefix | Meaning |
|---|---|
| `Hit` | The local copy is still current. Nothing was downloaded. |
| `Get` | A new or changed file was downloaded. |
| `Ign` | The file was ignored. This is often harmless, for example an optional file that the repository does not publish. |
| `Err` | The download or verification failed. Read the message below it. |
The final line, "N packages can be upgraded", is only as accurate as the refresh that just ran. If any source shows Err, treat that count as incomplete.
Why `apt update` is cheap to repeat
apt sends the checksums of the files it already has. If the repository has not changed, the answer is Hit and almost nothing is transferred. Running apt update before every review costs seconds.
Debian and Ubuntu may already be refreshing for you
Debian and Ubuntu ship systemd timers, apt-daily.timer and apt-daily-upgrade.timer. The first refreshes package lists on a schedule. The second runs unattended upgrades if they are enabled. You can see them with:
systemctl list-timers 'apt-daily*'Because of this, apt update can fail with a lock error if you run it while a background job holds the lock:
E: Could not get lock /var/lib/apt/lists/lock. It is held by process 1234 (apt-get)Wait for the process to finish and run the command again. Do not delete lock files. Removing a lock while a package operation is running can corrupt the package database.
Refreshing on RHEL-family systems
dnf works differently. There is no separate "update" step you must run first. dnf keeps cached metadata in /var/cache/dnf/ and refreshes it automatically when it is older than metadata_expire. The default is 48 hours. You can check the setting:
grep -E '^metadata_expire' /etc/dnf/dnf.conf || echo "metadata_expire not set; the default applies"This has a practical consequence. A plain dnf check-update may answer from a cache that is up to two days old, if the cache has not yet expired. To be sure the answer is current, force a refresh:
sudo dnf makecache --refresh
dnf check-update --refresh
echo "exit code: $?"These commands do different things:
dnf makecachedownloads and stores metadata for all enabled repositories.--refreshmakes it treat the cache as expired.dnf check-update --refreshexpires the cache for that one command and then lists updates. The exit code is 100 if updates exist and 0 if none do. Scripts can use this.dnf clean expire-cachemarks the cache as expired without deleting it. The nextdnfcommand then refreshes it.dnf clean metadatadeletes the cached metadata. The next command downloads it again.
You can also confirm which repositories dnf will contact:
dnf repolistA repository you expected to see but that is missing from this list is disabled or misconfigured, and its updates will not appear in any review.
RHEL-family systems also run dnf-makecache.timer on some versions, which refreshes the cache in the background. Do not rely on it for patch work. Refresh explicitly so that your review is current at the moment you run it.
Comparing the two tools
| Question | apt (Debian, Ubuntu) | dnf (RHEL family) |
|---|---|---|
| Explicit refresh command | `apt update` | `dnf makecache --refresh` |
| Does the query command refresh? | No. `apt list --upgradable` uses the lists as they are. | Yes, but only if the cache is older than `metadata_expire`. |
| Where is the cache? | `/var/lib/apt/lists/` | `/var/cache/dnf/` |
| Force a clean start | Delete the files in `/var/lib/apt/lists/` (see below), then `apt update` | `sudo dnf clean metadata`, then refresh |
| Background refresh | `apt-daily.timer` | `dnf-makecache.timer` on some versions |
The main difference to remember is that apt never refreshes on its own when you query, while dnf sometimes does. Developing the habit of refreshing explicitly with both tools means you never depend on that difference.
Checking how old your metadata is
Before trusting a review, you can check when the lists were last updated. On Debian and Ubuntu with the standard timers, this stamp file records the last successful apt update:
stat -c 'Last successful apt update: %y' /var/lib/apt/periodic/update-success-stampThe file may not exist on a minimal image where the periodic scripts have never run. In that case, check the modification time of the lists directly:
ls -lt --time-style=long-iso /var/lib/apt/lists/ | head -n 5What the refresh verifies
A refresh is also the point where trust in the repository is checked. apt downloads a signed file called InRelease and verifies its signature against the keys it trusts. That file contains checksums of the index files, which are then checked after download. dnf does the same with a repomd.xml file that lists the checksums of the other metadata files. The next chapter's topic, signing, explains the keys. Here you only need to know that a signature failure during refresh stops the process, and the old metadata remains in use.
On RHEL-family systems, signed metadata is optional. The gpgcheck setting verifies packages, while repo_gpgcheck verifies the metadata itself. Many repositories leave repo_gpgcheck at 0, so metadata is protected only by the checksum chain and HTTPS. You can see what a repository uses with:
grep -H -E '^(gpgcheck|repo_gpgcheck)' /etc/yum.repos.d/*.repoCommon errors and what to do
| Message | Likely cause | Action |
|---|---|---|
| `Hash Sum mismatch` | The mirror was updated while you downloaded, or a proxy served a stale file. | Wait a few minutes and retry. If it persists, clear the lists and refresh. |
| `NO_PUBKEY` or `The following signatures couldn't be verified` | The repository's signing key is missing or expired. | Obtain the key from the vendor's official documentation and install it. Do not ignore the warning. |
| `Release file ... is not valid yet` or `has expired` | The server clock is wrong, or the mirror is stale. | Check `timedatectl`. Fix time synchronization first. |
| `404 Not Found` on a whole release | The distribution release reached end of life and moved to an archive. | Plan a release upgrade. See the final chapter. |
| `Could not resolve host` or `Failed to download metadata` | DNS or network problem, or a proxy that blocks the mirror. | Test with `curl -I` against the repository URL. |
| `Could not get lock` | Another package job is running. | Wait. Do not delete the lock. |
If a hash mismatch will not clear, you can empty the apt cache and download it fresh:
sudo rm -rf /var/lib/apt/lists/*
sudo apt updateThis is safe. The lists are only a cache and apt update rebuilds them. Do it only when you have network access to the repositories, because while the lists are empty apt cannot see any packages.
On dnf, the equivalent is:
sudo dnf clean metadata
sudo dnf makecacheDo not ignore a partial failure
If one repository fails and the others succeed, both tools may still print results. The pending list is then missing everything that third-party repository would have supplied. Read the full output of the refresh, not only the last line, and fix errors before you act on the results.
Exercise
A server runs RHEL 9. Yesterday an administrator ran dnf check-update and it reported no updates. Today a vendor advisory says a fix was published last night. Running dnf check-update again still shows nothing. Why might that be, and what command gives a reliable answer?
Show answer
The cached metadata is probably still inside the default 48-hour metadata_expire window, so dnf did not contact the repository and answered from the old copy. Run dnf check-update --refresh (or sudo dnf makecache --refresh first). It forces a new download, and an exit code of 100 means updates are available.
Exercise
On Ubuntu, apt list --upgradable shows nothing, but you have not run apt update for two weeks. What is wrong with that result, and what is the next step?
Show answer
apt list --upgradable reads the local package lists and never contacts the repository, so it only compares against two-week-old data. Run sudo apt update, read the output for Err lines, then run apt list --upgradable again.