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.
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.
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. - Create
/root/pkg/report.txt. For the four packages you installed earlier,tree,ncdu,htop, andcowsay, write one line each in the format이름 버전(name and version), and on the last line writeHOLD=<hold 걸린 패키지 이름>(HOLD= followed by the name of the package under hold).
Notes
- The
Candidate:line ofapt-cache policy <패키지>is the install candidate (the placeholder is the package name). - You can pull the name and version on one line with
dpkg-query -W -f='${Package} ${Version}\n' <패키지>(the placeholder is the package name). - 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.
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.
You have to install it first so that the report in a later step can be filled in. apt-cache depends shows Recommends and Suggests along with Depends, so keep only the Depends lines.
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.
Installation status report
Create /root/pkg/report.txt. For the four packages you installed earlier, tree, ncdu, htop, and cowsay, write one line each in the format 이름 버전 (name and version), and on the last line write HOLD=<hold 걸린 패키지 이름> (HOLD= followed by the name of the package under hold).
If you use the -W -f option of dpkg-query, you can pull out just the fields you want in the format you want. The hold list is on the apt-mark side.