Linux6 min readPublished: 25 Sept 2024Updated: 9 Nov 2026

How to Safely Update and Patch a Linux Server

Apply security patches to a Linux server without breaking the sites on it: what to check first, how to separate security from feature updates, and rollback.

G7Cloud Engineering
Platform team
Share:
Abstract illustration representing how to safely update and patch a linux server.
  • Keep security updates separate from feature updates. Security patches can be applied automatically; version upgrades should not be.
  • apt list --upgradable and dnf check-update show what will change before anything happens.
  • Take a snapshot or backup before a manual patch run. People skip the way back, then need it.
  • Turn on unattended-upgrades or dnf-automatic for security patches only, and read the emails they send.
  • Restarting the affected service is usually enough. Only the kernel and core libraries need a full reboot.

How do I safely apply updates to a Linux server?

Check what will change before you change it. Apply security patches (fixes for known vulnerabilities) first, restart only what needs it, then check the site still works. The order matters: most bad patch nights come from applying everything at once, then hunting for which change broke something.

Debian and Ubuntu: the safe sequence
# 1. Refresh the package lists. Changes nothing yet.
sudo apt update

# 2. See exactly what would be upgraded
apt list --upgradable

# 3. Which of those are SECURITY updates?
apt list --upgradable 2>/dev/null | grep -i security

# 4. Apply security updates only
sudo unattended-upgrade --dry-run -d      # preview
sudo unattended-upgrade -d                # apply

# 5. Or apply everything, once you have read step 2
sudo apt upgrade

# 6. Does anything need a reboot?
[ -f /var/run/reboot-required ] && cat /var/run/reboot-required.pkgs
apt upgrade never removes a package. apt full-upgrade can, so read its output before you confirm.
AlmaLinux, Rocky and RHEL
# What is available
sudo dnf check-update

# Security updates only
sudo dnf updateinfo list security
sudo dnf update --security

# Everything
sudo dnf update

# Does anything need a restart?
sudo needs-restarting -r      # 0 = no reboot needed
sudo needs-restarting -s      # services running on old libraries
apt has no direct match for dnf update --security, so security-only patching is easier on the RHEL family.

apt upgrade and apt full-upgrade are not the same

apt upgrade only installs newer versions of what is already there. apt full-upgrade (once called dist-upgrade) removes packages when a dependency needs it, and on a server that can mean removing something a site relies on. Read the summary line before you type y.

What should I check before patching?

  1. 1Take a snapshot if your provider offers one. It is the only way back that covers everything, including a kernel that will not boot. Take it before you start.
  2. 2Back up the database separately. A snapshot taken while the database is writing may restore into a broken state. Dump the database first, then take the snapshot.
  3. 3Check free disk space. Upgrades need room to unpack. /boot in particular fills up with old kernels and then fails mid-upgrade, which is awkward to recover from.
  4. 4Read the changelog for anything major. apt changelog nginx before upgrading a package your site relies on takes a minute and sometimes saves an evening.
  5. 5Know how you get in if SSH breaks. Console access through your provider's panel, tested before you need it.
Pre-flight
# Disk space, especially /boot
df -h / /var /boot

# Old kernels filling /boot (Debian / Ubuntu)
dpkg --list | grep linux-image
sudo apt autoremove --purge

# Database dump before anything else
mysqldump --single-transaction --routines --all-databases \
  | gzip > /var/backups/pre-patch-$(date +%F).sql.gz

# Record current versions so you can compare afterwards
dpkg -l > /var/backups/packages-before-$(date +%F).txt

# Anything already broken? Fix it before adding changes.
systemctl --failed
A /boot partition at 90 percent will fail a kernel upgrade halfway through. Clear old kernels first.
Pro Tip

Run the whole session inside tmux. If SSH drops mid-upgrade, apt or dnf keeps running and you can reconnect. Without it, a dropped connection part-way through an install leaves the package database locked and half set up.

Should I enable automatic security updates?

Yes, for security patches. A known vulnerability left unpatched for weeks on a server open to the internet is a far bigger risk than a security patch breaking something. Automatic feature upgrades are different, and should stay off.

Debian and Ubuntu: unattended-upgrades
sudo apt install -y unattended-upgrades apt-listchanges
sudo dpkg-reconfigure -plow unattended-upgrades

# /etc/apt/apt.conf.d/50unattended-upgrades
Unattended-Upgrade::Allowed-Origins {
    "${distro_id}:${distro_codename}-security";
};
Unattended-Upgrade::Mail "alerts@example.com";
Unattended-Upgrade::MailReport "on-change";
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Automatic-Reboot "false";

# Confirm it is actually running
systemctl status unattended-upgrades
sudo unattended-upgrade --dry-run -d
Only the -security source is listed. Leave Automatic-Reboot false so a kernel patch never restarts your server unannounced.
RHEL family: dnf-automatic
sudo dnf install -y dnf-automatic

# /etc/dnf/automatic.conf
# upgrade_type = security
# apply_updates = yes
# emit_via = email
# email_to = alerts@example.com

sudo systemctl enable --now dnf-automatic.timer
systemctl list-timers dnf-automatic.timer
upgrade_type = security is the line that matters. The default is default, which applies everything.

