agentleFS
leroyguillaumeleroyguillaume/claude/systemd-conventions/SKILL.md

systemd-conventions

Skill7 starsChanged 3 days agoMITView on GitHub

systemd units and cloud-init, centred on service privileges. TRIGGER when: editing a `.service`, `.socket`, `.timer` or drop-in `override.conf`; writing `#cloud-config`/user-data (Terraform `user_data`, Packer, Ignition); making an app start at boot on a VM; user asks about systemd, service hardening, running as root or cloud-init. SKIP when: the workload runs in Kubernetes or Compose (`docker-conventions`, `helm-conventions`).

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Create it declaratively (systemd-sysusers or cloud-init's users: block), never with a useradd buried in runcmd.

    line 32
  2. Very specific:

    ExecStartPre= runs as the service user too: If a preparation step genuinely needs root, mark that one line + (ExecStartPre=+/usr/bin/…) …

    line 55
  3. Very specific:

    Every *DirectoryMode= defaults to 0755, and UMask= does not apply to these directories — without the explicit mode, the rest of the box can …

    line 134

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

systemd and cloud-init conventions

On a VM, the unit file is the security boundary — there is no pod security context, no admission controller, nothing else between the process and the machine. A service running as root because it was quicker to write is a root shell one deserialisation bug away. Everything below is about closing that gap, and it costs about fifteen lines of ini.

The service does not run as root

  • User= and Group= in every unit, always. A dedicated system account per service, named after the app, with no login shell and no password:

    [Service]
    User=myapp
    Group=myapp

    Create it declaratively (systemd-sysusers or cloud-init's users: block), never with a useradd buried in runcmd. How, and the account's own properties — shell, password, home, groups, and the file modes around it — are system-user-conventions.

  • DynamicUser=yes when the service only touches its own state. systemd allocates a transient UID for the lifetime of the unit, and implies ProtectSystem=strict, PrivateTmp=, RemoveIPC= and friends. Do not use it when the UID must be stable across reboots (NFS, a shared volume, files another account reads) — state then lives under /var/lib/private/ with a UID that changes under you.
  • Never User=root, and never a wrapper that shells out to sudo. If root looks necessary, name the one capability that is actually needed and grant only that:

    CapabilityBoundingSet=CAP_NET_BIND_SERVICE
    AmbientCapabilities=CAP_NET_BIND_SERVICE

    For a port below 1024 the better answer is usually to not need the capability: bind a high port and put the reverse proxy in front, or let a .socket unit (owned by root, activated as the service user) hold the privileged listener.

  • ExecStartPre= runs as the service user too. If a preparation step genuinely needs root, mark that one line + (ExecStartPre=+/usr/bin/…) rather than promoting the whole unit. PermissionsStartOnly= is deprecated — do not write it.

The hardening block

Paste this into every long-running unit, then remove only what demonstrably breaks the app — with a comment saying what broke:

[Service]
User=myapp
Group=myapp
NoNewPrivileges=true
CapabilityBoundingSet=
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
PrivateDevices=true
ProtectProc=invisible
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectKernelLogs=true
ProtectControlGroups=true
ProtectClock=true
ProtectHostname=true
RestrictNamespaces=true
RestrictRealtime=true
RestrictSUIDSGID=true
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
LockPersonality=true
MemoryDenyWriteExecute=true
SystemCallArchitectures=native
SystemCallFilter=@system-service
UMask=0077
StateDirectory=myapp
StateDirectoryMode=0700

What each of the load-bearing ones actually buys, since a directive nobody understands is a directive somebody deletes:

  • NoNewPrivileges=true — the process and every child can never gain privileges again, so a setuid binary reachable from the service (sudo, mount, pkexec) becomes inert. This is the single highest-value line; it is also what makes SystemCallFilter= enforceable.
  • ProtectSystem=strict — the entire filesystem is read-only except /dev, /proc, /sys. Combined with ProtectHome=true (/home, /root, /run/user empty), a compromised service cannot persist anything on disk.
  • CapabilityBoundingSet= (empty) — drops every capability, including the ones a root-owned parent might have handed down. List capabilities explicitly when one is genuinely needed; never leave it unset.
  • MemoryDenyWriteExecute=true — blocks W+X mappings. It breaks every JIT runtime: Node, the JVM, .NET, LuaJIT, anything with a tracing JIT. Drop it for those, keep it for Rust/Go/C binaries.
  • SystemCallFilter=@system-service — a seccomp allowlist covering what a normal daemon does. Add narrow groups when needed (SystemCallFilter=~@privileged @resources style subtraction is fine), never replace it with nothing.

Writable paths are declared, not chmod-ed

ProtectSystem=strict means the app cannot write anywhere it has not been given. Grant that with the *Directory= directives, never with a mkdir -p plus chown in provisioning:

NeedDirectivePath
Persistent stateStateDirectory=myapp + StateDirectoryMode=0700/var/lib/myapp
Runtime sockets/PIDsRuntimeDirectory=myapp + RuntimeDirectoryMode=0700/run/myapp, wiped on stop
CachesCacheDirectory=myapp + CacheDirectoryMode=0700/var/cache/myapp
Own log filesLogsDirectory=myapp + LogsDirectoryMode=0700/var/log/myapp
ConfigConfigurationDirectory=myapp + ConfigurationDirectoryMode=0755/etc/myapp

Who owns each path, and the modes of the files inside, are system-user-conventions.

Every *DirectoryMode= defaults to 0755, and UMask= does not apply to these directories — without the explicit mode, the rest of the box can list and read them. The config directory keeps 0755 on purpose: systemd creates it root:root and never changes its owner, so a 0750 locks the service out. systemd creates them with the right owner and mode before ExecStart=, and recreates them if they are missing — provisioning that pre-creates them by hand is one chown away from being wrong and never noticed. ReadWritePaths= is the last resort, for a path the app does not own (a data mount, a socket directory shared with another service); list the narrowest path that works.

Logging goes to stdout/stderr and the journal — no log file, no logrotate config, no LogsDirectory= at all in the normal case. See logging-conventions.

Secrets never sit in the unit file

  • Never Environment=API_TOKEN=…. Unit files are world-readable, and systemctl show myapp.service prints the environment to any unprivileged user on the box. Same for a secret on the ExecStart= command line: it is in /proc/<pid>/cmdline, visible in ps.
  • Prefer LoadCredential=: the file is mounted read-only into the service's private credential directory, owned by the service user, invisible to every other process and never in the journal:

    LoadCredential=api-token:/etc/myapp/api-token

    The app reads $CREDENTIALS_DIRECTORY/api-token. SetCredential= inlines a value in the unit — that is the same leak as Environment=, so only for non-secret data.

  • EnvironmentFile=/etc/myapp/env is the acceptable fallback for apps that only read the environment: root:root, mode 0600 — PID 1 reads it, not the service.

cloud-init

The provisioning file is where privileges are handed out for the life of the machine, and it is very easy to hand out too many.

  • runcmd and bootcmd run as root with no sandbox — runcmd once per instance, bootcmd on every boot. Use them to install and enable — systemctl enable --now myapp.service — never to run the application itself. An app started from runcmd is a root process with no unit, no restart, no hardening and no journal identity.
  • Every write_files entry carries explicit owner: and permissions:. The defaults are root:root and 0644; a credential written with the defaults is readable by every account on the machine. Quote the mode so YAML does not mangle the leading zero. An entry owned by the service account needs defer: true, or it is written before users: creates the account and the chown fails:

    write_files:
      - path: /etc/myapp/config.toml
        owner: root:myapp
        permissions: '0640'
        defer: true
        content: ""
      - path: /etc/systemd/system/myapp.service
        owner: root:root
        permissions: '0644'
        content: |
          [Unit]
          Description=myapp
  • The service account gets no sudo, ever — system-user-conventions owns that rule and has the users: block.
  • Lock the box's own doors: disable_root: true, ssh_pwauth: false, keys via ssh_authorized_keys only. No password hashes in user-data.
  • Never put a secret in user-data. It stays readable for the instance's entire life from the metadata endpoint (169.254.169.254) by any process on the machine, including the compromised one, and most providers keep it in the instance description too. Provision the value from the cloud's secret manager at boot, or place the file out of band and reference it with LoadCredential=.
  • write_files runs before runcmd — rely on that ordering rather than re-writing files from runcmd. After dropping a unit file, runcmd needs systemctl daemon-reload before enable --now.
  • Block style YAML only (yaml-conventions), and validate the file: cloud-init schema --config-file cloud-config.yaml.

Verify, don't assume

  • systemd-analyze security myapp.service is the lint for a unit. Run it after every unit change. A bare unit with no hardening scores around 9.6 ("UNSAFE"); the block above typically lands between 1 and 3 ("OK"). Treat anything above 5.0 as unfinished work, and read the per-directive table it prints rather than guessing which line is missing. For a unit file in the repo that is not installed, systemd-analyze security --offline=true ./myapp.service (systemd ≥ 250).
  • systemd-analyze verify ./myapp.service catches typos and bad references — an unknown directive is silently ignored at runtime, so a misspelled ProtectSytem= fails open, quietly, forever.
  • systemctl show myapp.service -p User -p CapabilityBoundingSet -p NoNewPrivileges after deploying: it prints what is effective, which is the only thing that counts. And systemctl status should show the service running under the expected UID.
  • Vendor units are never edited in place. Override with a drop-in: /etc/systemd/system/myapp.service.d/override.conf, then systemctl daemon-reload.

Also in the unit, while you are there

  • Restart=on-failure with RestartSec= — and the process handles SIGTERM so the restart is clean (signal-handling-conventions).
  • Type=notify when the app can signal readiness, Type=exec otherwise. Type=simple claims readiness before the process has done anything, so After= ordering on it means nothing.
  • TimeoutStopSec= longer than the app's drain window, otherwise systemd SIGKILLs mid-drain — exactly the failure the drain logic exists to avoid.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.