Linux systemd Explained: systemctl Cheat Sheet, Unit Files and Timers

understanding-linux-systemd

Linux systemd is the first process that starts on almost every modern distro, and it manages everything after that: services, mounts, timers, logs. Day to day you talk to it with two commands, systemctl to control services and journalctl to read their logs. Learn about 15 commands and one unit file layout and you can run your own services on any Ubuntu, Debian, Fedora or Arch box.

This page is built so you can come back to it. There’s a copy-paste cheat sheet, a unit file template I’ve checked with systemd-analyze verify, a timer that replaces cron, and the fixes for “my service won’t start”, which is why most people end up here.

The Short Version

  • Start a service and enable it at boot in one go: sudo systemctl enable --now nginx
  • See why it’s broken: systemctl status nginx then journalctl -u nginx -n 50
  • Your own services live in /etc/systemd/system/. After editing one, run sudo systemctl daemon-reload.
Table of Contents

What Is systemd in Linux?

systemd is the init system and service manager. The kernel starts it as PID 1, and from there it starts everything else in parallel, tracks every process it launched, restarts things that crash, and collects all their logs in one place.

Before systemd, distros used SysV init: a pile of shell scripts in /etc/init.d run one after another. systemd replaced those with declarative unit files. Some people still dislike it for doing too much, and they have a point, but it won. If you run Linux servers, you’re using it. For the history and the debate, see my older post on what systemd is used for.

systemctl and journalctl Cheat Sheet

Swap nginx for your service name. You can leave off .service, systemd assumes it.

Task Command
Status, recent logs, PID systemctl status nginx
Start / stop / restart sudo systemctl start|stop|restart nginx
Reload config without a restart sudo systemctl reload nginx
Start at boot sudo systemctl enable nginx
Start now and at boot sudo systemctl enable --now nginx
Stop now and not at boot sudo systemctl disable --now nginx
Is it enabled? Is it running? systemctl is-enabled nginx / systemctl is-active nginx
Everything that failed systemctl --failed
All running services systemctl list-units --type=service --state=running
Pick up edited unit files sudo systemctl daemon-reload
Block a service from ever starting sudo systemctl mask nginx
Show the unit file in use systemctl cat nginx
Override settings safely sudo systemctl edit nginx
Logs for one service journalctl -u nginx
Follow logs live journalctl -u nginx -f
Last 50 lines journalctl -u nginx -n 50 --no-pager
Logs since this boot journalctl -b
Only errors journalctl -p err -b
Last hour journalctl --since "1 hour ago"
What slowed down boot systemd-analyze blame
Scheduled timers systemctl list-timers

The two I’d make everyone memorize: enable --now, because people forget that start doesn’t survive a reboot and enable doesn’t start anything right away, and systemctl edit, because it writes an override file instead of touching the package’s unit file, so your change survives updates.

Understanding Unit Files

A unit file is a small INI-style file that tells systemd what to run and how. Package units live in /lib/systemd/system/ (or /usr/lib/systemd/system/). Yours go in /etc/systemd/system/, which takes priority.

A Unit File Template You Can Copy

This runs a Python app as its own user, restarts it if it crashes, and caps its memory. Save it as /etc/systemd/system/myapp.service:

Snippettext
[Unit]
Description=My App
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=myapp
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/python3 /opt/myapp/app.py
Restart=on-failure
RestartSec=5
Environment=PORT=8080
MemoryMax=500M

[Install]
WantedBy=multi-user.target

Then load and start it:

Terminalbash
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
systemctl status myapp

Before you start a new unit, check it for typos:

Snippettext
systemd-analyze verify /etc/systemd/system/myapp.service

Key Sections Explained

  • [Unit]: description and ordering. After= only sets order, Wants= or Requires= actually pulls the other unit in.
  • [Service]: what to run. ExecStart needs an absolute path. Restart=on-failure brings it back after a crash.
  • [Install]: what enable hooks into. multi-user.target means “start at normal boot”.

Which Type= Do You Need?

Type Use it when
simple (default) The program runs in the foreground and keeps running. Most apps
exec Like simple, but systemd waits until the binary actually launched before calling it started
forking Old-style daemons that fork into the background. Usually needs PIDFile=
oneshot A script that runs and exits, like a backup job
notify The program tells systemd itself when it’s ready (sd_notify)

