brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: PrivateDevices=yes hides your disks from a systemd service

Systemd
~/tips/tip-systemd-private-devices

PrivateDevices=yes

Give a service a minimal /dev without disks, so a bug can not read raw devices.

A web app or a small API has no reason to see /dev/sda or /dev/mem. By default it does, and as root it could read a whole disk.

sudo systemctl edit --drop-in=devices myapp.service

[Service]
PrivateDevices=yes

The service then gets its own small /dev with only pseudo devices. Listed through the service’s mount namespace, it holds null, zero, random, urandom, tty, pts, shm and a few more, and nothing else. A quick check with a one-off unit:

sudo systemd-run --wait -p PrivateDevices=yes ls /dev/sda /dev/null

That prints /dev/null and cannot access '/dev/sda': No such file or directory.

The setting also removes the capability to create device nodes and turns on NoNewPrivileges=, so the service can not get the disks back by itself. systemd-analyze security counts it among its checks, and I find it one of the easiest to add because most network services never touch hardware.

Leave it off for anything that does need a device: a backup tool reading a raw disk, a service talking to a USB serial adapter, a media server using /dev/dri for hardware transcoding, or smartmontools. Those fail with missing device errors, which at least makes the cause easy to spot in the journal.

Further reading: systemd.exec man page

More Systemd tips [Service] sudo mkdir -p /etc/systemd/system/service.d [Service] printf 'hunter2' | sudo systemd-creds encrypt --name=dbpass – dbpass.cred systemd-analyze timespan 1h30min All Systemd tips (88) All topics

Link