Units, targets and dependencies

Intermediate · 10 min

What a systemd unit is, the unit types you will meet, and why Wants= and Requires= decide what starts while After= decides when.

Before thissystemd as PID 1 and boot targets

tutoriaLinux · 10 min

Units, targets and dependencies

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by tutoriaLinuxReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A unit is anything systemd manages: services, sockets, mounts, timers, slices, paths, devices and targets. The file suffix says which type it is.
  • A target is a unit that does nothing itself. It is a named group of other units, which is how boot stages like multi-user.target are built.
  • Wants=, Requires=, BindsTo= and the rest say which units are pulled in together. Before= and After= say in what order. The two are independent.
  • A Wants= with no After= starts both units at the same time. When you mean 'start this after that', you almost always need both lines.
  • Wants= is the usual choice, because a failed wanted unit does not stop yours. Requires= also stops your unit if the other is explicitly stopped.
  • `systemctl list-dependencies` shows the requirement tree, and `--after` or `--before` show the ordering tree instead.

Two questions every boot has to answer

A web app needs its database. The database needs its disk mounted. The disk mount needs the device to exist. On an old init system, someone encoded this by numbering shell scripts: S20mount, S40postgres, S60app, run in order, one at a time. That works until a slow script holds up everything behind it, or someone picks the wrong number.

systemd splits the problem into two separate questions:

  1. What must run together? If I start the app, which other things should be started too?
  2. In what order? Of the things that are starting, which must be finished before another begins?

Everything else that is not ordered is started in parallel. That is why a systemd boot is fast, and also why the most common systemd bug is answering only the first question. The devops systemd lesson wrote a first service. This one is about how units relate to each other.

A unit is anything systemd manages

A unit is a small ini-style file whose suffix says what kind of thing it describes. To see the real mix, I downloaded Debian 13’s systemd package (version 257.13) from deb.debian.org, unpacked it on this Mac with ar and tar, and counted the files in usr/lib/systemd/system:

$ find . -maxdepth 1 ! -type d | sed -E 's/.*\.//' | sort | uniq -c | sort -rn
 111 service
  69 target
  14 socket
   9 mount
   3 slice
   2 path
   1 timer
   1 automount

What each type is for:

  • .service: a process to run and supervise.
  • .socket: a listening socket systemd opens, starting the service on the first connection.
  • .mount and .automount: a mount point. The fstab lesson shows these are generated from /etc/fstab.
  • .timer: a schedule that starts another unit, covered two lessons from now.
  • .path: watches a file or directory and starts a unit when it changes.
  • .slice: a node in the cgroup tree for resource limits, coming in the cgroups lesson.
  • .target: a group of units with no action of its own.
  • .device and .scope are made at runtime, not written as files, which is why none appear above.

Targets are named groups

Sixty-nine targets in one package says how much of boot is described by them. Here is multi-user.target from that package, minus its license header:

