apt and dnf commands cheat sheet
Updating the system
-
sudo apt updateDebian and Ubuntu: downloads the package lists again from all the configured repositories, without installing anything. Run it before every install or upgrade.
-
apt list --upgradeableDebian and Ubuntu: lists the packages that have a newer version, based on the lists downloaded by apt update.
-
sudo apt upgradeDebian and Ubuntu: upgrades all the installed packages. The command doesn't remove existing packages, so a package whose upgrade requires removing another package stays at its current version.
-
sudo apt full-upgradeDebian and Ubuntu: upgrades like upgrade, and also removes installed packages when that's needed to upgrade the whole system. Caution: go over the list of removals before you confirm. The equivalent apt-get command is dist-upgrade.
-
sudo apt install --only-upgrade nginxDebian and Ubuntu: upgrades one package that's already installed, and doesn't install it if it's missing.
-
dnf check-updateAlmaLinux, Rocky Linux, RHEL, and Fedora: checks which updates are available, without installing them. In DNF5, the command is named check-upgrade.
-
sudo dnf upgradeAlmaLinux, Rocky Linux, RHEL, and Fedora: refreshes the repository metadata as needed and upgrades all the installed packages. No separate refresh command is needed.
-
sudo dnf upgrade nginxUpgrades only one package to the latest version in the repositories.
-
sudo dnf makecacheDownloads and caches the metadata of the enabled repositories, without installing anything, much like apt update.
-
sudo dnf distro-syncCaution: matches the installed packages to the versions in the enabled repositories, so it may also downgrade a package. Go over the list before you confirm.
Installing and removing
-
sudo apt install nginxDebian and Ubuntu: installs a package with all its dependencies. You can list several packages separated by spaces.
-
sudo apt reinstall nginxDebian and Ubuntu: reinstalls a package that's already installed, for example after one of its files was deleted by mistake.
-
sudo apt remove nginxDebian and Ubuntu: removes a package and leaves its configuration files on the server.
-
sudo apt purge nginxCaution: removes a package together with its configuration files in the system directories, and they can't be recovered afterward. We recommend backing up the configuration files before you run it. The command doesn't touch files in the users' home directories.
-
sudo apt autoremoveCaution: removes packages that were installed automatically as dependencies and are no longer needed. A package installed manually isn't offered for removal, but go over the list before you confirm.
-
sudo apt-get -s upgradeDebian and Ubuntu: simulates the action and shows what would happen, without any change to the server. The -s option also works with install and remove.
-
sudo dnf install nginxAlmaLinux, Rocky Linux, RHEL, and Fedora: installs a package with all its dependencies.
-
sudo dnf reinstall nginxReinstalls a package that's already installed, from the version in the repositories.
-
sudo dnf remove nginxCaution: removes the package and also every package that depends on it. Check the list shown for what will be removed before you confirm.
-
sudo dnf autoremoveCaution: removes packages that were installed automatically as dependencies and are no longer needed by any package. Go over the list before you confirm.
-
sudo dnf downgrade nginxReturns a package to the previous version available in the repositories, for example when an update caused a problem.
Searching and package information
-
apt search nginxDebian and Ubuntu: searches the available packages by name and description.
-
apt show nginxDebian and Ubuntu: shows details about a package: version, size, dependencies, source repository, and description.
-
apt list --installedDebian and Ubuntu: lists all the installed packages. You can add a name or pattern, for example apt list --installed 'php*'.
-
apt policy nginxDebian and Ubuntu: shows the installed version, the version that will be installed, and the repositories each version is available from.
-
apt-mark showmanualDebian and Ubuntu: lists the packages installed manually, without the packages installed automatically as dependencies.
-
dnf search nginxAlmaLinux, Rocky Linux, RHEL, and Fedora: searches packages by name and summary. The search is case-insensitive.
-
dnf info nginxShows details about an installed or available package: version, repository, size, and description.
-
dnf list --installedLists all the installed packages. You can add a name or pattern, for example dnf list --installed 'php*'.
-
dnf list --upgradesLists the updates available for the installed packages.
-
dnf repoquery --userinstalledLists the packages installed by the user, without the packages installed as dependencies.
Which package provides a file
-
dpkg -S /etc/host.confDebian and Ubuntu: shows which installed package a file belongs to.
-
dpkg -L nginxDebian and Ubuntu: lists all the files an installed package placed on the server.
-
sudo apt install apt-file && sudo apt updateDebian and Ubuntu: installs apt-file and downloads the repository file lists it needs for searching.
-
apt-file search bin/htopSearches which package contains a file, even when the package isn't installed. Useful when a command is missing on the server and it's not clear which package to install.
Example:
htop: /usr/bin/htop -
apt-file list nginxLists the files in a package, even when it isn't installed.
-
dnf provides /usr/bin/digAlmaLinux, Rocky Linux, RHEL, and Fedora: shows which packages provide a file or command, including packages that aren't installed.
-
dnf repoquery -l nginxLists the files in a package, even when it isn't installed.
-
rpm -qf /etc/nginx/nginx.confShows which installed package a file belongs to.
-
rpm -ql nginxLists all the files an installed package placed on the server.
History and clearing the cache
-
dnf history listLists the dnf transactions: transaction ID, command, date, and number of packages. In DNF4, you can also type just dnf history.
-
dnf history info lastShows the details of the last transaction: which packages were installed, updated, or removed. Instead of last, you can type the transaction ID.
-
sudo dnf history undo lastCaution: undoes the last transaction, which means it removes what was installed in it and brings back what was removed. Check the list before you confirm.
-
sudo dnf history rollback 12Caution: undoes all the transactions made after transaction 12 and returns the packages to the state right after it. Useful for recovering from a series of changes. Check the list before you confirm.
-
grep " install " /var/log/dpkg.logDebian and Ubuntu: shows from the dpkg log which packages were installed and when.
-
sudo apt cleanDebian and Ubuntu: deletes all the downloaded package files kept in the cache and frees up disk space.
-
sudo apt autocleanDebian and Ubuntu: deletes from the cache only package files that are no longer available for download.
-
sudo dnf clean allAlmaLinux, Rocky Linux, RHEL, and Fedora: deletes the whole dnf cache, including the repository metadata and downloaded packages. Useful when a repository changes or metadata errors appear.
Locking versions and preventing updates
-
sudo apt-mark hold nginxDebian and Ubuntu: locks a package at its current version, so it isn't upgraded, reinstalled, or removed automatically.
-
sudo apt-mark unhold nginxRemoves the lock, and the package gets updates as usual again.
-
apt-mark showholdLists all the locked packages.
-
sudo dnf versionlock add nginxAlmaLinux, Rocky Linux, RHEL, and Fedora: locks a package at its current version. In DNF5 the command is built in, and in DNF4 you first need to install the versionlock plugin.
-
dnf versionlock listLists all the existing locks.
-
sudo dnf versionlock delete nginxRemoves the lock on the package. dnf versionlock clear removes all the locks.
-
sudo dnf upgrade --exclude='kernel*'Upgrades all the packages except those matching the pattern, for this run only. We recommend putting the pattern in quotes.
Repositories
-
ls /etc/apt/sources.list.d/Debian and Ubuntu: lists the repository files. On newer releases, repositories are defined in .sources files in the deb822 format, for example ubuntu.sources, and on older releases in the /etc/apt/sources.list file and in .list files.
-
sudo add-apt-repository ppa:user/ppa-nameUbuntu only: adds a PPA repository and enables it. Add only repositories from a known and trusted source. The command comes in the software-properties-common package.
-
sudo add-apt-repository -r ppa:user/ppa-nameUbuntu: removes a repository that was added. Packages already installed from it stay installed.
-
dnf repolistAlmaLinux, Rocky Linux, RHEL, and Fedora: lists the enabled repositories. With --all, disabled repositories are listed too. In DNF5 the new name is dnf repo list, and the old name still works.
-
sudo dnf install epel-releaseAlmaLinux and Rocky Linux: installs and enables the EPEL repository, which provides extra packages that aren't in the distribution repositories.
-
sudo dnf config-manager --set-enabled crbAlmaLinux and Rocky Linux: permanently enables the CRB repository, which some EPEL packages depend on. The command requires dnf-plugins-core, and this syntax is for DNF4.
-
sudo dnf config-manager setopt myrepo.enabled=0DNF5: permanently disables a repository, and enabled=1 enables it. The change is saved in a separate file, and the original repository file stays unchanged.
-
sudo dnf --enablerepo=myrepo install nginxEnables a disabled repository for this command only, without a permanent change to the settings. --disablerepo works the same way to disable a repository for one command.
Security updates
-
sudo apt install unattended-upgradesDebian and Ubuntu: installs the automatic updates mechanism. On Ubuntu Server it's preinstalled and installs security updates once a day.
-
sudo unattended-upgrade -v --dry-runSimulates a run of the automatic updates and shows what would be installed, without any change. The settings are in the /etc/apt/apt.conf.d/50unattended-upgrades and 20auto-upgrades files, and the logs in the /var/log/unattended-upgrades directory.
-
dnf updateinfo list --securityAlmaLinux, Rocky Linux, RHEL, and Fedora: lists the security updates available for the installed packages. In DNF5 the new name is dnf advisory list --security, and the old name still works.
-
sudo dnf upgrade --securityInstalls only updates that have a security advisory, without the other updates.
-
sudo dnf install dnf-automaticAlmaLinux, Rocky Linux, and RHEL: installs the automatic updates mechanism. The settings are in the /etc/dnf/automatic.conf file: upgrade_type = security limits it to security updates, and apply_updates = yes actually installs them.
-
sudo systemctl enable --now dnf-automatic.timerEnables the automatic updates schedule, according to the settings in the file. On Fedora with DNF5, the timer is named dnf5-automatic.timer.
Local packages: dpkg and rpm
-
sudo dpkg -i package.debDebian and Ubuntu: installs a .deb file downloaded to the server. dpkg doesn't download missing dependencies.
-
sudo apt-get -f installFixes broken dependencies and completes an installation that stopped, for example after dpkg -i failed because of a missing dependency.
-
sudo dpkg --configure -aCompletes the configuration of packages that were unpacked but not configured, for example after an installation was interrupted midway.
-
dpkg -l | grep nginxLists the installed packages whose name contains the word, with their version and state. ii at the start of the line marks an installed package.
-
sudo dnf install ./package.rpmAlmaLinux, Rocky Linux, RHEL, and Fedora: installs a local .rpm file and downloads its dependencies from the repositories.
-
rpm -qa | grep nginxLists all the installed packages whose name contains the word, including the version.
-
rpm -qi nginxShows information about an installed package: version, install date, source, and description.
-
rpm -q --changelog nginx | head -20Shows the latest changes in the package, for example to check whether a specific security fix is already included.
-
rpm -VaChecks all the files of the installed packages against the information saved at install time, and shows only files that changed, for example in size, permissions, or content.
-
sudo rpm -e --nodeps nginxExtreme caution: removes a package without checking dependencies, so programs that depend on it may stop working. For a normal removal, use dnf remove.
No matching commands.
apt or dnf: which command fits your server
- apt: The package manager of Debian, Ubuntu, and the distributions based on them. The apt command is meant for interactive use in the terminal, and in scripts we recommend apt-get and apt-cache, whose output is more stable across versions.
- dnf: The package manager of AlmaLinux, Rocky Linux, RHEL, and Fedora. On these distributions, the yum command points to dnf.
- dpkg and rpm: The tools underneath the package managers. They work with a package file or an installed package, and they don't download dependencies from the repositories.
DNF5 and DNF4
On current Fedora releases, the dnf command runs DNF5, while on AlmaLinux, Rocky Linux, and RHEL it's still DNF4. Most commands on this page are the same in both, and these are the main differences:
- Checking for updates: dnf check-update in DNF4, and dnf check-upgrade in DNF5.
- Security advisories: In DNF5, the updateinfo command was renamed advisory, and the old name still works.
- Enabling and disabling a repository: DNF4 uses config-manager --set-enabled, and DNF5 uses config-manager setopt with repoid.enabled=1.
- Version locking: versionlock is built into DNF5, and in DNF4 it's a plugin you install separately.
- Automatic updates: DNF5 has a single timer named dnf5-automatic.timer, which works according to the configuration file.
To check which version is installed on the server, run dnf --version. The package name nginx on this page is an example, so replace it with the real package name. General commands for working on a server are on the Linux commands page.
On a Linux VPS, you connect over SSH with full root access and manage the packages on the server with every command on this page: system and security updates, installing and removing software, extra repositories, and version locks.
Related pages
apt and dnf commands FAQ
What is the difference between apt and dnf?
Both manage software packages: they install, upgrade, and remove software together with its dependencies. The difference is the distribution. apt is used on Debian and Ubuntu and works with .deb packages, and dnf is used on AlmaLinux, Rocky Linux, RHEL, and Fedora and works with .rpm packages. Most actions exist in both, sometimes under a different name, so this page shows them side by side.
What is the difference between apt upgrade and apt full-upgrade?
apt upgrade upgrades the installed packages and never removes a package, so a package whose upgrade requires removing another package stays at its current version. apt full-upgrade completes those upgrades too, and removes packages when needed. For routine use, apt upgrade is enough, and when you run full-upgrade, we recommend going over the list of removals before you confirm.
How do I install only security updates on a server?
On AlmaLinux, Rocky Linux, and RHEL, run sudo dnf upgrade --security, and for automatic installation, set up dnf-automatic with upgrade_type = security. On Ubuntu Server, unattended-upgrades is preinstalled and installs security updates automatically once a day, and its settings are in the /etc/apt/apt.conf.d directory.
How do I stop a specific package from being updated?
On Debian and Ubuntu, run sudo apt-mark hold with the package name, and with dnf, use dnf versionlock add. The package stays at its current version even when you upgrade everything else. We recommend removing the lock once the reason for it is gone, so the package gets security updates again.
How do I find which package to install for a missing command?
With dnf, run dnf provides with the path of the command, for example dnf provides /usr/bin/dig, and the result shows the package name. On Debian and Ubuntu, install apt-file, run apt update, and then run apt-file search with the file name. For a file that's already on the server, dpkg -S or rpm -qf is enough.