İçeriğe Atla
Mustafa Erbay
Tutorials · 12 min read · görüntülenme Türkçe oku
100%

Hardening Services with systemd Sandboxing (ProtectSystem…

Constrain services into a tighter permission set without changing the application itself: filesystem, capability, syscall, and network limits.

Hardening Services with systemd Sandboxing (ProtectSystem… — cover image

For service hardening on Linux, there are heavy-duty tools like AppArmor and SELinux. But in the field, the fastest “impact per effort” usually comes from systemd unit sandboxing: you shrink the permission surface without modifying the application.

In this article I’ll walk through a model that begins with “minimum risk” and tightens the screws gradually.

Starting point: observe first

Before you start hardening, get clarity on two questions:

  • Which files does the service write to? (logs, tmp, cache, sockets)
  • Which ports does it dial out / listen on?

Without that observation, settings like ProtectSystem or ReadOnlyPaths turn into “break it and burn it.”

Phase 1 — The low-risk trio

These three settings rarely cause surprises on most services:

  • NoNewPrivileges=true
  • PrivateTmp=true
  • ProtectHome=true (when the service has no need for home access)

Phase 2 — Filesystem: ProtectSystem

This is the most effective lever — and the one most likely to break things:

  • ProtectSystem=full (some paths read-only)
  • ProtectSystem=strict (almost everything read-only)

At this point you explicitly declare where your service is allowed to write:

  • ReadWritePaths=/var/lib/myapp /var/log/myapp
  • StateDirectory=myapp (lets systemd own this for you)
  • CacheDirectory=myapp

Sample unit fragment

[Service]
DynamicUser=true
StateDirectory=myapp
CacheDirectory=myapp
LogsDirectory=myapp

NoNewPrivileges=true
PrivateTmp=true
ProtectHome=true
ProtectSystem=strict
ReadWritePaths=/var/lib/myapp /var/log/myapp

Phase 3 — Identity: DynamicUser

With DynamicUser=true, the service runs in isolation without provisioning a permanent system user. Wins I’ve seen in the field:

  • the “accidentally shared user” pattern goes away
  • the permission surface narrows
  • the service leaves no trace once removed

But watch out:

  • If the service must persist files, use a systemd-managed directory like StateDirectory=
  • Some applications expect a “stable” UID/GID — that’s a case for an exception

Phase 4 — Linux capabilities and syscall filters

The most important principle: keep the capability list minimal.

Example:

  • A web service typically only needs CAP_NET_BIND_SERVICE (if it has to listen on 80/443).
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE

Syscall filters are the next level up:

SystemCallFilter=@system-service
SystemCallErrorNumber=EPERM

Phase 5 — Network restrictions (when needed)

If the application only uses certain address families:

RestrictAddressFamilies=AF_INET AF_INET6

If the service “shouldn’t go out at all,” tighter restrictions are an option, but the breakage risk grows. I’d recommend observing egress first.

Rollout and rollback

I roll out hardening changes through a “release ring” too:

  • Canary: 1 host
  • Pilot: 5–10%
  • Prod: the rest

The rollback plan should be simple:

  • Revert the unit drop-in file
  • systemctl daemon-reload && systemctl restart

Closing: hardening should be managed like a product

systemd sandboxing is not a “one-shot security task” — it should be a part of the service lifecycle. The best results come in small, measured steps: which setting reduced which risk, and at what operational cost?

Paylaş:

Bu yazı faydalı oldu mu?

Yükleniyor...

Bu yazı nasıldı?

ME

Mustafa Erbay

Sistem Mimarisi · Network Uzmanı · Altyapı, Güvenlik ve Yazılım

2006'dan bu yana sistem mimarisi, network, sunucu altyapıları, büyük yapıların kurulumu, yazılım ve sistem güvenliği ekseninde çalışıyorum. Bu blogda sahada karşılığı olan teknik deneyimlerimi paylaşıyorum.

Kişisel Notlar

Bu notlar sadece sizde saklanır. Tarayıcınızda yerel olarak tutulur.

Hazır 0 karakter

Comments

Server-side AI Moderation

Comments are AI-moderated server-side and stored permanently.

?
0/2000

Server-side AI moderation

✉️ Free · No spam · Unsubscribe anytime

Curated digest, hand-picked by me — not the AI

Once a week: the most important post of the week, behind-the-scenes notes, and a "what I actually used this week" section. Less noise, more signal.

  • 📌
    Best of the week Single most-worth-reading post
  • 🔧
    Toolbox notes Real tools I used this week
  • 🧠
    Behind-the-scenes Notes that don't make it to blog

We don't spam. Unsubscribe anytime. · Tracked only by Umami (self-hosted, no Google).

Your Reading Stats

0

Posts Read

0m

Reading Time

0

Day Streak

-

Favorite Category

Related Posts