Report services needing a restart via a separate root needrestart unit

needrestart has to inspect every process, which an unprivileged agent
can't (and unprivileged it gave false kernel results). Add
kernelwatch-needrestart.service: root with only CAP_SYS_PTRACE and
CAP_DAC_READ_SEARCH, PrivateNetwork=yes, list-only (-r l). It writes
`needrestart -b` output to /run/kernelwatch/needrestart; the agent unit
pulls it in before each report and reads the file (ignored if older than
2h). The agent no longer runs needrestart itself. Skipped cleanly when
needrestart isn't installed: the dashboard shows "?" and status is
unaffected.

Server: services_need_restart / sessions_need_restart fields, an
"update"-level reason, a Services column, and a note (not an escalation)
for hosts running an agent older than the one shipped with the server.

install.sh --with-needrestart installs needrestart and sets it to list
services after apt upgrades instead of prompting; deploy.sh passes
anything after `--` to install.sh. Agent 0.2.0, server 0.2.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-10-17 11:54:39 -04:00
1 parent 69d4cf6363
commit 834c1459c0
14 files changed
+258 -26

No files matched your search

+28 -4
View File
@@ -27,7 +27,7 @@ Each host gets one status. When several apply, the worst one wins:
|----------|---------|
| `stale` | No report in `stale_after_hours` (default 3h), or never. Other data can't be trusted. |
| `reboot` | A newer kernel is installed but not running, or `/run/reboot-required` exists. |
| `update` | A kernel or security update is available but not installed, or apt's package lists are too old (`apt_stale_after_hours`) for "no updates" to mean anything. |
| `update` | A kernel or security update is available but not installed, services are running outdated libraries and need a restart, or apt's package lists are too old (`apt_stale_after_hours`) for "no updates" to mean anything. |
| `ok` | Running the newest installed kernel, with no security or kernel updates pending. |
Each row also lists the reasons behind its status, and clicking a host shows
@@ -42,9 +42,23 @@ How the agent works it out:
(`6.12.57-1`, `6.8.0-85.85`, `6.14.0-33.33~24.04.1`). A mismatch means the
package was upgraded in place after boot; `uname -r` didn't change, but a
reboot is still needed.
- [`needrestart`](https://github.com/liske/needrestart) `-b -k`, when it's
installed and can read `/boot`. It's optional. The agent runs unprivileged, so
on Ubuntu (root-only `/boot/vmlinuz-*`) it reports "unknown" and is ignored.
- [`needrestart`](https://github.com/liske/needrestart)'s kernel result, for
build strings the server doesn't recognise (see below).
- **Services needing a restart** (e.g. nginx still using the old OpenSSL
after an upgrade): from `needrestart -b`, if it's installed. Ubuntu ships it;
on Debian, install with `install.sh --with-needrestart`. Without it, the
Services column shows `?` and status isn't affected. needrestart has to
inspect every process, so it runs as root in its own unit,
`kernelwatch-needrestart.service`. That unit has no network access, only the
two capabilities needrestart needs, and list-only mode, so it never restarts
anything. It writes its results to `/run/kernelwatch/needrestart`, and the
unprivileged agent unit runs it just before each report and reads the file.
Neither unit has both root and network access. Fix it on the host:
`sudo needrestart -r a` (or restart the listed services), and the next report
clears it.
- **Agent version**: each report includes it. Hosts running an older agent
than the server shipped with get a note to run `agent/deploy.sh`; their status
isn't changed.
- **Available updates**: a simulated `apt-get dist-upgrade`. It is read-only, takes
no locks and writes no cache files. `dist-upgrade` is used so that new-ABI kernel
packages pulled in by the meta-package show up. Lines whose origin contains
@@ -185,6 +199,11 @@ to 15 minutes of jitter.
For unattended installs, set `KERNELWATCH_TOKEN=...` instead of using the prompt.
Add `--with-needrestart` to also report services needing a restart (Debian
needs this; Ubuntu already has needrestart). It installs needrestart if
missing and sets it to *list* services after apt upgrades instead of asking
interactively. Delete `/etc/needrestart/conf.d/kernelwatch.conf` to undo that.
Useful on a host:
```sh
@@ -212,6 +231,7 @@ agent/deploy.sh # upgrade every host in agent/hosts
agent/deploy.sh web01 # upgrade one
agent/deploy.sh --rotate web01 # new token, reinstall
agent/deploy.sh --dry-run # show the plan
agent/deploy.sh -- --with-needrestart # pass options through to install.sh
```
For each host it copies the agent into a private temp directory and runs
@@ -249,6 +269,10 @@ sudo kernelwatch list-users
- **Host identity comes from the token, not the payload.** A host can only
report as itself. Tokens are 256-bit random values; only their SHA-256 is stored.
- **needrestart** runs in a separate root unit with `PrivateNetwork=yes`, a
capability set of just `CAP_SYS_PTRACE` + `CAP_DAC_READ_SEARCH` (to read other
processes' memory maps), a read-only filesystem except `/run/kernelwatch`,
and `-r l` (list, never restart).
- **The agent** runs as a throwaway unprivileged user (`DynamicUser=yes`) with
no capabilities, a read-only filesystem view and a syscall filter. The token
reaches it through `LoadCredential=` and goes to curl on stdin, so it never