CI / tests (Python 3.10) (pull_request) Successful in 15s
CI / tests (Python 3.11) (pull_request) Successful in 15s
CI / tests (Python 3.12) (pull_request) Successful in 15s
CI / tests (Python 3.13) (pull_request) Successful in 15s
CI / lint (pull_request) Successful in 3s
The agent's dist-upgrade simulation already finds new-ABI kernels and security updates, but only once apt's lists know about them. Debian doesn't refresh lists by default, so a kernel security release could stay invisible until someone ran apt update. install.sh --with-apt-update adds a marker that enables kernelwatch-apt-update.service, a sandboxed root oneshot the agent pulls in before each report. Failures (no network, apt lock held) are ignored so the report still goes out. deploy.sh now ships the unit; a test keeps deploy.sh's file list in sync with what install.sh uses. Agent 0.3.2, server 0.3.3. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
439 lines
19 KiB
Markdown
439 lines
19 KiB
Markdown
# kernelwatch
|
||
|
||
A small, self-hosted dashboard that shows whether each machine in a Debian/Ubuntu
|
||
fleet is running its newest kernel, has updates waiting, or needs a reboot.
|
||
|
||
- **Agent**: a bash script run hourly by a systemd timer on each host. It
|
||
collects facts and POSTs them as JSON. It runs no daemon and listens on no port.
|
||
- **Server**: a standard-library Python app with SQLite. It has a token-authenticated
|
||
ingest API and a login-protected dashboard. You don't need pip, a venv or a
|
||
build step.
|
||
|
||
Supported: agents on **Debian 12/13** and **Ubuntu 22.04/24.04/26.04 LTS**; server on
|
||
**Debian 12+** or **Ubuntu 22.04+** (the system `python3`, 3.10 or newer).
|
||
|
||
```
|
||
host ──(systemd timer)── kernelwatch-agent ──HTTPS POST /api/v1/report──┐
|
||
host ──(systemd timer)── kernelwatch-agent ─────────────────────────────┤
|
||
▼
|
||
browser ──HTTPS──▶ Caddy/nginx ──▶ kernelwatch (127.0.0.1:8080) ──▶ SQLite
|
||
```
|
||
|
||
## What the dashboard tells you
|
||
|
||
Each host gets one status. When several apply, the worst one wins:
|
||
|
||
| Status | Meaning |
|
||
|----------|---------|
|
||
| `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, 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
|
||
its full report and recent history.
|
||
|
||
How the agent works it out:
|
||
|
||
- **Running vs. installed kernel**, three ways:
|
||
- A newer `linux-image-*` installed than `uname -r` means a version upgrade.
|
||
- The build in `uname -v` (`Debian 6.12.57-1`, `#85-Ubuntu`, `#33~24.04.1-Ubuntu`)
|
||
is compared with the installed package version of the running kernel
|
||
(`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)'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.
|
||
The agent never runs as root, and needrestart never gets 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
|
||
`-security` count as security updates.
|
||
- **Fresh package lists**: the simulation only knows what apt's package lists
|
||
say, and the agent can't refresh them itself. Ubuntu refreshes them about once
|
||
a day. Debian doesn't by default unless `unattended-upgrades` is set up, so a
|
||
new kernel or security update can stay invisible until someone runs
|
||
`apt update`. Install with `--with-apt-update` and
|
||
`kernelwatch-apt-update.service` runs `apt-get update` just before each
|
||
report, so updates show up within the hour. Either way, lists older than
|
||
`apt_stale_after_hours` put the host in `update`.
|
||
- **Phased updates** (Ubuntu only): Ubuntu rolls some updates out gradually and
|
||
holds them back from a share of machines at first. apt doesn't offer these to
|
||
the host yet, so they aren't counted as available updates. They appear as
|
||
their own Phased column (hover for the packages, `–` on Debian), with the
|
||
list also on the host page.
|
||
They never affect status, because Ubuntu doesn't phase security updates and
|
||
there's nothing to act on until apt offers them.
|
||
- **Package list freshness**: the mtime of apt's update stamps. The agent
|
||
**doesn't run `apt update`** itself; it relies on the stock `apt-daily.timer`.
|
||
If that's disabled, the host shows `update` with "package lists N days old".
|
||
|
||
The agent sends raw facts and the server decides what they mean, so you can
|
||
change the status rules without touching the agents.
|
||
|
||
## Server setup
|
||
|
||
On the dashboard host:
|
||
|
||
```sh
|
||
git clone <this repo> && cd kernel-automation
|
||
sudo ./server/install.sh
|
||
sudo kernelwatch set-password admin # dashboard login
|
||
```
|
||
|
||
This installs to `/opt/kernelwatch` and keeps config in `/etc/kernelwatch/server.toml`
|
||
(see [`server/server.toml.example`](server/server.toml.example)) and data in
|
||
`/var/lib/kernelwatch`. The `kernelwatch.service` unit runs it as the unprivileged
|
||
`kernelwatch` user. Re-run `install.sh` to upgrade; it keeps your config.
|
||
|
||
### TLS reverse proxy
|
||
|
||
The server speaks plain HTTP on `127.0.0.1:8080`. Put a TLS proxy in front of it.
|
||
The agents and your browser both connect through it.
|
||
|
||
**Caddy** (simplest):
|
||
|
||
```caddyfile
|
||
kernelwatch.example.internal {
|
||
reverse_proxy 127.0.0.1:8080
|
||
}
|
||
```
|
||
|
||
**nginx**:
|
||
|
||
```nginx
|
||
server {
|
||
listen 443 ssl;
|
||
server_name kernelwatch.example.internal;
|
||
# ssl_certificate / ssl_certificate_key ...
|
||
location / {
|
||
proxy_pass http://127.0.0.1:8080;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
}
|
||
}
|
||
```
|
||
|
||
With either proxy, set `trust_proxy_headers = true` in `server.toml` so login
|
||
rate limiting sees real client IPs.
|
||
|
||
If you use a private CA (e.g. Caddy's `tls internal`), give the agents its
|
||
root cert with `--ca` (below).
|
||
|
||
You can expose only `/api/v1/report` to the fleet network and keep the
|
||
dashboard on a VPN or tailnet. That's a proxy rule, not a kernelwatch setting.
|
||
|
||
### CloudPanel
|
||
|
||
CloudPanel gives each site its own Linux user and a document root at
|
||
`/home/<site-user>/htdocs/<domain>/`. kernelwatch doesn't use that directory.
|
||
nginx forwards requests to the app; it doesn't serve kernelwatch's files. Install
|
||
with `server/install.sh` as above, so the app runs as the `kernelwatch` user
|
||
with its data in `/var/lib/kernelwatch`, outside every site user's home. Leave
|
||
the document root empty, and keep your clone of this repo out of `htdocs`.
|
||
|
||
1. **Pick a free port.** CloudPanel's Varnish setup can run an nginx backend
|
||
on 8080. Check with `ss -ltnp | grep ':8080\b'`. If it's taken, set another port
|
||
in `/etc/kernelwatch/server.toml` and restart (`sudo systemctl restart kernelwatch`):
|
||
|
||
```toml
|
||
listen_port = 8095
|
||
trust_proxy_headers = true
|
||
```
|
||
|
||
Check it: `curl -s http://127.0.0.1:8095/healthz` should print `ok`.
|
||
|
||
2. **Create the site as a Reverse Proxy.** In CloudPanel: *Add Site → Create a
|
||
Reverse Proxy*, with your domain and Reverse Proxy URL
|
||
`http://127.0.0.1:8095`. CloudPanel can't change an existing site's type. If
|
||
the domain already exists as a PHP or static site, delete and recreate it.
|
||
Deleting a site also removes its site user and home directory, so move
|
||
anything you need out first.
|
||
|
||
3. **TLS.** In the site's *SSL/TLS* tab, issue a Let's Encrypt certificate. Agents
|
||
then need no `--ca`.
|
||
|
||
4. **Check the generated vhost** (the site's *Vhost* tab). The `location`
|
||
that proxies to the app should pass the original `Host` header and the
|
||
client address:
|
||
|
||
```nginx
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
```
|
||
|
||
Add them if they're missing. Without `Host`, logins fail the origin check
|
||
with "cross-origin request refused". Without `X-Forwarded-For`, every login
|
||
appears to come from 127.0.0.1, so one person's failed attempts lock everyone out.
|
||
Also check that the login page is styled. If `/static/style.css` returns 404,
|
||
a static-file `location` in the template is looking in the empty document root
|
||
instead of proxying. Make it fall through to the proxy, or remove it.
|
||
|
||
5. **Optionally restrict the dashboard.** The site's domain is public, so the
|
||
login page is reachable from the internet unless you limit it. Agents only
|
||
need `/api/v1/report`. In the Vhost tab, add an `allow <your IP>; deny all;`
|
||
to `location /` and a separate unrestricted
|
||
`location = /api/v1/report { proxy_pass http://127.0.0.1:8095; }`.
|
||
Changes made in the Vhost tab persist. Back them up before
|
||
recreating the site.
|
||
To require a login through Authelia instead of an IP allowlist, see
|
||
[Authelia (forward auth)](#authelia-forward-auth).
|
||
|
||
### Authelia (forward auth)
|
||
|
||
If you already run [Authelia](https://www.authelia.com/), you can put it in
|
||
front of the dashboard. That adds Authelia's 2FA and single sign-on, and
|
||
kernelwatch's own login still applies behind it. You log in to both, but
|
||
Authelia's remember-me makes that rare. This uses nginx's `auth_request`, so
|
||
you don't need an OIDC client.
|
||
|
||
**Agents must bypass Authelia.** They authenticate with their bearer token and
|
||
can't follow a login redirect. If Authelia covers `/api/v1/report`, every
|
||
report gets a redirect instead of a 401 or 204, and the whole fleet goes stale
|
||
within 3 hours.
|
||
|
||
1. **Authelia `access_control`.** Rules are checked in order, so put the bypass
|
||
first, and keep a single `rules:` list:
|
||
|
||
```yaml
|
||
access_control:
|
||
default_policy: two_factor
|
||
rules:
|
||
- domain: kernelwatch.example.com
|
||
resources:
|
||
- '^/api/v1/report$'
|
||
- '^/healthz$'
|
||
policy: bypass
|
||
- domain: "*.example.com"
|
||
policy: two_factor
|
||
```
|
||
|
||
For the redirect to the login page to work, your `session.cookies` entry has
|
||
to cover the dashboard's domain and set `authelia_url`. Without them, browsers
|
||
get a bare 401 page. Authelia reads this file only at startup, so restart it
|
||
after editing (in Kubernetes: `kubectl rollout restart deployment/authelia`).
|
||
Check the file first with `authelia validate-config`.
|
||
|
||
2. **nginx.** Add an internal location for Authelia's check, an exact-match
|
||
location for the agents with no check, and the check on `location /`.
|
||
For CloudPanel, edit the site's *Vhost* tab: leave the template's
|
||
`@reverse_proxy` block alone and add or replace these:
|
||
|
||
```nginx
|
||
# Authelia's check (internal subrequest only)
|
||
location = /internal/authelia/authz {
|
||
internal;
|
||
proxy_pass https://auth.example.com/api/authz/auth-request;
|
||
proxy_http_version 1.1;
|
||
proxy_ssl_server_name on;
|
||
proxy_set_header Host auth.example.com;
|
||
proxy_set_header X-Original-Method $request_method;
|
||
proxy_set_header X-Original-URL $scheme://$http_host$request_uri;
|
||
proxy_set_header X-Forwarded-For $remote_addr;
|
||
proxy_set_header Content-Length "";
|
||
proxy_set_header Connection "";
|
||
proxy_pass_request_body off;
|
||
}
|
||
|
||
# Agents: token auth only
|
||
location = /api/v1/report {
|
||
try_files $uri @reverse_proxy;
|
||
}
|
||
|
||
# Dashboard: Authelia first, then kernelwatch's login
|
||
location / {
|
||
auth_request /internal/authelia/authz;
|
||
auth_request_set $redirection_url $upstream_http_location;
|
||
error_page 401 =302 $redirection_url;
|
||
try_files $uri @reverse_proxy;
|
||
}
|
||
```
|
||
|
||
Without CloudPanel, use `proxy_pass http://127.0.0.1:8095;` plus the `Host`
|
||
and `X-Forwarded-For` headers in place of `try_files ... @reverse_proxy`. If
|
||
Authelia runs on the same machine, point the check straight at it (e.g.
|
||
`http://127.0.0.1:9091/api/authz/auth-request`) and drop the
|
||
`proxy_ssl_server_name` and `Host` lines. Keep `location ^~ /.well-known`
|
||
unprotected so certificate renewals still work. If something monitors
|
||
`/healthz`, give it an exact-match location like the agents' one.
|
||
|
||
3. **Check it.**
|
||
|
||
```sh
|
||
curl -sI https://kernelwatch.example.com/ | head -3
|
||
# 302, Location pointing at Authelia
|
||
|
||
curl -si -X POST https://kernelwatch.example.com/api/v1/report | head -1
|
||
# 401 from kernelwatch (no token), not a 302
|
||
```
|
||
|
||
Then run `sudo systemctl start kernelwatch-agent` on one host and check
|
||
that its *Last seen* updates. If the POST gets a 302, the agents' location
|
||
isn't matching. Fix that before the fleet goes stale.
|
||
|
||
## Adding a host
|
||
|
||
On the server:
|
||
|
||
```sh
|
||
sudo kernelwatch add-host web01
|
||
# prints a token, shown once
|
||
```
|
||
|
||
On the host (copy the `agent/` directory over, or clone the repo):
|
||
|
||
```sh
|
||
sudo ./agent/install.sh --server https://kernelwatch.example.internal [--ca root.pem]
|
||
# paste the token when prompted
|
||
```
|
||
|
||
The installer puts the agent in `/usr/local/sbin`, writes `/etc/kernelwatch/agent.conf`
|
||
and `agent.token` (root, 0600), enables `kernelwatch-agent.timer` and sends a first
|
||
report. After that, the agent reports 2 minutes after boot and then hourly, with up
|
||
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.
|
||
|
||
Add `--with-apt-update` to refresh apt's package lists before each report
|
||
(recommended on Debian, see above). It's the same as running `apt update`
|
||
hourly: lists only, nothing is installed. If apt is busy, the refresh is
|
||
skipped and the report goes ahead. `--without-apt-update` turns it off;
|
||
re-running the installer with neither keeps the current setting.
|
||
|
||
Useful on a host:
|
||
|
||
```sh
|
||
sudo kernelwatch-agent --print # see exactly what would be sent
|
||
sudo systemctl start kernelwatch-agent # report now
|
||
systemctl list-timers kernelwatch-agent.timer
|
||
journalctl -u kernelwatch-agent
|
||
```
|
||
|
||
### Deploying to many hosts
|
||
|
||
`agent/deploy.sh` does all of the above over SSH from your workstation's
|
||
checkout, so hosts need neither git nor Gitea access. Set it up once:
|
||
|
||
```sh
|
||
cp agent/deploy.conf.example agent/deploy.conf # SERVER_URL, DASHBOARD (ssh target)
|
||
cp agent/hosts.example agent/hosts # optional; --register fills it in
|
||
```
|
||
|
||
Then:
|
||
|
||
```sh
|
||
agent/deploy.sh --register web01 db01 # register on the dashboard + install
|
||
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
|
||
agent/deploy.sh -- --with-apt-update # refresh package lists before each report
|
||
```
|
||
|
||
For each host it copies the agent into a private temp directory and runs
|
||
`install.sh`, which sends a first report, then deletes the temp directory.
|
||
With `--register`/`--rotate`, it also runs
|
||
`sudo kernelwatch add-host --token-only` on the dashboard host and passes the
|
||
token to the host on SSH stdin. The token never appears on a command line or
|
||
in your terminal. A plain run is an upgrade: config and token stay as they
|
||
are. You'll get sudo prompts on each host, and on the dashboard when
|
||
registering, unless you have passwordless sudo.
|
||
|
||
Both files are git-ignored. Host names default to each host's `hostname -s`;
|
||
put `<ssh target> <name>` in `agent/hosts` to override. For a host your
|
||
workstation can't reach directly (e.g. IPv6-only while you're on IPv4), give it
|
||
a jump host in `~/.ssh/config` and `deploy.sh` works unchanged:
|
||
|
||
```
|
||
Host zeus
|
||
ProxyJump eurus
|
||
```
|
||
|
||
## Server admin commands
|
||
|
||
```
|
||
sudo kernelwatch add-host NAME register a host, print its token (--token-only for scripts)
|
||
sudo kernelwatch rotate-token NAME issue a new token (old one stops working)
|
||
sudo kernelwatch remove-host NAME delete a host and its history
|
||
sudo kernelwatch list-hosts hosts and when they last reported
|
||
sudo kernelwatch set-password USER create a user / change password (logs out their sessions)
|
||
sudo kernelwatch remove-user USER
|
||
sudo kernelwatch list-users
|
||
```
|
||
|
||
## Security notes
|
||
|
||
- **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).
|
||
- **apt-get update** (with `--with-apt-update`) runs as root with network
|
||
access, like apt's own `apt-daily.service`, but sandboxed: no access to home
|
||
directories, devices or kernel settings. apt drops to its `_apt` user for the
|
||
downloads, and nothing gets installed.
|
||
- **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
|
||
appears in a process listing.
|
||
- **The dashboard** uses scrypt-hashed passwords, server-side sessions,
|
||
`HttpOnly` + `Secure` + `SameSite=Strict` cookies, an Origin check on form
|
||
posts, per-IP login rate limiting, and a strict CSP. Everything a host
|
||
reports is HTML-escaped.
|
||
- **It's read-only by design.** The server can't make hosts do anything.
|
||
Adding remote actions (like "reboot this host") would turn it into a
|
||
remote-execution system; think hard before doing that.
|
||
|
||
## Development
|
||
|
||
```sh
|
||
make test # unit + integration tests (stdlib unittest)
|
||
make lint # shellcheck, if available
|
||
```
|
||
|
||
Gitea Actions (`.gitea/workflows/ci.yml`) runs both on every push to `main`
|
||
and every pull request: the tests on Python 3.10–3.13 (the versions on the
|
||
supported Ubuntu and Debian releases) and shellcheck on the shell scripts.
|
||
|
||
The tests run a real server on an ephemeral port. They also run the real agent
|
||
script against stubbed `dpkg-query`/`apt-get`/`needrestart`/`uname` and a fake
|
||
filesystem root (`KERNELWATCH_ROOT`), including one end-to-end agent → server
|
||
report.
|
||
|
||
Run a local server against a scratch database:
|
||
|
||
```sh
|
||
printf 'db_path = "/tmp/kw.db"\nsecure_cookies = false\n' > /tmp/kw.toml
|
||
export KERNELWATCH_CONFIG=/tmp/kw.toml
|
||
./server/kernelwatch-server set-password admin
|
||
./server/kernelwatch-server serve # http://127.0.0.1:8080
|
||
```
|
||
|
||
## Ideas for later
|
||
|
||
- Alerts on status changes (email/ntfy/webhook)
|
||
- Ubuntu Livepatch / Pro status, so live-patched hosts aren't flagged for reboot
|
||
- A JSON endpoint for scripting
|
||
- Grouping or tagging hosts (prod/staging, location)
|