Handling Packages With apt
Goal
You search, install, and remove with apt, and get used to holding a version in place with hold and pin. When this lab is finished, you can explain "why this version was picked" from the apt-cache policy output alone, and you can split reports that "we updated, but the old version is installed" by cause.
Why it matters
apt does not ask the repository in real time. It judges only from the index cached in /var/lib/apt/lists/, and when the same package is in several repositories, it picks by priority. Once you know this structure, both "it's in the repository but I can't see it" (the index is stale) and "I added the internal mirror but it fetches from the official one" (the priorities are the same) are explained at once. This lab environment has no internet and only the /opt/localrepo offline repository, so every installation happens from that repository.
The last step is an incident. "We updated, but the old version is installed" is the most common report in the field, and behind the same symptom hide completely different causes: hold, pins, dependencies, and a stale index. If you push through with --force or "just remove it and reinstall" without telling the causes apart, the same thing repeats at the next update or a bigger outage happens. The policy queries, hold, and pins you got used to in the earlier steps become your diagnostic tools as they are.
Steps
- Create the
/root/pkgdirectory and save the full output ofapt-cache policyto/root/pkg/policy.txt. - Search for a package named
treeand save the result to/root/pkg/search-tree.txt. The result must contain a line whose name is exactlytree. - Write only the install candidate version of
treeon one line in/root/pkg/tree-version.txt. (For example:2.1.1-2ubuntu3) - Install the two packages
treeandncdu. - Install
cowsay, pick out only the Depends line of its dependencies, and save it to/root/pkg/cowsay-depends.txt. - Install
htop, exclude it from upgrades (hold), and save that fact to/root/pkg/hold.txt. - Create
/etc/apt/preferences.d/labhub.prefand pin thehtoppackage at priority 1001. All three linesPackage,Pin, andPin-Prioritymust be present. - Incident: run
incident pk-stuck-oldand read the ticket. Four acme packages fail to receive the 2.0 update and stay on 1.0-1. Upgrade those that can be upgraded to 2.0-1 so they keep following in the next update too, and leave alone those whose dependencies would break. In/root/incident/pk-stuck-old/report.txt, write a cause code for each package (dep-unmet,hold,not-in-repo,pin,stale-index) and oneevidence:line.
Notes
- The
Candidate:line ofapt-cache policy <패키지>(package) is the install candidate, and the number on the left of the version table is that version's priority. If the installed version (***) and the candidate differ, "why do they differ" is the starting point of the diagnosis. - Common mistake 1: writing the
Installed:value in step 3. If it is not installed yet, it is(none). - Common mistake 2: leaving out the leading keyword, as in
Pin: version 3.*, in step 7. ThePin:value must start with one ofversion,release, ororigin, and if it is wrong apt silently ignores it; that is why step 7 is graded not on the file but on whether apt actually uses 1001. - Incident tools:
incident listshows the list, andincident show <이름>(name) shows the ticket (a colleague's report) again. Raising the same incident again returns it to its original broken state. - Write the report to
/root/incident/<이름>/report.txt(name is the incident name): one line<이름>: <원인 코드>(name: cause code) per target, plusevidence: <관찰한 출력 한 줄>(one line of output you observed). The evidence line is checked against the output actually seen in the incident. - Grading does not look only at the report. The actual state, such as the dpkg status DB, apt's candidate, and signature verification, must be right, and workarounds that only erase the symptom (forced installs, removals, turning off signatures) fail.
Working directory and repository check
Create the /root/pkg directory and save the full output of apt-cache policy to /root/pkg/policy.txt.
If you run apt-cache policy without arguments, it prints all the repositories and priorities currently configured. Save that output to a file.
Searching for a package
Search for a package named tree and save the result to /root/pkg/search-tree.txt. The result must contain a line whose name is exactly tree.
apt-cache search searches names and descriptions. The result must contain a line whose name is exactly tree.
Checking the install candidate version
Write only the install candidate version of tree on one line in /root/pkg/tree-version.txt. (For example: 2.1.1-2ubuntu3)
It is the value on the Candidate line of apt-cache policy . Write only the version string on one line.
Installing packages
Install the two packages tree and ncdu.
After installing, check the state with dpkg -s. Only if the Status line is 'install ok installed' is it really installed.
Pulling out the dependency list
Install cowsay, pick out only the Depends line of its dependencies, and save it to /root/pkg/cowsay-depends.txt.
The dependency list comes from the index whether or not the package is installed. The apt-cache subcommand that shows dependency relations also prints layers such as Recommends, Suggests, and Breaks besides Depends, so keep only the Depends lines with the tool's own filtering options or a line filter. Do the install separately afterward.
Excluding from upgrades
Install htop, exclude it from upgrades (hold), and save that fact to /root/pkg/hold.txt.
apt-mark has the subcommands hold / unhold / showhold. Also check that the mark was actually set.
Setting priority with a pin file
Create /etc/apt/preferences.d/labhub.pref and pin the htop package at priority 1001. All three lines Package, Pin, and Pin-Priority must be present.
In a pin file, the three lines Package / Pin / Pin-Priority form one group. Pin starts with one of version, release, or origin.
Incident: supposedly updated, but four packages stay on the old version
Running incident pk-stuck-old raises an incident in four acme packages that use the internal mirror (read the ticket first). Symptom: the 2.0 security update has supposedly been published to the mirror, but all four of acme-agent, acme-cli, acme-report, and acme-sync stay on 1.0-1. Upgrade those that can be upgraded to 2.0-1, and make them keep following in the next regular update as well. Leave those whose dependencies would break if upgraded now at 1.0-1: forcing them in, removing them, or crafting and adding what is not on the mirror is a failure. Do not touch the holds and pins of non-acme packages. In /root/incident/pk-stuck-old/report.txt, write four lines <패키지>: <코드> (package: code) and evidence: <관찰한 출력 한 줄> (one line of output you observed). The code is one of dep-unmet (something the new version requires is missing), hold (an administrator held it), not-in-repo (the new version is not on the mirror), pin (another version wins by priority), or stale-index (this server's index is stale).
Direction: even if "the old version is installed" is the same, apt picking 2.0 as the candidate but failing to upgrade and picking 1.0 as the candidate in the first place are different stories. First split the four into those two groups. Tools: apt-cache policy (installed version, candidate, priorities in the version table), apt-mark, apt-get's simulated run (-s), and the index files in the mirror directory. Concretely: for those whose candidate is 2.0 but were not upgraded, look at the administrator's marked list and the dependencies the simulated run reports. For those whose candidate is 1.0, compare the numbers on the left of the version table (priorities), /etc/apt/preferences.d, and the versions written in the index this server downloaded and in the mirror's Packages file.