systemd Service Generator
Build a .service unit and an optional .timer, with the command quoted the way systemd reads it, a sandbox scored the way systemd-analyze security scores it, and the next run times of any schedule.
Free · No signupBuilt in your browser
[Unit] Description=My application After=network.target [Service] Type=simple DynamicUser=yes ExecStart=/usr/local/bin/myapp Restart=on-failure RestartSec=5 # Sandboxing. Check the result with systemd-analyze security NoNewPrivileges=yes PrivateDevices=yes ProtectSystem=strict ProtectHome=yes ProtectKernelTunables=yes ProtectKernelModules=yes ProtectKernelLogs=yes ProtectControlGroups=yes ProtectClock=yes ProtectHostname=yes RestrictSUIDSGID=yes RestrictRealtime=yes RestrictNamespaces=yes LockPersonality=yes RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 SystemCallArchitectures=native [Install] WantedBy=multi-user.target
Save as /etc/systemd/system/myapp.service. The Install steps tab writes, checks and starts it in one go.
systemd-analyze security
4.8OK
0 is fully sandboxed, 10 is wide open. This is the score the server prints.
Counts most against it
CapabilityBoundingSet=~CAP_SYS_ADMINKeeps CAP_SYS_ADMINCapabilityBoundingSet=~CAP_SET(UID|GID|PCAP)Keeps CAP_SET(UID|GID|PCAP)CapabilityBoundingSet=~CAP_SYS_PTRACEKeeps CAP_SYS_PTRACECapabilityBoundingSet=~CAP_NET_ADMINKeeps CAP_NET_ADMIN
A dynamic user can write almost nowhere. Turn on the data folder for /var/lib/myapp if it saves anything.
Built in this tab as you type. Nothing you enter leaves this page.
From unit file to running fleet
Manage services across every server.
CtrlOps deploys apps and manages their services across your whole fleet: start, stop, watch status and logs, and monitor resources from one local-first desktop app, with your credentials encrypted on your own machine.
Directive reference
The lines that
matter most.
Every one of these is set from the form above. The full list is in man systemd.directives.
| Directive | Section | What it does |
|---|---|---|
Description | [Unit] | The name systemctl status and the journal show. |
After / Wants | [Unit] | After= only orders the start; Wants= pulls the other unit in. Most dependencies need both. |
Requires | [Unit] | Stop this service too when the listed unit stops or fails. |
Type | [Service] | When systemd counts the service as started: simple, exec, notify, forking or oneshot. |
ExecStart | [Service] | The command. Not run by a shell: pipes, redirects and && need /bin/sh -c. |
Restart / RestartSec | [Service] | When to bring it back, and how long to wait first. |
DynamicUser | [Service] | Runs it as a throwaway user created at start, with a read-only system around it. |
StateDirectory | [Service] | Creates /var/lib/name, owned by the service user, and keeps it writable inside the sandbox. |
ProtectSystem=strict | [Service] | The whole file system is read-only for it, except the paths you list. |
Environment | [Service] | One variable, quoted. $ is literal here; % has to be written %%. |
WantedBy | [Install] | What enable hooks it into. multi-user.target means at boot. |
OnCalendar | [Timer] | When a timer fires, such as daily or Mon..Fri 09:00. Persistent=true catches up on missed runs. |
How a unit file works
What you write.
What systemd runs.
What is a systemd service?
On modern Linux, systemd starts, supervises, restarts and logs long-running programs. Each one is described in a unit file such as myapp.service, in three sections: [Unit] for its name and what it depends on, [Service] for how to run it, and [Install] for how it is enabled. Files you write go in /etc/systemd/system/.
ExecStart is not a shell
systemd splits the command into words itself. A pipe, a redirect, && or a $(…) reaches the program as a literal argument, and a % is read as a specifier, so date +%Y fails to load. The generator reads your command the way a shell would: plain commands are quoted for systemd word by word, and anything that needs a shell runs under /bin/sh -c, with $ and % escaped so the shell sees exactly what you typed.
Choosing a service type
simple fits a program that stays in the foreground, which is almost all of them. exec also fails the start when the program cannot run at all. notify waits for the program to report it is ready, forking is for old daemons that background themselves, and oneshot is for a job that runs and exits, usually from a timer. systemd refuses Restart=always on a oneshot.
The sandbox, and its score
A handful of lines take most of a service's power away: ProtectSystem=strict makes the file system read-only, PrivateTmp and PrivateDevices hide what other programs use, and NoNewPrivileges stops setuid programs from lifting it back to root. Run systemd-analyze security myapp.service and systemd scores the result from 0, fully sandboxed, to 10, wide open. The score beside the unit above is computed the same way, check by check.
Where a sandboxed service can write
With ProtectSystem=strict a service can write almost nowhere, which is the point. Give it StateDirectory=myapp and systemd creates /var/lib/myapp, owned by the service user, and passes its path in $STATE_DIRECTORY. For other paths, list them in ReadWritePaths.
Timers instead of cron
A oneshot service paired with a .timer runs on schedule with its output in the journal, catches up on runs missed while the server was off with Persistent=true, and can wait for the network or a database first. OnCalendar takes expressions such as daily, Mon..Fri 09:00 or *:0/15; the generator shows how systemd normalizes yours and when it fires next.
Install, enable, and check logs
After saving the file, sudo systemctl daemon-reload makes systemd read it, and sudo systemctl enable --now myapp starts it and enables it at boot. systemctl status myapp shows whether it is running, and journalctl -u myapp -f follows its output.
systemd FAQ
systemd services,
explained.
Write a unit file with [Unit], [Service], and [Install] sections - at minimum an ExecStart command, plus WantedBy=multi-user.target if it should start at boot; Type defaults to simple. Save it to /etc/systemd/system/myapp.service, run sudo systemctl daemon-reload, then sudo systemctl enable --now myapp. This generator builds the file for you and shows those exact commands.
Add Restart=on-failure (or Restart=always) with RestartSec=5 in the [Service] section to restart after a crash, and enable the service (systemctl enable) with WantedBy=multi-user.target in [Install] so it also starts on boot.
Set User= and optionally Group= in the [Service] section, e.g. User=www-data. For a throwaway, automatically-managed account, use DynamicUser=yes instead - it runs the service under a transient user that exists only while the service runs.
Use a systemd timer (a oneshot service plus a .timer) when you want journal-integrated logging, missed-run catch-up with Persistent=true, and dependencies on other units. Use cron for simple, portable, one-line schedules. This tool can generate the paired .timer; use the cron generator for crontab.
Add directives like NoNewPrivileges=yes, ProtectSystem=strict, PrivateTmp=yes, ProtectHome=yes, and DynamicUser=yes to sandbox the service with almost no effort. The Sandbox setting in this generator turns on a safe set with Standard, or a tighter one with Strict, and shows the exposure score systemd-analyze security will give the unit.
Use journalctl -u myapp to see its logs, or journalctl -u myapp -f to follow them live. By default systemd captures the service's stdout and stderr into the journal, so you do not need to configure log files.
Keep reading
Tools and guides for your next step.
- Guide
DevOps Automation Tools
The automation ecosystem for provisioning, deploying, and operating infrastructure.
- Guide
Manage Multiple Servers Without Losing Control
Patterns for keeping a growing fleet of Linux servers organized and supervised.
- Tool
cron expression generator
The same schedules for crontab, with the next run times.