Debian 13 Tutorial / Diagnose APT Locks, Dependencies, Backports and Pinning

Diagnose Debian 13 APT errors by checking lock owners, available space and inodes, interrupted dpkg state, then package candidates and dependencies. Inspect the actual process and proposed changes instead of deleting lock files or adding another Debian release. Use backports and pinning only for a defined version-selection need after identifying the original problem.

Record the Error and Current Package State

Do not blindly repeat the same command; record the failed command and the last error. Recording server status and APT logs help narrow down when and what has changed. The following is a query that does not change packages.

cat /etc/os-release
sudo dpkg --audit
apt-mark showhold
df -h / /var
df -i / /var
sudo tail -n 40 /var/log/apt/history.log
sudo tail -n 40 /var/log/dpkg.log

No output from dpkg --audit means that this check has no incomplete package state to report; it does not prove that every application works. Investigate the reason for any hold before removing it. Check both byte capacity and free inodes, the filesystem objects needed to represent files and directories. If /var is a separate filesystem, inspect that filesystem rather than assuming free space on / is sufficient.

Error clues Targets to check first Do Not Use as a Shortcut
Could not get lock Ongoing apt, dpkg, automatic updates Delete lock file
No space left on device Space and inodes on the target filesystem Delete system files of unknown cause
dpkg was interrupted Interruption, audit results and logs Directly edit database files
unmet dependencies / held broken packages Candidate, repositories, pins and holds Arbitrarily add testing or unstable repositories

Identify the Process Holding a Lock

Package tools use process-held locks to prevent simultaneous changes to the same database. A lock file existing on disk does not prove that a process currently holds its lock. The Debian dpkg FAQ explains why deleting lock files is not a correct solution.

sudo lslocks -u -o PID,COMMAND,TYPE,PATH
systemctl status apt-daily.service apt-daily-upgrade.service --no-pager
sudo journalctl -u apt-daily-upgrade.service -b --no-pager -n 30

lslocks currently checks locks and PIDs, and -u prevents truncating long paths. Support was confirmed on util-linux 2.41.5 in the practice VM. Check /var/lib/dpkg/lock-frontend, /var/lib/dpkg/lock, and APT list and archive lock paths and compare them with the PID shown in the error.

read -r -p 'PID reported by the error or lock listing: ' lock_pid
ps -fp "$lock_pid"

If a normal update is in progress, wait until it finishes. Do not assume that the installation has stopped just because the screen appears frozen; check the logs, process status, and runtime together. Forced termination is a separate recovery decision that considers the possibility of normal completion and the scope of impact, and kill -9 is not executed as a simple first step.

In the verification, instead of the actual dpkg lock, a process lock was created on a separate temporary file. Collisions with other processes were rejected and visible in lslocks, and it was confirmed that the lock could be obtained again when closed without deleting the file. We did not intentionally interrupt an ongoing dpkg operation to reproduce errors.

Review Interrupted Configuration and Dependency Repair

Only review the following after other package operations have finished and you have confirmed there is enough space, and only if audit and logs point to a stopped configuration.

sudo dpkg --configure -a
sudo apt-get check
sudo apt-get -s -f install

dpkg --configure -a processes packages that are unpacked but not yet configured. Since package management scripts and service operations may run, it is not a simple query command. If errors persist, read which package's script failed and resolve causes related to disk, configuration, or dependencies.

apt-get check inspects dependencies, and -s -f install simulates a plan to fix broken dependencies. If required services, kernels, or administrative tools are included in the removal target, do not approve it. Only execute with sudo apt-get -f install if you understand the plan and have checked backups and impacts. The verification VM was in a normal state, so check and simulated recovery showed zero changes, and it does not claim to have actually repaired a broken database.

For issues with candidates not being found, check Installed, Candidate, and Source with apt-cache policy package-name. An explicitly requested version may be absent from the current repository. A practice apt-get -s install tmux=0.invalid.0 request was rejected with Version was not found, and the package was not installed. This error is different from a corrupted package database.

Decide Whether Backports Are Needed

Backports provides selected newer packages rebuilt for the stable release, generally from Debian testing. It differs from adding the testing repository directly. Keep the stable package when it meets your needs. Before choosing a newer version, identify the needed feature, dependency changes, configuration differences and who will maintain it.

