apt and dnf commands cheat sheet

This cheat sheet brings together apt and dnf commands for managing software packages on Linux servers, side by side: updating the system, installing and removing packages, searching, finding the package that provides a file, locking versions, repositories, and security updates. The apt commands are used on Debian and Ubuntu, and the dnf commands on AlmaLinux, Rocky Linux, RHEL, and Fedora. You can search for a command, filter by topic, and copy it with one click.

Updating the system

  • sudo apt update

    Debian and Ubuntu: downloads the package lists again from all the configured repositories, without installing anything. Run it before every install or upgrade.

  • apt list --upgradeable

    Debian and Ubuntu: lists the packages that have a newer version, based on the lists downloaded by apt update.

  • sudo apt upgrade

    Debian 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-upgrade

    Debian 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 nginx

    Debian and Ubuntu: upgrades one package that's already installed, and doesn't install it if it's missing.

  • dnf check-update

    AlmaLinux, Rocky Linux, RHEL, and Fedora: checks which updates are available, without installing them. In DNF5, the command is named check-upgrade.

  • sudo dnf upgrade

    AlmaLinux, 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 nginx

    Upgrades only one package to the latest version in the repositories.

  • sudo dnf makecache

    Downloads and caches the metadata of the enabled repositories, without installing anything, much like apt update.

  • sudo dnf distro-sync

    Caution: 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 nginx

    Debian and Ubuntu: installs a package with all its dependencies. You can list several packages separated by spaces.

  • sudo apt reinstall nginx

    Debian and Ubuntu: reinstalls a package that's already installed, for example after one of its files was deleted by mistake.

  • sudo apt remove nginx

    Debian and Ubuntu: removes a package and leaves its configuration files on the server.

  • sudo apt purge nginx

    Caution: 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 autoremove

    Caution: 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 upgrade

    Debian 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 nginx

    AlmaLinux, Rocky Linux, RHEL, and Fedora: installs a package with all its dependencies.

  • sudo dnf reinstall nginx

    Reinstalls a package that's already installed, from the version in the repositories.

  • sudo dnf remove nginx

    Caution: 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 autoremove

    Caution: 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 nginx

    Returns a package to the previous version available in the repositories, for example when an update caused a problem.

Searching and package information

  • apt search nginx

    Debian and Ubuntu: searches the available packages by name and description.

  • apt show nginx

    Debian and Ubuntu: shows details about a package: version, size, dependencies, source repository, and description.

  • apt list --installed

    Debian and Ubuntu: lists all the installed packages. You can add a name or pattern, for example apt list --installed 'php*'.

  • apt policy nginx

    Debian and Ubuntu: shows the installed version, the version that will be installed, and the repositories each version is available from.

  • apt-mark showmanual

    Debian and Ubuntu: lists the packages installed manually, without the packages installed automatically as dependencies.

  • dnf search nginx

    AlmaLinux, Rocky Linux, RHEL, and Fedora: searches packages by name and summary. The search is case-insensitive.

  • dnf info nginx

    Shows details about an installed or available package: version, repository, size, and description.

  • dnf list --installed

    Lists all the installed packages. You can add a name or pattern, for example dnf list --installed 'php*'.

  • dnf list --upgrades

    Lists the updates available for the installed packages.

  • dnf repoquery --userinstalled

    Lists the packages installed by the user, without the packages installed as dependencies.

Which package provides a file

  • dpkg -S /etc/host.conf

    Debian and Ubuntu: shows which installed package a file belongs to.

  • dpkg -L nginx

    Debian and Ubuntu: lists all the files an installed package placed on the server.

  • sudo apt install apt-file && sudo apt update

    Debian and Ubuntu: installs apt-file and downloads the repository file lists it needs for searching.

  • apt-file search bin/htop

    Searches 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 nginx

    Lists the files in a package, even when it isn't installed.

  • dnf provides /usr/bin/dig

    AlmaLinux, Rocky Linux, RHEL, and Fedora: shows which packages provide a file or command, including packages that aren't installed.

  • dnf repoquery -l nginx

    Lists the files in a package, even when it isn't installed.

  • rpm -qf /etc/nginx/nginx.conf

    Shows which installed package a file belongs to.

  • rpm -ql nginx

    Lists all the files an installed package placed on the server.

History and clearing the cache

  • dnf history list

    Lists the dnf transactions: transaction ID, command, date, and number of packages. In DNF4, you can also type just dnf history.

  • dnf history info last

    Shows 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 last

    Caution: 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 12

    Caution: 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.log

    Debian and Ubuntu: shows from the dpkg log which packages were installed and when.

  • sudo apt clean

    Debian and Ubuntu: deletes all the downloaded package files kept in the cache and frees up disk space.

  • sudo apt autoclean

    Debian and Ubuntu: deletes from the cache only package files that are no longer available for download.

  • sudo dnf clean all

    AlmaLinux, 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 nginx

    Debian and Ubuntu: locks a package at its current version, so it isn't upgraded, reinstalled, or removed automatically.

  • sudo apt-mark unhold nginx

    Removes the lock, and the package gets updates as usual again.

  • apt-mark showhold

    Lists all the locked packages.

  • sudo dnf versionlock add nginx

    AlmaLinux, 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 list

    Lists all the existing locks.

  • sudo dnf versionlock delete nginx

    Removes 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-name

    Ubuntu 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-name

    Ubuntu: removes a repository that was added. Packages already installed from it stay installed.

  • dnf repolist

    AlmaLinux, 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-release

    AlmaLinux 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 crb

    AlmaLinux 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=0

    DNF5: 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 nginx

    Enables 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-upgrades

    Debian 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-run

    Simulates 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 --security

    AlmaLinux, 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 --security

    Installs only updates that have a security advisory, without the other updates.

  • sudo dnf install dnf-automatic

    AlmaLinux, 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.timer

    Enables 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.deb

    Debian and Ubuntu: installs a .deb file downloaded to the server. dpkg doesn't download missing dependencies.

  • sudo apt-get -f install

    Fixes broken dependencies and completes an installation that stopped, for example after dpkg -i failed because of a missing dependency.

  • sudo dpkg --configure -a

    Completes the configuration of packages that were unpacked but not configured, for example after an installation was interrupted midway.

  • dpkg -l | grep nginx

    Lists 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.rpm

    AlmaLinux, Rocky Linux, RHEL, and Fedora: installs a local .rpm file and downloads its dependencies from the repositories.

  • rpm -qa | grep nginx

    Lists all the installed packages whose name contains the word, including the version.

  • rpm -qi nginx

    Shows information about an installed package: version, install date, source, and description.

  • rpm -q --changelog nginx | head -20

    Shows the latest changes in the package, for example to check whether a specific security fix is already included.

  • rpm -Va

    Checks 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 nginx

    Extreme caution: removes a package without checking dependencies, so programs that depend on it may stop working. For a normal removal, use dnf remove.

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.

Did you know?

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.

Linux VPS plans

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.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement