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
#!/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.u0022nfiHow to use it
- Save it as
update-system.sh. - Make it executable:
chmod +x update-system.sh - Run it:
./update-system.sh
What it does
set -euo pipefailstops 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-requiredand 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.

