Sub topic 3
Holding packages with apt-mark and dnf versionlock
This chapter assumes you can use sudo, read systemd status, and have SSH access to a Debian, Ubuntu, or RHEL-family test machine. You should already know how apt and dnf fetch updates from repositories.
Holding packages with apt-mark and dnf versionlock
A hold stops one package from being upgraded while the rest of the system updates. On Debian and Ubuntu, use apt-mark:
sudo apt-mark hold nginx
apt-mark showhold
sudo apt-mark unhold nginxOn RHEL 8 and 9, install the versionlock plugin, then pin the package:
sudo dnf install python3-dnf-plugin-versionlock
sudo dnf versionlock add nginx
dnf versionlock list
sudo dnf versionlock delete nginxRelease a hold as soon as the reason for it ends. A forgotten hold leaves a known security flaw in place.
Reading history and undoing changes
dnf records every transaction and can reverse one:
sudo dnf history list
sudo dnf history info last
sudo dnf history undo lastapt has no undo command. Check what it installed or upgraded in /var/log/apt/history.log:
grep -E 'Start-Date|Commandline|Upgrade:' /var/log/apt/history.logCleaning up with autoremove and cache cleaning
Autoremove deletes packages that were installed only as dependencies and are no longer needed. Review its list before you confirm:
sudo apt autoremove
sudo apt clean
sudo dnf autoremove
sudo dnf clean allclean removes cached package files. It does not remove installed software.
Automating updates with unattended-upgrades and dnf-automatic
Automatic updates reduce the gap between a fix and its install. Keep them conservative. On Debian and Ubuntu, enable daily list refreshes and security-only upgrades:
sudo apt install unattended-upgrades
sudo tee /etc/apt/apt.conf.d/20auto-upgrades >/dev/null <<'EOF'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
EOF
sudo unattended-upgrade --dry-run --debugOn RHEL-family systems, set upgrade_type = security in /etc/dnf/automatic.conf, then run sudo systemctl enable --now dnf-automatic.timer.
Testing patches and rolling them out in stages
Apply updates to a test host first, then to a small group of production hosts, then to the rest. Keep a rollback plan for each stage.
Logs and verifying a server after patching
After each run, check that the system is healthy:
systemctl --failed
journalctl -p err -bCheck whether a reboot is needed. On Debian and Ubuntu, look for /var/run/reboot-required. On RHEL-family systems, run sudo dnf needs-restarting -r, which exits with 1 when a reboot is recommended.
Major release upgrades are a separate project
Moving from one major release to the next, such as Ubuntu 22.04 to 24.04 or RHEL 8 to 9, changes many packages at once. Plan it separately with its own test environment and rollback steps. Routine patching does not replace it.
Exercise
A server must stay on nginx 1.24 until an application test passes. Which command pins nginx on Ubuntu, and which command releases it later?
Show answer
Run sudo apt-mark hold nginx to pin it, and run sudo apt-mark unhold nginx to release it.