How to List and Trace systemd Services Belonging to Specific Target Units


2 views

When working with systemd, target units serve as synchronization points that group other units (services, sockets, etc.). To investigate which units belong to a specific target, we have several powerful tools at our disposal.

The most straightforward method is using systemctl list-dependencies:

# Show all units under graphical.target
systemctl list-dependencies graphical.target

# Include inactive units and show as tree
systemctl list-dependencies --all graphical.target

# Show reverse dependencies (what requires this target)
systemctl list-dependencies --reverse graphical.target

For a more detailed analysis of target composition:

# Show the critical chain for a target
systemd-analyze critical-chain multi-user.target

# Generate a dependency graph (requires graphviz)
systemd-analyze dot multi-user.target | dot -Tsvg > target-dependencies.svg

To trace how units contribute to reaching a target in the journal:

# Show journal entries with unit startup timing
journalctl -b -u nginx.service --output=short-monotonic

# Filter for target achievement messages
journalctl -b _SYSTEMD_UNIT=targetname.target

Let's examine a common target and its components:

# List network target dependencies
systemctl list-dependencies --no-pager network.target

# Sample output:
network.target
● ├─systemd-networkd.service
● ├─network-online.target
● └─NetworkManager.service

For programmatic access to target information:

# Show all units wanted by a target
busctl call org.freedesktop.systemd1 /org/freedesktop/systemd1 \
org.freedesktop.systemd1.Manager ListUnitFilesByPatterns \
as 1 "target" as 1 "*.target"

Here's a bash script to generate a dependency report:

#!/bin/bash
TARGET=${1:-multi-user.target}

echo "Dependency analysis for $TARGET"
echo "=============================="
systemctl list-dependencies --all "$TARGET"

echo -e "\nUnit activation timeline:"
systemd-analyize plot | grep -A 5 "$TARGET"

When working with systemd, targets act as synchronization points that group multiple units (services, sockets, etc.) together. To inspect the composition of a specific target, we can use several powerful systemd commands.

The most straightforward approach is:

systemctl list-dependencies TARGET_NAME

For example, to see all units in the graphical target:

systemctl list-dependencies graphical.target

To find which targets include a specific unit (reverse lookup):

systemctl list-dependencies --reverse UNIT_NAME

For a service like NetworkManager:

systemctl list-dependencies --reverse NetworkManager.service

For comprehensive target analysis including startup timing:

systemd-analyze dot TARGET_NAME | dot -Tsvg > target.svg

This generates a visual dependency graph that shows:

  • All included units
  • Their dependency relationships
  • Startup sequence visualization

Let's examine a common server target:

systemctl list-dependencies multi-user.target --all

The --all flag shows inactive units too. For a filtered view of just services:

systemctl list-dependencies multi-user.target --type=service

To correlate targets with boot messages:

journalctl -b -u TARGET_NAME

For example, to see all messages related to basic.target:

journalctl -b -u basic.target

For developers needing programmatic access, parse the target's unit file:

systemctl cat TARGET_NAME

Or directly read the file:

cat /usr/lib/systemd/system/TARGET_NAME

For frequent analysis, create a bash function in your .bashrc:

function target-services() {
    systemctl list-dependencies $1 --type=service --no-pager | \
    grep -vE "●|targets|slices"
}

Usage:

target-services graphical.target