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.
sudo apt install -y unattended-upgrades apt-listchanges needrestart
sudo dpkg-reconfigure -plow unattended-upgrades # answer YesConfirm the schedule file exists and is on:
cat /etc/apt/apt.conf.d/20auto-upgradesAPT::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:
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:
sudo unattended-upgrade --dry-run --debug
sudo tail -n 40 /var/log/unattended-upgrades/unattended-upgrades.logReboots. 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, thensudo pro enable livepatch). Pro also adds ESM for security fixes on packages inuniverse, 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.
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 trustedRules: 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.
systemd-analyze security | head -20 # exposure score for every service (lower is better)
systemd-analyze security nginx.service # detail for one serviceCreate an override for a service (replace myapp with yours):
sudo systemctl edit myapp.service[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:
sudo systemctl daemon-reload && sudo systemctl restart myapp.service
systemctl status myapp.service --no-pager
systemd-analyze security myapp.serviceWhat 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=yesis powerful but breaks JIT runtimes (Node.js, Java, .NET), so leave it out for those. Always apply one service at a time and watchjournalctl -u myappfor permission errors.
Back-out: sudo systemctl revert myapp.service removes your override.
🧪 Lab
- Enable unattended upgrades, run the dry run, and read the log.
- Run
needrestartandls /var/run/reboot-required; decide your reboot policy. - Pick one service, record its
systemd-analyze securityscore, add the sandbox options, and record the new score. - Remove two packages or snaps you do not need.
Checkpoint
- Why are automatic security updates worth the occasional surprise?
- What does
needrestartshow you thatapt upgradedoes not? - What does
ProtectSystem=strictdo, and how do you allow a service to write to its own directory? - Which sandbox option breaks JIT runtimes?
- How do you undo a systemd override?