Getting this wrong is the most common reason a hand-written service “starts” and then immediately shows as inactive. If your app forks into the background and you left it on simple, systemd thinks it exited.

Replace Cron with a systemd Timer

Timers do what cron does, but you get logs in journalctl, missed runs after downtime, and dependencies for free. You need two files. First the job, /etc/systemd/system/backup.service:

Snippettext
[Unit]
Description=Nightly backup

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh

Then the schedule, /etc/systemd/system/backup.timer:

Snippettext
[Unit]
Description=Run backup every night at 02:30

[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true

[Install]
WantedBy=timers.target
Terminalbash
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers backup.timer

Persistent=true means if the machine was off at 02:30, the job runs at the next boot. Not sure your OnCalendar line means what you think? Test it: systemd-analyze calendar '*-*-* 02:30:00' prints the next run time. If you still prefer cron, my cronjob guide covers it.

systemd Target Units

Targets are groups of units, roughly what runlevels used to be. The two you’ll meet are multi-user.target (normal server boot, no GUI) and graphical.target (with a desktop).

Snippettext
# current default
systemctl get-default

# boot to console instead of a desktop
sudo systemctl set-default multi-user.target

Troubleshooting: systemd Service Won’t Start

Start with these two, every time:

Terminalbash
systemctl status myapp
journalctl -u myapp -n 50 --no-pager

The status output usually has a line like code=exited, status=203/EXEC. That code tells you most of what you need:

Status What it means Fix
203/EXEC systemd couldn’t run ExecStart Use an absolute path, check it exists and is executable (chmod +x), check the shebang
217/USER The User= account doesn’t exist Create it: sudo useradd -r -s /usr/sbin/nologin myapp
200/CHDIR WorkingDirectory doesn’t exist or isn’t readable Create the folder and fix ownership
status=1/FAILURE Your program itself exited with an error Read the journal. It’s your app’s error, not systemd’s

Other things that bite people:

  • Edited the file, nothing changed. You forgot sudo systemctl daemon-reload.
  • Works in your shell, fails as a service. Services don’t load your .bashrc. No custom PATH, no aliases, no ~. Use full paths and set variables with Environment= or EnvironmentFile=.
  • “Start request repeated too quickly”. It crashed several times in a row and systemd gave up. Fix the crash, then run sudo systemctl reset-failed myapp.
  • Starts before the network is up. Add After=network-online.target and Wants=network-online.target, like in the template.

Boot Time Issues

Snippettext
# slowest units first
systemd-analyze blame

# the chain that actually delayed boot
systemd-analyze critical-chain

Advanced Features

Resource Control

Every service runs in its own cgroup, so you can cap it right in the unit file: MemoryMax=, CPUQuota=50%, TasksMax=. A service that hits MemoryMax gets killed alone instead of dragging the whole box down. My Linux memory management guide explains why that matters.

Socket Activation

A .socket unit can listen on a port and only start the matching service when the first connection arrives. Handy for rarely used services. Most people never need it.

Security Considerations

systemd can sandbox a service with a few lines. Add these to [Service] and most apps keep working:

Snippettext
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/opt/myapp/data

Then see how exposed a service still is:

Snippettext
systemd-analyze security myapp

Never run your own services as root unless they truly need it. A dedicated user with User= costs you one useradd.

FAQ

What’s the difference between systemctl start and enable?

start runs it now. enable makes it start at boot. enable --now does both.

Where are systemd service files stored?

Your own go in /etc/systemd/system/. Package-provided ones are in /lib/systemd/system/ or /usr/lib/systemd/system/. Run systemctl cat name to see which file is actually used.

How do I see logs for a systemd service?

journalctl -u name. Add -f to follow live, or -n 50 for the last 50 lines.

Does WSL2 support systemd?

Yes. Add systemd=true under the [boot] section of /etc/wsl.conf, then run wsl --shutdown from Windows and reopen your distro.

Conclusion

You don’t need to know all of systemd. Bookmark the cheat sheet, copy the unit file template when you need a new service, and reach for status plus journalctl -u the second something breaks. That covers most of what you’ll ever do with it.

whoami

Stefan founded Ceos3c in 2016 and has spent a decade teaching cybersecurity, Linux, and web development through hands-on tutorials. He builds and ships real products while sharing his journey: ProofSwap.me, XPMetric.com, and EasyEarningsTracker.com.

More about me