How to Check if a Reboot is Required After Updates on RHEL/CentOS via Command Line


2 views

On Ubuntu-based systems, checking for pending reboots is straightforward—just look for the existence of /var/run/reboot-required. However, Red Hat Enterprise Linux (RHEL) and CentOS handle this differently. Here’s how you can determine if a reboot is needed after applying updates on these systems.

needs-restarting Utility /h2>

The most reliable method is to use the needs-restarting tool, part of the yum-utils package. This utility checks whether running processes are using outdated libraries or if the kernel has been updated.

# Install yum-utils if not already present
sudo yum install -y yum-utils

# Check if a reboot is required
sudo needs-restarting -r

If a reboot is needed, the output will explicitly state it. For example:

Core libraries or services have been updated:
  kernel -> 3.10.0-1160.45.1.el7
  systemd -> 219-78.el7_9.3

Reboot is required to ensure system stability.

Another way is to compare the running kernel version with the latest installed kernel:

# Check the currently running kernel
uname -r

# List all installed kernels
rpm -q kernel

If the latest installed kernel doesn’t match the running one, a reboot is required.

dnf Plugin (RHEL 8/CentOS 8+) /h2>

For newer RHEL/CentOS versions, the dnf package manager includes a plugin for this purpose:

# Enable the plugin if not already active
sudo dnf install -y dnf-plugin-needs-restarting

# Check for pending reboots
sudo dnf needs-restarting -r

For scripting or monitoring, you can use the exit code of needs-restarting -r:

sudo needs-restarting -r &> /dev/null
if [ $? -eq 1 ]; then
  echo "Reboot required!"
else
  echo "No reboot needed."
fi

/var/run/reboot-required /h2>

While RHEL/CentOS don’t create this file by default, you can simulate Ubuntu’s behavior with a simple script:

#!/bin/bash
if sudo needs-restarting -r &> /dev/null; then
  touch /var/run/reboot-required
else
  rm -f /var/run/reboot-required
fi

This creates the file only when a reboot is pending.

Unlike Ubuntu, RHEL and CentOS require specific tools to check for pending reboots. The needs-restarting utility is the most comprehensive method, but kernel version checks or dnf plugins also work well. Automating these checks can help maintain system stability after updates.


Unlike Debian-based systems that create a /var/run/reboot-required flag, RHEL and CentOS handle reboot notifications differently. The need for reboot typically occurs when:

  • Kernel updates are installed
  • Critical libraries like glibc are updated
  • Systemd or other core components receive updates

The most reliable way is to use the needs-restarting tool from yum-utils package:

# First install yum-utils if not present
sudo yum install -y yum-utils

# Check for services needing restart
needs-restarting -s

# Check for processes using old libraries
needs-restarting -r

For modern RHEL 8+ and CentOS Stream:

sudo dnf install python3-dnf-plugin-needs-restarting
dnf needs-restarting

Compare the running kernel vs. installed kernels:

# Current running kernel
uname -r

# Installed kernels
rpm -q kernel

If the running kernel is older than the newest installed kernel, a reboot is needed.

# List unused kernels (implies reboot needed if multiple entries)
package-cleanup --oldkernels --count=1

Sometimes pending updates might require reboot:

sudo yum check-update
# Or for dnf systems:
sudo dnf check-update

Here's a bash function you can add to your scripts:

check_reboot_required() {
  if [ -f /var/run/reboot-required ]; then
    return 0
  elif command -v needs-restarting >/dev/null && \
       needs-restarting -r &>/dev/null; then
    return 0
  elif [ "$(rpm -q kernel | wc -l)" -gt 1 ] && \
       [ "$(uname -r)" != "$(rpm -q kernel --last | head -1 | cut -d' ' -f1 | cut -d'-' -f1-3)" ]; then
    return 0
  fi
  return 1
}

For system administrators managing multiple servers, consider these approaches:

# Create a custom Nagios check
#!/bin/bash
if needs-restarting -r &>/dev/null; then
  echo "WARNING: System requires reboot"
  exit 1
else
  echo "OK: No reboot required"
  exit 0
fi

If needs-restarting isn't available:

# RHEL 7/CentOS 7
sudo yum install yum-utils

# RHEL 8+/CentOS Stream
sudo dnf install dnf-utils