DEV Community

Cover image for Systemd Explained: Units, Targets, and Why systemctl Is More Than Start and Stop
Rijul Rajesh
Rijul Rajesh

Posted on

Systemd Explained: Units, Targets, and Why systemctl Is More Than Start and Stop

You must have often used the systemctl commands frequently, but do you know what they exactly are? Rather than just thinking of them as a way to toggle some services on and off?

This article is here to give you a better idea of what systemctl actually is.

What is systemd?

You can think of systemd as the init system and service manager for Linux.

When your machine boots, systemd is the first user-space process (usually running as PID 1).
It is responsible for starting, stopping, and supervising most other user-space processes on the system.

Things that systemd does

Rather than thinking of it as just starting and stopping things, systemd handles a wider variety of tasks.

These include:

  • Starting services
  • Stopping services
  • Restarting on failure
  • Dependency ordering
  • Boot-time startup
  • Logging (via journald)
  • Sockets, timers, mounts, devices

How systemd works

We can broadly term it into three things when specifying how systemd works.

1. Unit files

What is a unit?

A unit is a declarative description of something systemd can manage.
This can include:

  • Process
  • Socket
  • Mount
  • Timer
  • Logical grouping

We can take the example of an nginx.service file, where .service is a type of unit.

[Unit]
Description=NGINX Web Server
After=network.target

[Service]
ExecStart=/usr/sbin/nginx
Restart=always

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Unit section

  • This section doesn’t start anything
  • It acts as a dependency and ordering graph
  • After=network.target means that if both are started, network.target will be started before nginx (it controls ordering, not readiness)

Service – Process lifecycle

  • ExecStart defines the exact binary systemd will fork + exec. systemd becomes the parent process, and systemd will track the PID
  • Restart=always creates a control loop. If nginx exits, systemd notices and restarts it

From this file, systemd will get an idea of:

  • What to run
  • When to run
  • How to start
  • When to stop

Install

  • Does nothing at runtime
  • It is used when you run systemctl enable nginx
  • It creates a symlink so it starts on boot

2. Targets

In systemd, a target represents a system state or milestone, not a process or a service.

A service is started as part of a target by declaring that it should be started when systemd is trying to reach that target.

For example:

systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

Creates a link:

multi-user.target.wants/nginx.service
Enter fullscreen mode Exit fullscreen mode

When systemd activates a target, it starts all services that are wanted by that target, respecting dependency and ordering rules.

multi-user.target
Represents a fully booted system in text mode (no GUI).
This is the normal state for servers and headless machines.
Most backend services (databases, web servers, APIs) are started here.

graphical.target
Represents a system with a graphical environment available.
Desktop systems usually boot into this target.
Internally, it includes multi-user.target and then adds display managers and GUI services.

network.target
Indicates that the basic networking stack is initialized.
It does not guarantee that the network is fully configured or that the internet is reachable.
It is mainly used for startup ordering, not readiness checks.

3. systemctl

systemctl is just a remote control for systemd.

So you have been using this remote control to handle services.

systemctl start postgresql
Enter fullscreen mode Exit fullscreen mode

Wrapping up

Hope this article gives you a better idea of systemd and how it works. This will serve you better when you deal with problems related to process management, since you now have a better idea of how things work.

If you’ve ever struggled with repetitive tasks, obscure commands, or debugging headaches, this platform is here to make your life easier. It’s free, open-source, and built with developers in mind.

👉 Explore the tools: FreeDevTools
👉 Star the repo: freedevtools

Top comments (0)