Package Information Queries
Learn how to query package information in Linux systems, including tools, commands, and key metadata fields used in package management.
Package Information Queries refer to the set of commands and techniques used to retrieve detailed information about software packages installed on or available to a Linux system. These queries enable system administrators and users to inspect package metadata, verify installation status, view version details, dependencies, file lists, changelogs, and other relevant data that facilitates package management, troubleshooting, and system auditing.
Querying Installed Package Information
Checking if a Package is Installed
Determining if a particular package is installed is fundamental. Package managers provide commands to verify the installation status by querying the local package database.
- For Debian-based systems using
dpkg:
dpkg -l package_name
- For Red Hat-based systems using
rpm:
rpm -q package_name
These commands return the package version and indicate installation or absence accordingly.
Retrieving Package Version
To obtain the exact version of an installed package:
- Debian-based:
dpkg-query -W -f='${Version}\n' package_name
- RPM-based:
rpm -q --queryformat '%{VERSION}-%{RELEASE}\n' package_name
This helps in verifying whether the package meets version requirements.
Listing Files Installed by a Package
Knowing which files belong to a package assists in management and troubleshooting.
- Debian-based:
dpkg -L package_name
- RPM-based:
rpm -ql package_name
This outputs the complete list of files installed by the package on the system.
Querying Package Metadata and Dependencies
Viewing Package Dependencies
Understanding package dependencies is crucial for system stability and updates.
- Debian-based:
apt-cache depends package_name
or
apt show package_name
- RPM-based:
rpm -qR package_name
These commands list the required packages or capabilities that must be present for the package to function.
Checking Reverse Dependencies
Reverse dependencies show which installed packages depend on a given package, useful before removal.
- Debian-based:
apt-cache rdepends package_name
- RPM-based:
repoquery --whatrequires package_name
Note: repoquery is part of the yum-utils package.
Accessing Package Description and Summary
To read the package’s purpose and description:
- Debian-based:
apt show package_name
- RPM-based:
rpm -qi package_name
This information helps in identifying the package’s role and functionality.
Querying Package Status and History
Checking Package Installation Status and State
Some tools provide detailed status beyond installed or not, such as configuration or broken status.
- Debian-based:
dpkg -s package_name
This provides package status, including whether it is installed, unpacked, configured, or held.
Viewing Changelog and Package Updates
Inspecting the changelog helps understand recent changes or fixes.
- Debian-based:
zless /usr/share/doc/package_name/changelog.Debian.gz
- RPM-based:
rpm -q --changelog package_name
Querying Package Origin and Repository Information
Identifying the repository or source of a package is important for security and update management.
- Debian-based:
apt-cache policy package_name
- RPM-based:
yum info package_name
or
dnf info package_name
This shows the repository from which the package was installed.
Advanced Querying Techniques
Querying Package Files for Ownership
To find which package owns a specific file (reverse file query):
- Debian-based:
dpkg -S /path/to/file
- RPM-based:
rpm -qf /path/to/file
This is useful when determining which package provides or installed a given file.
Using Query Formatting Options
Many package managers allow custom formatting of query output to extract specific fields.
Example with RPM:
rpm -qi --queryformat '%{NAME} %{VERSION} %{ARCH}\n' package_name
This prints the package name, version, and architecture in a concise format.
Querying Package Scripts
Packages often include installation or removal scripts (preinst, postinst, prerm, postrm), accessible for inspection.
- Debian-based:
These scripts reside under /var/lib/dpkg/info/ as:
package_name.preinst
package_name.postinst
package_name.prerm
package_name.postrm
- RPM-based:
Scripts can be viewed with:
rpm -q --scripts package_name
Reviewing these scripts aids in understanding package behavior during installation or removal.
Summary of Common Package Query Commands
| Query Purpose | Debian-based Command | RPM-based Command |
|---|---|---|
| Check if installed | dpkg -l package_name | rpm -q package_name |
| Get package version | dpkg-query -W -f='${Version}\n' package_name | rpm -q --queryformat '%{VERSION}-%{RELEASE}\n' package_name |
| List installed files | dpkg -L package_name | rpm -ql package_name |
| Show package dependencies | apt-cache depends package_name | rpm -qR package_name |
| Show reverse dependencies | apt-cache rdepends package_name | repoquery --whatrequires package_name |
| Show package description | apt show package_name | rpm -qi package_name |
| Check package status | dpkg -s package_name | rpm -qi package_name |
| View changelog | zless /usr/share/doc/package_name/changelog.Debian.gz | rpm -q --changelog package_name |
| Find package owning a file | dpkg -S /path/to/file | rpm -qf /path/to/file |
| Show installation scripts | /var/lib/dpkg/info/package_name.* | rpm -q --scripts package_name |
| Show package origin/repo | apt-cache policy package_name | yum info package_name or dnf info package_name |
Practical Use Cases
- Verifying Package Installation: Confirm if a critical package is installed and its version complies with system requirements.
- Dependency Analysis: Before installing or removing a package, check required dependencies or reverse dependencies to avoid breaking the system.
- File Ownership Troubleshooting: Identify which package installed a specific file causing issues or conflicts.
- Security Audits: Check package origin and changelogs to ensure packages come from trusted sources and are up to date.
- System Maintenance: Review installation scripts and statuses to diagnose installation problems or configure packages properly.
Package Information Queries form an essential component of Linux system administration, enabling thorough inspection and management of software packages through precise and flexible querying capabilities provided by package management tools.