Automatic-Reboot false, always

Set it to true and a kernel security patch can restart your live server at 6am with no warning and nobody watching. Leave it false, let the tool tell you a reboot is waiting, and schedule it yourself. See rebooting a Linux server safely.

Update typeAutomate?Why
Security patchesYesFixes copied back into your current version, with little else changed
Regular package upgradesNoFeature changes can break configuration
Kernel updatesInstall yes, reboot noNeeds a reboot you schedule
Distribution release upgradesNeverA major change: plan it, test it and do it by hand
Third-party repository packagesNoNobody has tested them against your setup

What makes automation safe is keeping security patches apart from everything else.

What do I do after applying updates?

Post-patch verification
# Which services are running against libraries that no longer exist?
sudo needs-restarting -s                 # RHEL family
sudo checkrestart                        # Debian, from debian-goodies

# Restart the ones that need it, individually
sudo systemctl restart nginx php8.3-fpm

# Anything failed?
systemctl --failed
sudo journalctl -p err --since '30 min ago'

# Do the versions you care about still work?
nginx -v && php -v && mysql --version

# Test the site from outside the server
curl -s -o /dev/null -w 'HTTP %{http_code} in %{time_total}s\n' https://example.com/
Restarting single services is quicker and safer than rebooting, and covers most library updates.

Config file prompts need a decision

When apt asks whether to keep your version of a config file or take the package's, keep yours (N, the default). Then compare them with diff /etc/nginx/nginx.conf /etc/nginx/nginx.conf.dpkg-dist and copy across anything new by hand. Taking the package version overwrites your settings.

Rolling back a bad package
# What was installed and when
grep ' install \| upgrade ' /var/log/dpkg.log | tail -30      # Debian / Ubuntu
sudo dnf history                                              # RHEL family

# Undo an entire dnf transaction. Genuinely useful.
sudo dnf history undo 42

# Downgrade a single package on Debian / Ubuntu
apt list -a nginx
sudo apt install nginx=1.24.0-1ubuntu1

# Pin it so the next upgrade does not undo your fix
sudo apt-mark hold nginx
apt has no clean match for dnf history undo, which is why a snapshot matters more on Debian and Ubuntu.

How often should I patch a production server?

TypeFrequencyApproach
Security patchesAutomatically, dailyunattended-upgrades or dnf-automatic
Critical vulnerabilitiesWithin 24 hoursBy hand, as soon as you hear about it
Regular packagesMonthlyScheduled window, snapshot first
Kernel rebootsMonthly, or when requiredAnnounced window, out of hours
Distribution upgradesBefore end of supportTest on a copy first, never in place on the live server

The monthly window takes discipline. Security patching should not need a window at all.

Watch the end-of-life date

Once a release stops getting security updates, apt upgrade keeps reporting that all is well while known vulnerabilities go unpatched. Check your version with hostnamectl against your distribution's support calendar, and plan the move months ahead, not in the last week.

Patching never finishes, and getting it wrong lands on a live site. On G7Cloud, we patch the PHP and web server images every site runs on, and each site runs in its own isolated container, so an update for one site never touches another. See how we maintain WordPress sites, or read WordPress security beyond plugins for the wider picture.

Frequently asked questions

What is the difference between apt upgrade and apt full-upgrade?

apt upgrade installs newer versions of installed packages and never removes anything. apt full-upgrade removes packages when a dependency change needs it. On a live server, start with upgrade, and use full-upgrade only after reading exactly what it wants to remove.

Should I enable automatic updates on a production Linux server?

For security patches, yes. They are narrow fixes, and leaving a known vulnerability unpatched is a far greater risk. Limit the setup to the security source only, and leave automatic reboots off so you decide when the server restarts.

How do I apply only security updates on Ubuntu?

Install unattended-upgrades and run sudo unattended-upgrade -d, with Allowed-Origins limited to ${distro_id}:${distro_codename}-security. apt has no --security flag, which is why the tool exists. On the RHEL family, run dnf update --security.

Do I need to reboot after every Linux update?

No. Only kernel and core library updates need one. Check /var/run/reboot-required on Debian and Ubuntu, or needs-restarting -r on the RHEL family. For everything else, restarting the affected service is enough and far less disruptive.

What should I do when apt asks about a modified config file?

Keep your version, which is the default. Then compare afterwards with diff /etc/service/conf /etc/service/conf.dpkg-dist and copy any new options across by hand. Taking the package's version overwrites your settings, and is the usual reason a service fails to start after patching.

How do I roll back a Linux update that broke something?

On the RHEL family, sudo dnf history then sudo dnf history undo <id> reverses a whole transaction. Debian and Ubuntu have no equivalent: downgrade single packages with apt install package=version and hold them. That is why a snapshot beforehand matters more there.

Put this into practice on G7Cloud

Every site runs in its own dedicated container behind ScaleShield, with daily backups that are restore-tested every night. Start on the free plan, no card needed.

About G7Cloud Engineering

Platform team

Articles written by the engineers who build and run G7Cloud: UK managed hosting and the AI Website Builder. We write about what we operate every day: containers, backups, databases, and the small-business websites that run on them.

More about G7Cloud →