Three Go Services, Clear Boundaries: systemd Instead of a Container Stack
A statically linked Go binary is self-executing. Its release does not have to
bring a package manager, a separate interpreter, or its own Linux userspace
with /bin/sh. It does not need dynamically linked libraries in exactly
matching versions on the target system either. Even so, a long-running service
needs a clean operational boundary: limited memory and CPU time, an
unprivileged user, only the file paths it actually needs, reliable restarts, and
a clear logging interface.
A container stack can provide that, but it is not always the smallest secure way to operate a service. On a single systemd-based Linux host, the existing mechanisms already provide much of it: not as a replacement for OCI containers or Kubernetes, but as a deliberately small operating contract directly on the host.
Why Standalone Binaries Are Attractive Again
This kind of deployment is not new, but it is experiencing a remarkable
renaissance with Go. A Go program can be linked statically with CGO disabled,
include assets directly with embed, and be built by cross-compilation for a
specific combination of operating system and architecture. From one source
revision, this produces clearly defined releases for, say, Linux on x86_64 or
ARM, without needing a Go installation or a separate runtime file tree on the
target system.
This is not magical platform independence: a Linux ARM binary does not run on Windows or x86_64. But Go reduces the friction of producing a suitable, small release for each target. For services with an embedded web interface, templates, or default configuration, this means a pleasant developer experience: build, ship, start as a service.
A Case Study with Three Different Sets of Permissions
Three statically linked Go services run on a single home server: Governer executes rules and automations, Console provides operation and observation, and Meterbridge reads two optical meter ports. It is precisely these different permissions that make the three services a good example: two need network access and separate state, and one additionally needs two specific devices.
The Application Has to Cooperate
systemd can confine a process well, but it cannot fix an unclear application. The three services should meet a few simple properties:
| Property | Why it simplifies operations |
|---|---|
| One process per service | systemd unambiguously monitors the right main process. |
| Configuration outside the binary | The same release file runs with different configuration in a different environment. |
| State in a defined path | Write permissions stay limited to exactly one location. |
| Logs to stdout/stderr | journalctl becomes the central interface for the logs the services emit. |
| Handle SIGTERM cleanly | Updates and restarts shut down HTTP servers, MQTT connections, and serial ports in a controlled way. |
| Statically linked, where possible | The deployment really consists only of the binary and not of a hidden library tree. |
This complements the principles from The 12-Factor App in Go (German): configuration stays separate from code, logs are an event stream, and the process is disposable. The difference lies in the target: not an arbitrary cloud platform, but a single, deliberately simple Linux host.
For Go, a portable Linux release can be built like this, for example:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -trimpath -ldflags='-s -w' -o dist/governer ./cmd/governer
CGO_ENABLED=0 is not dogma. If a service deliberately needs CGO, for example
because of a hardware or database driver, that is legitimate. In that case,
however, the loader and libraries must be part of the deployment contract.
ldd dist/governer shows whether the result really has no dynamic
dependencies.
What systemd Already Provides as a Boundary
The settings mostly come from systemd.exec(5).
They are not an additional daemon and not a special systemd edition, but
options in the [Service] section of a normal unit.
| Setting | Effect |
|---|---|
DynamicUser=yes |
systemd allocates an unprivileged UID at runtime; a permanent user account is not needed. |
StateDirectory= |
creates a persistent, appropriately permissioned state path under /var/lib/. |
ProtectSystem=strict |
makes the filesystem largely read-only for the service. |
ProtectHome=yes |
hides home directories. |
PrivateTmp=yes |
creates a private /tmp and /var/tmp. |
PrivateDevices=yes |
provides only a very small set of devices. Additional devices must be explicitly allowed and bound in. |
NoNewPrivileges=yes |
prevents the process from gaining new privileges through setuid files or file capabilities. |
CapabilityBoundingSet= |
removes Linux capabilities the service does not need. |
RestrictAddressFamilies= |
restricts which kinds of sockets a process may open. |
MemoryMax=, CPUQuota=, TasksMax= |
set limits for memory, CPU time, and the number of threads or processes. |
These are real technical boundaries, not conventions for developers. They still do not replace a threat model: a service with access to a sensitive MQTT account, a writable host mount, or physical actuators can cause damage despite the sandbox. The boundary must always match the permissions granted.
MemoryMax= works through the cgroup, ProtectSystem= through a mount
namespace, DynamicUser= through Unix file permissions, and NoNewPrivileges=
against later privilege gains. Only the combination confines a service in a
meaningful way. No single setting turns a process into a container or a VM.
A Simple Release Contract
Delivery can be kept clear without an image and without a dedicated deployment
daemon. Binaries live under /usr/local/libexec, configuration under /etc,
and only systemd manages runtime state under /var/lib.
/usr/local/libexec/governer
/usr/local/libexec/console
/usr/local/libexec/meterbridge
/etc/governer/governer.env
/etc/console/console.env
/etc/meterbridge/meterbridge.env
/etc/systemd/system/governer.service
/etc/systemd/system/console.service
/etc/systemd/system/meterbridge.service
/var/lib/governer/ # created by systemd
/var/lib/console/ # created by systemd
A release is then not a container layer, but the replacement of one unambiguous file. The configuration is preserved and not mixed with the binary:
sudo install -m 0755 dist/governer /usr/local/libexec/governer
sudo install -m 0755 dist/console /usr/local/libexec/console
sudo install -m 0755 dist/meterbridge /usr/local/libexec/meterbridge
sudo systemctl restart governer console meterbridge
In an automated deployment, a checksum, a signature, or a versioned release
directory belongs in the process. The separation is what matters: the build
produces binaries, configuration management handles /etc, and systemd runs
them. This preserves the same advantage that is also behind declarative
Quadlets: the desired
operating state is traceable as text and can be handled with the usual system
tools.
A Common Basis for the Services
The following units deliberately use only the hardening that fits these three
services. They need network access for MQTT, HTTP, or Prometheus;
PrivateNetwork=yes would therefore be wrong without additional routing. The
host network is deliberately shared, while the filesystem, users, temporary
files, and device access are restricted.
Wants=network-online.target and After=network-online.target only order the
startup. They guarantee neither working DNS nor a reachable MQTT broker. Each
service must therefore be able to re-establish connections robustly on its own.
All services log to stdout and stderr. That keeps day-to-day work small:
sudo systemctl enable --now governer console meterbridge
sudo systemctl status governer
sudo journalctl -u meterbridge -f
systemd-cgtop
systemd-cgtop shows actual resource usage per cgroup. The limits themselves
are documented in systemd.resource-control(5).
They should come from measurements, not from wishful thinking: observe first,
set a sensible upper limit, and deliberately test load peaks.
systemd-analyze security governer.service assesses the configured sandbox
options and shows missing protections. This is a helpful configuration check,
but no substitute for a threat model or a test of the permissions actually
required.
Governer: Rules and Automation
Governer evaluates MQTT states and publishes functional commands. That makes it
an important service, but not the place for an actuator’s immediate core
function. The local actuator logic and the clear model of state/, config/,
and cmd/ stay with the components, as described in the article
A Smart Home That Feels Like a Home (German).
If Governer fails, comfort automation ends, not the usability of the house.
# /etc/systemd/system/governer.service
[Unit]
Description=Governer Smart-Home Rules and Automation Engine
Wants=network-online.target
After=network-online.target
[Service]
Type=exec
ExecStart=/usr/local/libexec/governer
EnvironmentFile=/etc/governer/governer.env
DynamicUser=yes
StateDirectory=governer
StateDirectoryMode=0750
Restart=on-failure
RestartSec=5s
TimeoutStopSec=30s
MemoryMax=256M
CPUQuota=50%
TasksMax=64
NoNewPrivileges=yes
CapabilityBoundingSet=
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectKernelTunables=yes
ProtectControlGroups=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
[Install]
WantedBy=multi-user.target
StateDirectory=governer is the only intended persistent write path. The binary
must therefore store its optional state under /var/lib/governer. Rule
definitions can live in the protected /etc/governer. Credentials should not be
put into EnvironmentFile= without thought: systemd does read the file before
the process starts, but passed environment variables are more easily exposed by
accident than files provided deliberately. LoadCredential= is usually the
better interface for MQTT passwords or certificates.
Console: Operation and Observation
Console provides the local web interface, observes MQTT, and exports metrics if needed. It may trigger functionally permitted requests, but has no hidden second actuator logic. This is the same deliberate separation that keeps the smart home architecture able to act when the interface restarts.
# /etc/systemd/system/console.service
[Unit]
Description=Console Smart-Home UI and Monitoring
Wants=network-online.target
After=network-online.target
[Service]
Type=exec
ExecStart=/usr/local/libexec/console
EnvironmentFile=/etc/console/console.env
DynamicUser=yes
StateDirectory=console
StateDirectoryMode=0750
Restart=on-failure
RestartSec=5s
TimeoutStopSec=30s
MemoryMax=256M
CPUQuota=50%
TasksMax=96
NoNewPrivileges=yes
CapabilityBoundingSet=
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectKernelTunables=yes
ProtectControlGroups=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
[Install]
WantedBy=multi-user.target
The unit deliberately publishes the HTTP port directly on the host network. On a
single home server, this is often the simplest and most transparent way.
Firewall, bind address, authentication, and TLS together determine from which
networks and under which conditions Console is reachable. If the interface
should be reachable locally only, the application binds to 127.0.0.1 or
::1; a reverse proxy can handle external access.
Meterbridge: Two Serial Ports, Nothing Else
Meterbridge additionally needs access to two USB IEC reading heads. Here it is
especially important not to simply expose the entire host /dev tree.
PrivateDevices=yes creates a reduced device landscape. The two required
devices are then explicitly bound in and allowed through the cgroup device
policy.
# /etc/systemd/system/meterbridge.service
[Unit]
Description=Meterbridge Smart-Meter to MQTT Gateway
Wants=network-online.target
After=network-online.target
ConditionPathExists=/dev/ttyUSB0
ConditionPathExists=/dev/ttyUSB1
[Service]
Type=exec
ExecStart=/usr/local/libexec/meterbridge
EnvironmentFile=/etc/meterbridge/meterbridge.env
DynamicUser=yes
SupplementaryGroups=dialout
Restart=on-failure
RestartSec=5s
TimeoutStopSec=30s
MemoryMax=128M
CPUQuota=25%
TasksMax=32
NoNewPrivileges=yes
CapabilityBoundingSet=
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
PrivateDevices=yes
BindPaths=/dev/ttyUSB0
BindPaths=/dev/ttyUSB1
DeviceAllow=/dev/ttyUSB0 rw
DeviceAllow=/dev/ttyUSB1 rw
ProtectKernelTunables=yes
ProtectControlGroups=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
[Install]
WantedBy=multi-user.target
The combination matters: BindPaths= makes the devices visible in the private
device namespace, DeviceAllow= permits access to them in the cgroup, and
SupplementaryGroups=dialout satisfies the normal Unix file permissions of many
distributions. If one of these three layers is missing, the service cannot open
the port despite a seemingly correct configuration.
/dev/ttyUSB0 and /dev/ttyUSB1 are chosen in the example for readability
only. Their numbering can change after a reboot. In a production setup, a udev
rule should create a stable name for each reading head, for example
/dev/smartmeter/main and /dev/smartmeter/solar. These stable paths are then
used consistently in ConditionPathExists=, BindPaths=, DeviceAllow=, and
the Meterbridge configuration. For this, the rule has to match on stable
properties such as the serial number or USB port path, not on the changing
ttyUSB number. If Meterbridge should also start automatically after a later
plug-in, it additionally needs udev-triggered startup logic or a suitable device
dependency; ConditionPathExists= alone skips the start at boot and does not
react to hotplug.
Choosing the Right Boundary
Isolation is not a one-dimensional staircase. Each level protects different resources and brings different operational work. An OCI container still shares the kernel with the host; a VM separates the kernel, but does not automatically make networking, images, and patching secure. Conversely, a consistently hardened systemd unit can be more effective for a single service than a container with generous mounts and root privileges.
The traffic lights rate each column: 🟢 stands for a strong property, 🟡 for a deliberate trade-off, and 🔴 for a weak property. The right-hand column describes when this trade-off makes sense.
| Mechanism | Simple operations | Technical separation | When it fits |
|---|---|---|---|
| Host binary | 🟢 | 🔴 | One-off, fully trusted admin tools or local development. For long-running services, resource, user, filesystem, and process boundaries are missing. |
chroot |
🟢 | 🔴 | An older software stack or a build step needs a different root path. It is not a security container: processes, network, and many host properties remain visible. |
| Hardened systemd unit | 🟢 | 🟡 | Standalone, statically linked Go services on one host. cgroups, dynamic UID, mount and device restrictions are visible declaratively in one unit. |
systemd-nspawn |
🟡 | 🟡 | An appliance or legacy workload needs its own root file tree, PID and mount namespaces, or an optionally separate network. The host kernel remains shared. |
| OCI container with Quadlet | 🟡 | 🟡 | Images are already the delivery format or the application brings several runtime dependencies. Image, registry, and runtime then become part of the supply chain. |
| Virtual machine | 🔴 | 🟢 | Different trust zones, foreign code, a dedicated kernel, or hard tenant separation justify the higher resource and patching effort. Hypervisor, network, and passthrough configuration remain part of the security boundary. |
The table is not a ranking. The question is not “What is the most modern?” but: Which resource has to be protected from which failure or attacker?
For Governer, Console, and Meterbridge, the hardened systemd unit is the smallest suitable level. The services are standalone binaries, their responsibilities are already separated, and only Meterbridge needs two explicit hardware resources. An additional root file tree, a registry, or a container runtime would create more operational surface in the current model without better covering any relevant requirement.
The Switching Point to nspawn, Quadlet, or VM
The three systemd units are not a dead-end design. If the requirements change, the next step is clear rather than reflexive:
- A service needs a foreign or historically grown Linux userspace:
systemd-nspawngives it its own root file tree and additional namespaces without immediately turning it into a container platform. - The application is delivered with libraries, helper programs, and
reproducible images: an OCI image with a
Quadlet makes this delivery
contract declarative and still operable via
systemctl. - The service processes untrusted code, needs its own kernel, or separates tenants: a VM provides the appropriate virtualization boundary.
systemd-nspawn and Quadlets are thus not competing answers to this setup. They
are additional, clearly delimited tools for when the need for protection or
delivery grows beyond a hardened process boundary.
The yardstick remains: choose the smallest boundary that reliably addresses the
concrete risk. For Governer, Console, and Meterbridge, the visible operating
contract therefore consists of three unit files, three configuration
directories, and three binaries. Start, stop, logs, limits, and the permitted
host resources remain traceable in one place everywhere: systemctl,
journalctl, and cgroups.
References
- systemd.exec(5)
- systemd.resource-control(5)
- systemd.service(5)
- systemd-nspawn(1)
- Arch Wiki: systemd-nspawn
Image credit: Tux, the Linux mascot, was created by Larry Ewing (using GIMP, 1996); used for editorial reporting.