[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes

It runs nothing. It says “being in multi-user mode means basic.target is up, and rescue mode is not”. Services join it from the other side: a service’s [Install] section says WantedBy=multi-user.target, and systemctl enable turns that into a symlink in multi-user.target.wants/. The same package ships that directory with four links in it already, including systemd-logind.service. The boot targets lesson covered which target the system boots to.

Requirement dependencies: who comes along

The [Unit] section has a family of settings that pull other units in. From weakest to strongest, per systemd.unit(5) for systemd 262:

Setting Pulls the other unit in? If the other unit fails to start If the other unit stops later
Wants= Yes Yours starts anyway Nothing happens to yours
Requires= Yes Yours does not start, if you also have After= Yours stops only if the other was explicitly stopped
Requisite= No, it must already be running Yours fails at once As Requires=
BindsTo= Yes As Requires= Yours stops even if the other died on its own

Two more change the shape. PartOf= only passes stop and restart from the other unit to yours. Conflicts= is the negative: starting one stops the other, which is how multi-user.target and rescue.target exclude each other.

The man page says it plainly: Wants= is “the recommended way to hook the start-up of one unit to the start-up of another unit”, and it suggests Wants= over Requires= for a system that copes better with failing services.

Ordering is a separate axis

Before= and After= do not pull anything in. They only say: if both units are being started, finish this one first. The man page’s own example is the trap: if foo.service has Wants=bar.service and no ordering, “both units will be started simultaneously and without any delay between them”.

So the usual pair is:

[Unit]
Wants=postgresql.service
After=postgresql.service

“Finished starting” depends on the service type. For a Type=notify service it means the daemon reported ready. For the default Type=simple, it means only that the process was forked, which is why the next lesson spends time on Type=. On shutdown the order reverses: a unit with After=postgresql.service is stopped before postgres.

The network-online.target trap is the same idea. systemd.special(7) says a unit that really needs a configured network must pull it in with Wants= and order itself After= it. After= alone does nothing if nothing else pulled the target in.

Dependencies you did not write

Services get some dependencies for free. systemd.service(5) lists them: unless DefaultDependencies=no is set, a service gets Requires= and After= on sysinit.target, After= on basic.target, and Conflicts= and Before= on shutdown.target. That is why your service never starts before the filesystems are up, and stops cleanly at shutdown. Early-boot services such as systemd-journald.service turn this off, because they must run before sysinit.target.

Seeing the graph

These are the commands I would run. I had no running systemd here, so they are from systemctl(1), not a live session:

systemctl list-dependencies sshd.service            # what it pulls in (Wants/Requires/...)
systemctl list-dependencies --reverse sshd.service  # what pulls it in (WantedBy/RequiredBy)
systemctl list-dependencies --after sshd.service    # what must start before it
systemctl show -p Wants,Requires,After sshd.service # the raw properties, defaults included

By default only targets are expanded recursively, and --all expands everything. The man page warns that it lists only units currently loaded, so it is not a full reverse-dependency search of the disk.

About the video

The embed is the whole of tutoriaLinux’s “systemd Dependencies & Ordering”, part 4 of his systemd series, August 2021, about 10 minutes. Its chapters run from “Dependencies ARE NOT Ordering” through Wants/WantedBy, Requires/RequiredBy, Before and After, to Requisite, BindsTo, PartOf and Conflicts, which matches this lesson’s core. It does not count unit types or cover default dependencies, which is why the lesson does. Part 1 of the same series is the embed in the devops systemd lesson. This is a different episode. Neither the video nor this lesson covers Upholds= (added in systemd 249), a rarer keep-it-running dependency. English was judged from the title, channel and the English auto-caption track. I judged its content from the chapters and did not watch it through.

What was run: the package download, unpacking and counting above, on macOS. Everything about systemd’s behaviour is from the systemd 262 man pages, checked 2026-11-28.

Where this fits

You can now read the [Unit] section of any unit and say what it pulls in and when it starts. Next, writing your own units and changing the ones your distribution ships without editing them: writing and overriding unit files.

Check yourself

  1. 01A unit has Wants=postgresql.service and nothing else about postgres. Does systemd wait for postgres to finish starting before starting this unit?

    No. Wants= only pulls postgres into the same transaction. Without After=postgresql.service the two start in parallel.

  2. 02What is the difference between Requires= and BindsTo= when the other unit's process exits on its own?

    Requires= only reacts when the other unit is explicitly stopped or restarted. BindsTo= also stops your unit when the other one stops unexpectedly or fails.

  3. 03Why does a service unit without any After= lines still start after basic system setup?

    Default dependencies. Unless DefaultDependencies=no is set, a service gets Requires= and After= on sysinit.target, After= on basic.target, and Conflicts= and Before= on shutdown.target.

  4. 04How do you see which units must be started before sshd.service, rather than which units it pulls in?

    systemctl list-dependencies --after sshd.service. Plain list-dependencies follows requirement dependencies, --after follows ordering.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark