Lesson 4 of 12 · 12 min · Intermediate

Ubuntu: automatic patching and sandboxing your services

Turn on unattended security updates with controlled reboots, learn Livepatch and ESM options, trim the package footprint, and use systemd's sandboxing options to confine services, scored with systemd-analyze security.

In this lesson you will

  • Enable and test unattended security updates with a reboot window
  • Detect pending reboots and restart services after library updates
  • Remove unneeded packages and trust only the repositories you need
  • Sandbox a service with systemd options and measure the improvement

Two ideas in this lesson do more for a typical server than almost anything else: patch automatically, and confine every service so a bug in it cannot become a full compromise.

Automatic security updates

Known, already-fixed flaws are behind most real intrusions. Waiting for a monthly maintenance window leaves a gap. Ubuntu can install security updates by itself.

bash
sudo apt install -y unattended-upgrades apt-listchanges needrestart
sudo dpkg-reconfigure -plow unattended-upgrades     # answer Yes

Confirm the schedule file exists and is on:

bash
cat /etc/apt/apt.conf.d/20auto-upgrades
text
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

Tune behaviour in /etc/apt/apt.conf.d/50unattended-upgrades. The defaults already allow the security pocket; consider these:

text
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:30";
Unattended-Upgrade::Mail "ops@example.com";

Test without changing anything, and read the log:

bash
sudo unattended-upgrade --dry-run --debug
sudo tail -n 40 /var/log/unattended-upgrades/unattended-upgrades.log

Reboots. Kernel and core library fixes need a restart. Automatic reboots at a quiet hour are fine for stateless or redundant servers. For single, critical servers, schedule reboots yourself, but do schedule them. Check at any time with ls /var/run/reboot-required.

Fewer reboots: Livepatch, ESM and needrestart

  • needrestart (installed above) tells you which services are still running old libraries after an update: sudo needrestart -r l.
  • Livepatch applies many kernel security fixes without a reboot. It is part of Ubuntu Pro (sudo pro attach TOKEN, then sudo pro enable livepatch). Pro also adds ESM for security fixes on packages in universe, and a free tier exists for personal use. Check Canonical's current terms for your situation.

Shrink the footprint

Every package is code that can have a flaw. Remove what you do not need.

bash
apt list --installed 2>/dev/null | wc -l
sudo apt autoremove --purge -y
snap list                         # snaps are updated automatically; remove ones you do not use
apt-cache policy | grep -E 'http|https'   # which repositories are trusted

Rules: install from Ubuntu's repositories where possible; keep third-party repos pinned with a signed-by key; never run curl ... | sudo bash from a source you have not read.

Sandbox services with systemd

Most services run with far more power than they need. systemd can confine them with a few lines, and it will score how exposed each one is.

A service, before and after sandboxing

bash
systemd-analyze security | head -20          # exposure score for every service (lower is better)
systemd-analyze security nginx.service        # detail for one service

Create an override for a service (replace myapp with yours):

bash
sudo systemctl edit myapp.service
text
[Service]
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/var/lib/myapp /var/log/myapp
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
RestrictSUIDSGID=yes
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
LockPersonality=yes
SystemCallArchitectures=native
CapabilityBoundingSet=

Then apply and re-score:

bash
sudo systemctl daemon-reload && sudo systemctl restart myapp.service
systemctl status myapp.service --no-pager
systemd-analyze security myapp.service

What the key lines do:

Option Effect
NoNewPrivileges The process and its children cannot gain privileges (blocks setuid tricks)
ProtectSystem=strict The whole filesystem is read-only except paths you list in ReadWritePaths
PrivateTmp Its own private /tmp, so it cannot snoop on or poison others
ProtectHome Cannot read user home directories
RestrictAddressFamilies Only the network socket types it needs
CapabilityBoundingSet= Drops all Linux capabilities

Gotchas. A service that binds a port below 1024 needs AmbientCapabilities=CAP_NET_BIND_SERVICE (and that capability in the bounding set). MemoryDenyWriteExecute=yes is powerful but breaks JIT runtimes (Node.js, Java, .NET), so leave it out for those. Always apply one service at a time and watch journalctl -u myapp for permission errors.

Back-out: sudo systemctl revert myapp.service removes your override.

🧪 Lab

  1. Enable unattended upgrades, run the dry run, and read the log.
  2. Run needrestart and ls /var/run/reboot-required; decide your reboot policy.
  3. Pick one service, record its systemd-analyze security score, add the sandbox options, and record the new score.
  4. Remove two packages or snaps you do not need.

Checkpoint

  1. Why are automatic security updates worth the occasional surprise?
  2. What does needrestart show you that apt upgrade does not?
  3. What does ProtectSystem=strict do, and how do you allow a service to write to its own directory?
  4. Which sandbox option breaks JIT runtimes?
  5. How do you undo a systemd override?