Zum Inhalt springen
Ubuntu / Linux

One-Command Ubuntu Updates: A Safe Update Script

Save the script as update-system.sh, make it executable and run it. It stops on the first error and tells you if a reboot is needed.

Getestet mit Ubuntu 24.04 LTS, 22.04 LTS

Keeping servers patched is the single most effective security habit – and the easiest to skip when it takes four commands. This script runs the whole routine and stops if anything fails.

The script

update-system.sh
#!/usr/bin/env bashn# Update Ubuntu in one command – Inder Kochar (inderkochar.in)nset -euo pipefailnnsudo -v || { echo u0022This script needs sudo rights.u0022 u003eu00262; exit 1; }nnecho u0022==u003e Refreshing package listsu0022nsudo apt updatennecho u0022==u003e Packages that will be upgradedu0022napt list u002du002dupgradable 2u003e/dev/null | tail -n +2nnecho u0022==u003e Upgradingu0022nsudo apt upgrade -ynnecho u0022==u003e Removing packages that are no longer neededu0022nsudo apt autoremove u002du002dpurge -ynnif [ -f /var/run/reboot-required ]; thenn    echo u0022==u003e A reboot is required to finish the update.u0022nelsen    echo u0022==u003e System is up to date.u0022nfi

How to use it

  1. Save it as update-system.sh.
  2. Make it executable: chmod +x update-system.sh
  3. Run it: ./update-system.sh

What it does

  • set -euo pipefail stops the script on the first error instead of carrying on.
  • Lists upgradable packages so you can see what is about to change.
  • Upgrades without prompts, then removes unused dependencies.
  • Checks /var/run/reboot-required and tells you when a kernel or library update needs a restart.

Automate security updates

For unattended servers, Ubuntu’s unattended-upgrades package installs security fixes automatically (sudo dpkg-reconfigure unattended-upgrades). Use this script for the full upgrades you want to watch – or schedule it with the Crontab Generator.

Inder Kochar

Geschrieben von

Inder Kochar

System- & Netzwerkingenieur – Ich schreibe die Lösungen auf, die ich bei Kunden einsetze – getestet auf echten Systemen. Hängen Sie an etwas Ähnlichem fest?

Weiterlesen

Stellen Sie eine Frage oder teilen Sie Ihre Lösung

Ihre E-Mail-Adresse wird nicht veröffentlicht. Kommentare werden moderiert – bitte beim Thema bleiben.