Systemd - kamialie/knowledge_corner GitHub Wiki
unit
is a basic object that systemd manages and acts upon. Unit is defined
using unit file. Similar to services or jobs in other init
systems. Can be used to abstract services, network resources, devices,
filesystem mounts and so on.
Most common type of unit is service (indicated by a unit file name with
.service
suffix).
Static unit doesn't contain install
section, thus, can not be enabled -
usually such units perform one-off action or act as a dependency for other
unit.
File locations(listed in low to high priority order):
Location | Description |
---|---|
/lib/systemd/system | Generic, vanilla, unit file. System's copy of unit files, as well as default location for units files installed by software. Shouldn't be edited, but rather overridden, if necessary, using another location which supersedes this location. |
/run/systemd/system | Run-time unit definition reside here. Can be used to apply changes to be valid for a session. Also used by systemd itself for dynamically created unit files. All changes are lost upon reboot. |
/etc/systemd/system | Modifications of the whole unit file should be placed here. Best place to modify the system's copy of a unit file. |
/etc/systemd/system/..d | Unit file snippets, which are used to Override or append only specific directive(s). Within this directory a file named override.conf is created. |
List of unit files on the system:
$ systemctl list-unit-files
See full content of a unit file;
$ systemctl cat foo.service
edit
command, by default, opens a unit file snippet, which would be a blank
file that can be used to override or add directives to the unit definition. A
directory at /etc/systemd/system
with a service name will be created, e.g.
foo.service.d
with a snippet override.conf
file. --full
flag load the
whole unit file definition; changes will be stored at /etc/systemd/system
.
# Add unit file snippet
$ systemctl edit foo.service
# Modify entire content
$ systemctl edit --full foo.service
# Reload the process to pick up changes
$ systemctl daemon-reload
Show low level details of the unit's settings (outputs key-value pairs):
$ systemctl show nginx.service
# Show individual key value
$ systemctl show nginx.service -p Before
View dependency tree - which units were activated when stating the unit (the
unit that are specified as Wanted
or Required
). --reverse
would show
units that depend on specified unit. Additional --before
and --after
options narrow the list to those that depend before or after.
$ systemctl list-dependencies foo.service
# Expand all dependencies recursively
$ systemctl list-dependencies --all foo.service
# --reverse - show units that depend on unit
# --before, --after - depend before or after
Unit can be marked as unstartable (masked). Corresponding command creates a
symlink to /dev/null
:
$ systemctl [mask|unmask] foo.service
When operating on services, *.service
suffix can be omitted - systemd
identifies service management commands and targets *.service
files
automatically. Commands affecting OS, require sudo privileges.
# Basic system commands
# Use reload if application can reload configuration without restarting
$ systemctl [start|stop|restart|reload] foo.service
# Use the following if unsure if app has reload functionality
$ systemctl reload-or-restart foo.service
Start services automatically at boot. Enabling creates a symbolic link at the location where systemd looks for autostart files.
$ systemctl [enable|disable] foo.service
View current state of a unit. Shortcut commands return also return exit code 0
or 1 that can be used with scripts. is-failed
return active
(properly
running), failed
(error occurred) or unknown
/inactive
(intentionally
stopped); 0 exit code indicates failure, 1 - other states.
# Current state of a unit
$ systemctl status foo.service
# Shortcuts
$ systemctl is-active foo.service
$ systemctl is-enabled foo.service
$ systemctl is-failed foo.service
Some major states have shortcuts (rescue is single user mode, would be same
as systemctl isolate rescue.target
):
$ sudo systemctl [poweroff|reboot|rescue|halt]
View all unit files that are listed as "active"; list-units
can be dropped,
as it is the default behavior of systemctl
. --all
lists all units that
systemd attempted to load regardless of current state. Use --state=
,
--type=
to narrow down the list.
$ systemctl list-units
$ systemctl list-units --all
$ systemctl list-units --all --state=inactive
$ systemctl list-units --type=service
Targets, previously known as runlevels, describe a system state and act as synchronization points that the server can use to bring itself to a specific state. Unlike runlevels multiple targets can be active at the same time.
# View all available targets on the system
$ systemctl list-unit-files --type=target
# View all active targets
$ systemctl list-units --type=target
# View default target that systemd tries to reach
$ systemctl get-default
# Set new default
$ systemctl set-default multi-user.target
Stop all units that are not tied to the specified target. Make sure no essential services would be stopped before running.
$ systemctl list-dependencies multi-user.target
$ systemctl isolate multi-user.target
The system that collects and manages all kernel and userland processes' logs.
Implemented as journald
daemon. Can be used together with existing syslog
implementation (centralized server with local journal for multiple services),
or can completely replace it.
By default entries are shown in local time (make sure correct time zone is set
via timedatectl
utility); can also be viewed in UTC.
# Display entries with UTC timestamps
$ journalctl --utc
# List only kernel messages (typically represented by dmesg
$ journalctl -k
# List journal entries for the unit
$ journalctl -u foo.service
By default shows all log entries with a pager (usually less
) starting from
oldest including current and previous boots, if journald
is configured to
save previous boot records.
To enable storing previous boot log entries set Storage=
option under
[Journal]
section to persistent
in /etc/systemd/journald.conf
file, or
create persistent directory:
$ sudo mkdir -p /var/log/journal
Boot listing: first column is an offset, second is boot ID. Both can be
referenced in other journalctl
commands.
# List entries from current boot
$ journalctl -b
# List boots that journald is aware of
$ journalctl --list-boots
# List entries on previous boot
$ journalctl -b -1
Arbitrary time limits can be specified via --since
and --until
options.
YYYY-MM-DD HH:MM:SS
format is used to specify absolute time values. Some
defaults are applied, if parts of the format are left off: current date is
assumed for date, midnight time for time, 00
seconds for seconds field.
Relative values such as yesterday
, today
, tomorrow
, and now
. Also
prepending -
or +
to a numbered value or using ago
in sentence
construction is possible.
$ journalctl --since "2015-01-10" --until "2015-01-11 03:00"
$ journalctl --since yesterday
$ journalctl --since 09:00 --until "1 hour ago"
# Display system info, status may be ommited
$ timedatectl status
# List time zones
$ timedatectl list-timezones
# Set time zone
$ sudo timedatectl set-timezone <zone>