跳到正文
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.

测试环境 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

作者

Inder Kochar

系统与网络工程师 – 我把为客户使用的解决方案整理成文,并在真实系统上测试过。遇到类似问题了吗?

继续阅读

提出问题或分享您的解决方法

您的邮箱不会公开。评论需经审核——请围绕主题发言。