Systemd Explained: Services, Timers and Fixing Failed Units on Linux


Boot a modern Linux machine and systemd is the first thing running. It starts your services, manages your network, schedules your jobs and logs almost everything. Yet most people only know two commands: systemctl start and systemctl restart. Here’s the mental model that makes the rest click.

What Systemd Actually Is

Systemd is the init system — process number 1, the parent of everything else. It reads small text files called units that describe what should run, when, and how. The most common unit types are:

  • Service units (.service) — a program that runs, like a web server or database.
  • Timer units (.timer) — a schedule that triggers something, systemd’s replacement for many cron jobs.
  • Socket, mount and target units — more specialised pieces you will meet later.

Units live in /etc/systemd/system/ (yours and your admin’s) and /lib/systemd/system/ (shipped by packages). Never edit the package copies directly — your changes get overwritten on update. Override them instead with systemctl edit.

The Commands You Actually Need

systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl enable nginx
systemctl disable nginx
systemctl is-enabled nginx

The distinction beginners miss: start affects right now, enable affects boot. A service can be running but not enabled (it won’t survive a reboot), or enabled but not running (it will start next boot, but isn’t up now). For a server, you almost always want both.

Reading a Failed Service

When a service fails, don’t just restart it and hope. Read what it said first:

systemctl status nginx --no-pager
journalctl -u nginx -n 50 --no-pager

The first shows the exit code and the last few log lines. The second shows the service’s full journal — its real logs, including why it died. Nine times out of ten the answer is in there: a bad config path, a port already in use, a missing permission. Common causes of failure, in order of likelihood: a typo in a config file you just edited, a port conflict with another service, a file the service can’t read because of permissions, or a dependency (like a database) that isn’t up yet.

Timers: Cron’s Modern Cousin

A timer unit triggers a service on a schedule. List them with: systemctl list-timers –all

Timers have real advantages over cron: they log to the journal like everything else, they can catch up on missed runs after downtime, and you can see their next run time at a glance. Writing one is two files: a .service that does the job and a .timer that says when. For a daily security check at 2am, a timer is the cleaner choice.

Safe Habits

  • Check status before restarting. Restarting hides the error message you need.
  • Use systemctl edit for overrides, not direct edits of package files.
  • After editing any unit file, run systemctl daemon-reload so systemd re-reads it. Forgetting this is the classic “why didn’t my change work” bug.
  • Only manage services you understand. Disabling a service you don’t recognise can break boot. Check what it does first with systemctl cat.
  • Never run untrusted unit files. A service unit runs with the privileges you give it — a malicious one is a straight path to a compromised machine. Only install units from trusted sources, and only on systems you own or administer.

Systemd feels complex because it does a lot. But the core loop is simple: check status, read the journal, fix the cause, reload, restart. Learn that loop and you can run any Linux server with confidence.

Comments

Popular Posts