The Debian Backports instructions recommend selecting the particular packages you need. The Backports security FAQ describes best-effort fixes by package maintainers rather than the same security-support commitment as stable. Do not treat backports as a policy for replacing the whole stable system.

The following is a practice comparing candidates and simulated installation on a dedicated VM where tmux is not installed. Tmux is a program that manages multiple work sessions within one terminal. This practice does not involve removing or replacing the existing operational tmux.

apt-cache policy tmux
sudo test ! -e /etc/apt/sources.list.d/lf-course-backports.sources
sudo test ! -e /etc/apt/preferences.d/lf-course-tmux.pref
sudo nano /etc/apt/sources.list.d/lf-course-backports.sources

Stop if either named configuration file already exists or tmux is already installed. Use a clean, separate lab VM rather than removing an operational tmux package to match this example. For the new file, paste the complete definition below. Follow the backup and deb822 guidance in the repository configuration lab.

Types: deb
URIs: http://deb.debian.org/debian
Suites: trixie-backports
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
sudo apt update
apt-cache policy tmux
sudo apt-get -s install tmux
sudo apt-get -s -t trixie-backports install tmux

In the actual comparison, the stable version was 3.5a-3 with priority 500, and the backports version was 3.7c-1~bpo13+1 with priority 100. The default request used the stable version, while the simulated request specifying the target branch with -t selected the backports version. Since the required libevent package was also indicated in the plan, you should not just read and approve the tmux entry. The timing of availability by version and architecture may differ, so base it on the results of your own policy.

If an actual installation is needed, review the entire dependency plan and then remove -s from the corresponding simulated command. The verification in this text only went up to candidate comparison and simulated installation, and it does not indicate that a new tmux has been installed. The -t option specifies the target branch for that request, so it may also affect dependency selection.

Compare Candidates with a Single-Package Pin

Pinning adjusts the selection priority for a package or version. Read the rules in the APT preferences manual and apply them narrowly. Do not assign high priorities to every package in the hope that a larger number will solve an unrelated problem.

sudo nano /etc/apt/preferences.d/lf-course-tmux.pref

Enter the following three lines in a new file. Limit the Package to just tmux, and n refers to the codename condition from the Release information.

Package: tmux
Pin: release n=trixie-backports
Pin-Priority: 600
apt-cache policy tmux
sudo apt-get -s install tmux

In practice, tmux from backports was evaluated at 600, so the candidate for the regular request also changed to 3.7c. Now change only the 600 in the same file to 50 and check again.

sudo nano /etc/apt/preferences.d/lf-course-tmux.pref
apt-cache policy tmux
sudo apt-get -s install tmux
Condition: tmux Remains Uninstalled Backports priority Observed Default Candidate
No user pin 100 Stable version 3.5a
Only tmux pinned to 600 600 backports 3.7c
Change the same pin to 50 50 Stable version 3.5a

This is a comparison where one value is changed in the same repository and the same uninstalled package. If you have already installed the backports version, lowering the priority or removing the repository will not automatically downgrade to the previous version. Because of APT's downgrade rules and dependencies, it is not determined by simple numerical comparison. Do not force priorities above 1000 simply to attempt a downgrade.

Remove Only the Lab Settings and Recheck State

Remove only the two files newly created for this lab. Inspect their exact paths first and preserve any comparison records you need. Leave existing repositories and other administrators’ pin files unchanged.

sudo rm -- /etc/apt/preferences.d/lf-course-tmux.pref
sudo rm -- /etc/apt/sources.list.d/lf-course-backports.sources
sudo apt update
apt-cache policy tmux
sudo dpkg --audit
sudo apt-get check

In the verification VM, it was restored with the same tmux candidate as initially, and the installed package, version, and status list did not change in byte-level comparison. If backports were actually installed, this cleanup alone will not revert to the state before installation. If recovery is needed, create a separate plan based on the changed packages and data.

The completion criteria for resolving errors are not hiding the warning messages, but being able to explain the lock holder, ensuring that the required space and package states are normal, and confirming that the sources, candidates, and dependency plans are as intended. After modification, re-check the originally failed tasks within the affected scope.