FIELD NOTES / UNIX & LINUXNew tools on weekdays. Beyond the man page.
GUEST SERIESPID 1: a week with systemdFrom the Service Manager's Desk · Part 2 of 5
$systemctl

systemctl: change the right layer of a unit

Understand runtime state, loaded configuration, and installation policy before choosing a command that changes any of them.

THE DEEP DIVE19 MIN READ
01

Begin with a report, not a restart

Systemctl is the command-line client for the systemd service manager. Use it to ask what the manager knows about a unit, then request a specific change. A unit is a managed resource, not necessarily a running process: services perform work, while targets group units. Monday's unit-graph introduction develops that model. The local refresher is enough for today: dependencies describe relationships between units; ordering describes which jobs wait for others; neither is the same as whether a unit is active now.

On a Linux host booted with systemd, start with this read-only question. The status command asks for a human-readable report. The operand multi-user.target names the standard non-graphical multi-user target. The --no-pager flag prints directly instead of opening an interactive page viewer. No administrator privilege is normally needed for this query, although access to some accompanying log data may be restricted.

Read the report as three separate questions. Loaded describes whether a definition can be loaded and commonly shows its path and installation state. Active describes current runtime state. An enabled installation policy is neither proof of current activity nor a guarantee of successful activation at boot. Conversely, inactive is not automatically failed. Status can cause a definition to be loaded into manager memory; “read-only” means no requested activation or configuration change, not absolutely no internal bookkeeping.

This installment teaches the upstream systemd 255 interface and uses Ubuntu's 255.4-1ubuntu8.17 binary for its file-based experiments, with Bash 5.2.21. The test container runs supervisord, not systemd, as process ID 1. All host-inspection recipes are source-verified, unexecuted examples with host-dependent results. Only the explicitly isolated alternate-root experiments below were executed. None of them starts a service or changes the host's boot policy.

TERMINALSHELL
systemctl --no-pager status multi-user.target

Expect a report containing Loaded and Active fields, not a fixed transcript. A host that has reached this target will normally show it as active, without a service process of its own. A connection failure in a non-systemd container is a client/manager problem, not evidence that this target failed. Status commonly exits 0 for an active unit, 3 for an inactive unit, or 4 for an unknown unit; the manual warns that this older service-status mapping is imperfect. Use explicit states rather than treating every nonzero result as the same diagnosis. See the systemctl 255 manual, status and Exit status sections.

02

Separate the manager's properties from the files on disk

The next question is not “what does the colored status line say?” but “which layer supplied that answer?” Show exposes named properties rather than a presentation intended for people. The --property option takes a comma-separated list of names. Here LoadState describes loading, ActiveState is high-level runtime state, and SubState is the more detailed unit-type-specific state. UnitFileState reports installation state. FragmentPath names the main source file, while DropInPaths lists supplemental configuration files. NeedDaemonReload indicates that the manager believes its loaded configuration needs reloading. The --all flag retains properties whose values are empty; without it, empty properties are suppressed.

The second command, cat, prints backing unit files and drop-ins with filenames. It is not a dump of the manager's already-parsed configuration. If files changed since the last manager reload, the current disk text and loaded understanding can disagree. That distinction, documented in systemctl's cat section, explains why looking at a new setting does not prove it has taken effect.

TERMINALSHELL
systemctl show --all --property=LoadState,ActiveState,SubState,UnitFileState,FragmentPath,DropInPaths,NeedDaemonReload multi-user.target
systemctl --no-pager cat multi-user.target

The first command produces property=value lines; their values and order are host-dependent. Empty DropInPaths means no paths were reported, not that the main file is missing. The second shows disk text, not a historical record of what a running service originally read. NeedDaemonReload is a useful signal, not an application-configuration freshness test. Both commands inspect; neither asks the application to reread its own configuration or restarts it. The systemctl show documentation and the systemd D-Bus unit interface describe these distinct properties.

03

Choose an inventory before interpreting an absence

List-units answers a memory question: which units does the manager currently hold? By default it displays active units, units with pending jobs, and failed units. With --all it includes loaded but inactive units too. In this command, --all broadens the unit listing; it still does not turn memory into a catalog of every installed file.

List-unit-files answers the installation question instead: which unit files are installed, and what is their enablement state? It can include templates, definitions that name a family of possible instances rather than a particular runnable instance. A template can appear here without appearing in list-units. The quoted pattern 'multi-user.*' narrows both examples. Single quotes prevent the shell from expanding the asterisk against filenames in the working directory; systemctl interprets it as a name-matching pattern. The two lists need not contain the same rows.

For one runtime predicate, is-active asks whether the named unit is active. It prints the state and returns 0 when its condition is satisfied; nonzero means it was not satisfied or the query failed. With several operands, success means at least one is active, not that all are. The --quiet flag suppresses the state text, which is convenient only when the caller intentionally checks the exit status. Keeping the text here makes unexpected states visible.

TERMINALSHELL
systemctl --no-pager list-units --all 'multi-user.*'
systemctl --no-pager list-unit-files 'multi-user.*'
systemctl is-active multi-user.target

Compare LOAD, ACTIVE, and SUB in the first list with STATE in the second; the second may also show a vendor preset, which is recommended default policy, not current runtime state. The final line reports runtime state only. These are host-dependent queries, not executed fixtures. An absent memory row does not prove a file is absent, and an enabled file does not prove the unit is active. These scopes and the “at least one” query contract are specified by systemctl 255.

04

Decide what should change before choosing a verb

Runtime requests normally need appropriate authorization to the system manager and a loadable, unmasked unit. Start asks for activation now; dependency jobs may be involved. It does not install boot-policy links. Starting an already active unit does not ordinarily rerun its work. Stop asks for deactivation now; it does not disable future activation, and a still-active socket, timer, or other trigger can activate the unit again. Stopping can interrupt work. It can also propagate through configured relationships, so checking one unit name is not a promise that only one resource will change.

Reload asks an active service that supports reloading to reread its application configuration. It does not reread systemd's unit definition. A service with no reload implementation cannot be made reloadable merely by choosing this verb. Restart requests stop followed by start, and it starts a unit that was not running. That can introduce downtime and is not a harmless substitute for an unsupported reload. It also does not necessarily flush every retained resource; the manual specifically notes that stored service file descriptors can survive a restart job. Where a deliberate fallback is appropriate, reload-or-restart reloads when supported and otherwise restarts; it also starts an inactive unit. Try-reload-or-restart preserves the inactive case instead. Choose that policy explicitly rather than hiding it in a routine maintenance command.

Daemon-reload changes the manager-configuration layer: it reruns generators, reloads unit files, and rebuilds the dependency tree. Generators are programs that translate other configuration into units or relationships. After editing a unit or drop-in on an authorized host, this is how the manager learns the new definition; it does not by itself restart the service or ask the application to reread its own files. Decide separately whether the changed settings require restarting work. An application configuration edit may instead call for the application's supported reload path, without any unit edit at all.

Enable and disable, examined next, change installation policy rather than current runtime state. On a live manager they also implicitly reload manager configuration, but they do not start or stop the affected units by default. The --now flag deliberately combines these layers: enable also starts, while disable or mask also stops, after successful installation-policy changes. Do not add it to the offline lab. There is no live-manager mutation recipe here: these are documented choices and consequences, not operations executed in this container. The systemctl 255 sections for these verbs define their preconditions and effects.

05

Read the installation contract before writing it

To see policy changes directly, use a private filesystem tree rather than the host manager. The following two definitions belong to that experiment only. The filename comments beginning with # are display labels, not a request to combine them into one file. A bracketed section groups key=value settings. In [Unit], Description supplies human-readable text. In [Service], Type=oneshot means the manager waits for the startup program to finish. ExecStart identifies that program; /usr/bin/true simply succeeds. RemainAfterExit=yes would let the unit remain active after successful completion even though it has no remaining process. This is another reason active cannot universally mean “a daemon is running.” Nothing is activated in this experiment.

The [Install] section supplies instructions for installation tools. WantedBy=clitdj-demo.target tells enable to create a link in that target's .wants directory. When the target is activated, such a link contributes a Wants dependency on the service. This is the installation counterpart to Monday's graph: an edge added to the target, not a command to start work immediately. The manager does not consult [Install] directly during ordinary activation. The target definition needs no [Service] section because it groups units rather than launches a program. The systemd.unit Install section and systemd.service Type and RemainAfterExit sections document this contract.

TERMINALUNIT DEFINITIONS
# clitdj-demo.service
[Unit]
Description=CLI demo work
[Service]
Type=oneshot
ExecStart=/usr/bin/true
RemainAfterExit=yes
[Install]
WantedBy=clitdj-demo.target

# clitdj-demo.target
[Unit]
Description=CLI demo group

These are readable definitions, not shell commands. No file has been created yet. The next block creates two separate files containing these settings, without the display comments or separating blank line. We will not boot this tree, install it on the host, or run the service. The target is deliberately not wired into the real machine's default boot target.

06

Build a filesystem that cannot be mistaken for the host

Run the remaining fixture blocks in order in one disposable Bash session, using Bash's default error-handling options. Later blocks intentionally inspect nonzero query results instead of automatically exiting on them. This setup creates a new temporary directory and writes only inside it. Mktemp with -d creates a private directory; the six X characters in the /tmp/clitdj-systemctl.XXXXXX template are replaced to produce a unique name. The assignment stores its pathname in clitdj_root. The shell's $(...) captures the command's output. The && operators run the next step only if the preceding one succeeded; do not split the guarded chain into independently run lines.

Mkdir creates directories, and -p creates missing parents. Double quotes keep an expanded pathname together. Umask 022 prevents newly created regular files from granting write access to group or others. Printf uses the fixed format %s\n to print each following string and a newline; single quotes keep the text literal for the shell, and > writes it to the named file. The file settings below exactly reproduce the previous definitions. These writes are confined to the new root, not /usr/lib or /etc on the host.

The lab deliberately uses /usr/bin/systemctl, the installed native executable. Some containers put a different systemctl wrapper earlier in the command search path; the explicit pathname avoids that ambiguity in the tested Ubuntu environment. Its --root option takes an alternate filesystem root and, for the supported unit-file commands used here, operates directly on files instead of contacting a running manager. This is not a universal “safe mode”: it does not provide a simulated runtime for start or status. No executable needs to exist inside this tree because nothing will run there. The systemctl 255 --root section documents this offline operation.

TERMINALSHELL
clitdj_root=$(mktemp -d /tmp/clitdj-systemctl.XXXXXX) &&
mkdir -p "$clitdj_root/usr/lib/systemd/system" "$clitdj_root/etc/systemd/system" &&
umask 022 &&
printf '%s\n' '[Unit]' 'Description=CLI demo work' '[Service]' 'Type=oneshot' 'ExecStart=/usr/bin/true' 'RemainAfterExit=yes' '[Install]' 'WantedBy=clitdj-demo.target' > "$clitdj_root/usr/lib/systemd/system/clitdj-demo.service" &&
printf '%s\n' '[Unit]' 'Description=CLI demo group' > "$clitdj_root/usr/lib/systemd/system/clitdj-demo.target"

Successful setup is silent. The fresh tree now has two vendor-style definitions under usr/lib/systemd/system and an empty local configuration directory under etc/systemd/system. It has no enablement or mask links. No current runtime state exists to query. Leave the temporary tree in place until the walkthrough is complete; the subsequent blocks reuse clitdj_root. Cleanup is intentionally not an automatic recursive-delete command: remove only the exact temporary tree allocated for this run after inspecting its pathname, never a host configuration directory.

07

Observe the edge that enable creates

Now enable the fixture service. This writes an installation symlink beneath the alternate root; it does not launch /usr/bin/true. The --quiet flag suppresses systemctl's informational filesystem-operation messages, not errors. Is-enabled then reports installation state. Readlink prints the stored destination of a symbolic link without following it, letting us inspect the mechanism behind the label. A symbolic link is a filesystem entry that refers to another pathname rather than containing the unit's settings.

Each later block starts with a shell guard. The colon is Bash's no-op command; the ${clitdj_root:?...} expansion requires clitdj_root to be set and nonempty, otherwise the shell reports the supplied message and aborts this block before systemctl runs. The following && preserves the dependency on that check. Keep using the variable from the successful setup, not a hand-entered host pathname.

TERMINALSHELL
: "${clitdj_root:?Run the fixture setup first}" &&
/usr/bin/systemctl --root="$clitdj_root" --quiet enable clitdj-demo.service &&
/usr/bin/systemctl --root="$clitdj_root" is-enabled clitdj-demo.service &&
readlink "$clitdj_root/etc/systemd/system/clitdj-demo.target.wants/clitdj-demo.service"
TERMINALOBSERVED FIXTURE STDOUT
enabled
/usr/lib/systemd/system/clitdj-demo.service

The destination is a pathname within the alternate-root installation model. An ordinary host tool following an absolute link does not automatically reinterpret it relative to clitdj_root; readlink only prints its text. WantedBy supplied the proposed edge; enable created the link. This proves installation policy in the fixture, not successful boot, service execution, or host enablement. On a live system, a target must itself be reached for this particular edge to matter. Enablement is broader than the shorthand “run at boot”: links can connect units to other activation paths too.

08

Disable the policy, then read the query's contract

Disable removes symlinks to the specified unit's backing file from the configuration directory. It leaves the vendor definition in place. On a live system it would not stop an already active service without --now. In this fixture the change removes the target's Wants link, so is-enabled reports disabled.

The query's nonzero exit is expected here, so it must not accidentally terminate the example before we inspect it. The brace group collects commands after the successful guard and disable. Within the group, newlines separate commands without requiring each to succeed. The assignment immediately saves $?—the shell's exit status of the preceding command—before another command overwrites it. Printf then displays that saved number. The closing brace ends the group. This deliberately handles an expected negative predicate; it does not discard an unexplained setup failure.

The second query asks about the target, which has no installation instructions. Its static result has exit status 0 in systemd 255 even though the target was not enabled by an installation link. This documented contract is why “if is-enabled succeeds, it must start at boot” is not sound. Query one unit at a time when the exact state matters, and read the state string; other successful states include alias, generated, and indirect.

TERMINALSHELL
: "${clitdj_root:?Run the fixture setup first}" &&
/usr/bin/systemctl --root="$clitdj_root" --quiet disable clitdj-demo.service &&
{
  /usr/bin/systemctl --root="$clitdj_root" is-enabled clitdj-demo.service
  clitdj_status=$?
  printf 'query status=%s\n' "$clitdj_status"
  /usr/bin/systemctl --root="$clitdj_root" is-enabled clitdj-demo.target
  clitdj_status=$?
  printf 'target query status=%s\n' "$clitdj_status"
}
TERMINALOBSERVED FIXTURE STDOUT
disabled
query status=1
static
target query status=0

The exact status 1 is the observed systemd 255 result for this disabled fixture; the documented general contract is nonzero. It is not the exit status of the service program, which never ran. Disabled units can still be started manually or pulled in by another dependency. Static units can also be activated; lacking enablement instructions is not an activation prohibition. See the is-enabled state table in systemctl 255 rather than borrowing the numeric status mapping from status.

09

Use a mask only when activation must be blocked

The service is now disabled, but its definition remains loadable. Mask is stronger: it places a same-name link to /dev/null in a higher-priority unit directory. The load result is masked, and activation is prohibited, including manual starts. A mask does not stop already running work by default. The --runtime flag would put a temporary mask under /run instead of persistent /etc; we omit it, so the fixture's mask is persistent installation policy within this disposable tree. Unmask removes the mask; it does not enable or start the unit.

This is why the fixture's main definition lives in the vendor-style directory. Without an overriding option, mask fails if a same-name file already occupies its destination under /etc/systemd/system or /run/systemd/system. Do not delete an existing local definition to make masking “work,” and do not reach for force before understanding what would be replaced. Here the destination is free. The block writes a mask, inspects it, attempts an intentionally rejected enable, records that command's result, and removes the mask. This is all alternate-root file work.

TERMINALSHELL
: "${clitdj_root:?Run the fixture setup first}" &&
/usr/bin/systemctl --root="$clitdj_root" --quiet mask clitdj-demo.service &&
{
  readlink "$clitdj_root/etc/systemd/system/clitdj-demo.service"
  /usr/bin/systemctl --root="$clitdj_root" is-enabled clitdj-demo.service
  clitdj_status=$?
  printf 'masked query status=%s\n' "$clitdj_status"
  /usr/bin/systemctl --root="$clitdj_root" --quiet enable clitdj-demo.service
  clitdj_status=$?
  printf 'enable status=%s\n' "$clitdj_status"
  /usr/bin/systemctl --root="$clitdj_root" --quiet unmask clitdj-demo.service &&
  {
    /usr/bin/systemctl --root="$clitdj_root" is-enabled clitdj-demo.service
    clitdj_status=$?
    printf 'unmasked query status=%s\n' "$clitdj_status"
  }
}
TERMINALOBSERVED FIXTURE STDOUT
/dev/null
masked
masked query status=1
enable status=1
disabled
unmasked query status=1

This block also emits an error on standard error explaining that enable cannot operate on the masked unit; its displayed path includes the run-specific temporary root. The output above shows standard output only. The attempted enable does not recreate the Wants link. After unmask, the vendor file is visible again, and the state returns to disabled, not enabled. The general documented contract is nonzero for masked and failed enable, not a promise that every error everywhere is status 1. The mask and unmask sections of systemctl 255 describe these effects and destination restrictions.

10

Explain an unexpected link before removing it

During an authorized configuration review, “enabled but no one recalls enabling it” is a finding to explain, not proof of a vulnerability or intrusion. Installation state describes the filesystem, not who created it. To demonstrate that limit, return to the disabled, unmasked fixture and create the same Wants link manually. Ln creates a link; -s selects a symbolic link. Its first operand is the destination text and its second is the new link pathname. Mkdir -p ensures the .wants directory exists. These changes remain inside the disposable root.

Is-enabled now recognizes the policy without knowing whether enable created it. Inspect the exact link and compare it with an approved baseline, package ownership, and change records before altering a real snapshot. If activation is unexpectedly blocked, distinguish a same-name /dev/null mask from a Wants link instead of trying to cure both with enable. If the main unit or its parent/drop-in directories are writable by unintended identities, assess that separate configuration trust boundary described on Monday. None of these observations alone establishes exploitation or a defect in systemd.

Finally, disable removes this manually created link too. It is not a transaction rollback that deletes only what the last enable call wrote. On an actual host that scope may remove intentional custom links, so capture their paths and destinations before repair. The systemctl disable documentation explicitly includes manually created links. Do not execute the fixture's repair against a real machine merely because its result looks familiar.

TERMINALSHELL
: "${clitdj_root:?Run the fixture setup first}" &&
mkdir -p "$clitdj_root/etc/systemd/system/clitdj-demo.target.wants" &&
ln -s /usr/lib/systemd/system/clitdj-demo.service "$clitdj_root/etc/systemd/system/clitdj-demo.target.wants/clitdj-demo.service" &&
/usr/bin/systemctl --root="$clitdj_root" is-enabled clitdj-demo.service &&
readlink "$clitdj_root/etc/systemd/system/clitdj-demo.target.wants/clitdj-demo.service" &&
/usr/bin/systemctl --root="$clitdj_root" --quiet disable clitdj-demo.service &&
{
  /usr/bin/systemctl --root="$clitdj_root" is-enabled clitdj-demo.service
  clitdj_status=$?
  printf 'after repair status=%s\n' "$clitdj_status"
}
TERMINALOBSERVED FIXTURE STDOUT
enabled
/usr/lib/systemd/system/clitdj-demo.service
disabled
after repair status=1

The fixture ends with both original definitions intact, no enablement link, and no mask. The exercise distinguishes vendor-provided installation instructions, an enable-created link, a manually created link, and a mask link. Their provenance and purposes differ even when some produce the same is-enabled label. This file-only experiment makes no claim about current processes, events on a real host, or a security incident.

11

Preserve failure evidence before resetting the record

Back on an authorized systemd host, a failed unit needs diagnosis before tidying. For this recipe, the input is a service named clitdj-demo.service that the host administrator has already installed or identified; the alternate-root fixture was never installed there. If no such service exists, do not run the recipe expecting fixture state to appear. Use the actual approved unit name. The query is read-only and has no fixed output in this article.

Show can separate LoadState, ActiveState, and SubState from Result, the service's overall execution result. ExecMainCode describes how its main process terminated; ExecMainStatus is interpreted in that context as an exit code or signal number. These may describe a current or previous invocation, not necessarily a process still running. A failed result can reflect a timeout or resource problem as well as a nonzero program exit. A client connection, authorization, missing-unit, or job failure is not interchangeable with the program's exit status. The systemd D-Bus service interface documents these fields; status adds a human-readable summary and recent journal context when available.

TERMINALSHELL
systemctl show --all --property=LoadState,ActiveState,SubState,Result,ExecMainCode,ExecMainStatus clitdj-demo.service
systemctl --no-pager status clitdj-demo.service

Save the observed state and relevant logs before making changes. Reset-failed clears the selected unit's failed state and recorded failure information; it also resets its start-rate-limit counter and, for services, restart counter. Omitting a unit name applies the reset to all units, so a targeted reset is the deliberate choice. It does not fix the cause or start the unit. Likewise, a successful stop request is not proof that the configured stop helper succeeded: systemctl's stop contract allows forced termination despite a helper failure. Diagnose the layer that failed, then choose the change. Ask what is active now, what definition the manager has loaded, and what installation policy will trigger later work—three answers, not one green light.

Back to latest issue
HELP SHAPE THE NEXT ISSUE

What belongs in your toolbox?

Choose a command you want to know better. We start with familiar tools and work toward the less obvious ones.

READER’S CHOICE

What should we cover next?

Vote for a future deep dive, or suggest a command we missed.

One command name, up to 32 characters. Suggestions are reviewed by a person. A new article starts a fresh poll.

Loading poll…