systemd

systemd

unit

  • Unit types

    • automount
    • device
    • mount
    • path
    • scope
    • service
    • slice
    • snapshot
    • socket
    • swap
    • target
    • timer

systemd - target

Get current targets

sc list-units --type=target
# Output
  UNIT                   LOAD   ACTIVE SUB    DESCRIPTION
  basic.target           loaded active active Basic System
  bluetooth.target       loaded active active Bluetooth Support
  cryptsetup.target      loaded active active Local Encrypted Volumes
  getty.target           loaded active active Login Prompts
  graphical.target       loaded active active Graphical Interface
  integritysetup.target  loaded active active Local Integrity Protected Volumes
  local-fs-pre.target    loaded active active Preparation for Local File Systems
  local-fs.target        loaded active active Local File Systems
  multi-user.target      loaded active active Multi-User System
  network-online.target  loaded active active Network is Online
  network-pre.target     loaded active active Preparation for Network
  network.target         loaded active active Network
  nfs-client.target      loaded active active NFS client services
  nss-lookup.target      loaded active active Host and Network Name Lookups
  nss-user-lookup.target loaded active active User and Group Name Lookups
  paths.target           loaded active active Path Units
  remote-fs-pre.target   loaded active active Preparation for Remote File Systems
  remote-fs.target       loaded active active Remote File Systems
  rpc_pipefs.target      loaded active active rpc_pipefs.target
  slices.target          loaded active active Slice Units
  sockets.target         loaded active active Socket Units
  sound.target           loaded active active Sound Card
  sshd-keygen.target     loaded active active sshd-keygen.target
  swap.target            loaded active active Swaps
  sysinit.target         loaded active active System Initialization
  timers.target          loaded active active Timer Units
  tpm2.target            loaded active active Trusted Platform Module
  veritysetup.target     loaded active active Local Verity Protected Volumes

systemd - service

[Service] - User

If you specify the user/group in the unit file then you should start the service as a system service.

If you want to start the service as a user service then the User/Group is not needed and can be removed from the unit config. You simply start the service when logged in as the current user passing the --user flag to systemctl.

systemd - service - oneshot

instantiated unit

unit generators

systemctl - cheatsheet

List active system units

sc

or

sc list-units

or

sc-list-units

List active user units

sc --user

or

sc list-units --user

or

sc-list-units --user

List active sockets

sc list-sockets

or

sc-list-sockets

List active services

sc list-units --type service

List all running units

sc --state running

Display unit status

sc status $unit

or

sc-status $unit

Display unit properties

sc show $unit

or

sc-show $unit

Display unit config

sc cat $unit

or

sc-cat $unit

Create snippet to override a part of unit file

sc edit $unit

or

sc-edit $unit

Reload changed unit files

sc daemon-reload

or

sc-daemon-reload

Reload changed configuration file

sc reload $unit ...

Revert unit file changes by deleting overriding snippet file

sc revert $unit

or

sc-revert $unit

Start/Stop a service

sudo sc start/stop ${service_name}.service

Enable/Disable a service from auto-start

sudo sc enable/disable ${service_name}.service

Start system in text mode (Fedora)

sc set-default multi-user.target

Start system in graphical mode (Fedora)

sc set-default graphical.target

List the directories where systemd looks for unit files

systemd-analyze --user unit-paths

List the actual directories where systemd loaded unit files from

sc --user show -p UnitPath --value | tr ' ' '\n' | sort

Configuration files priority

  1. /etc
  2. /run
  3. /usr/lib

Display an overview of overridden or modified unit files

systemd-delta --type=extended

Create a SVG file showing when every service started, highlighting the time taken on initialization

systemd-analyze plot > $svg_file.svg

sc - Prevent a service from running / Mask a service

sudo sc mask $service_name

journald

journalctl - cheatsheet

Display unit log
# Single unit
journalctl -u $unit
 
# Multiple units
journalctl -u $unit1 -u $unit2 ...
Display logs from the current boot
journalctl -b
List past boots
journalctl --list-boots
Display logs since yesterday
journalctl --since yesterday
Display logs of a specific process
journalctl _PID=$pid
Display logs of the current user
journalctl _UID=$(id -u)
Display kernel logs
journalctl -k
Display logs in JSON format
# Compact
journalctl -o json
 
# Pretty print
journalctl -o json-pretty

resolved

resolvectl - cheatsheet

DNS domain lookup
resolvectl query <domain>
DNS IP reverse lookup
resolvectl query <IP>
Show DNS configuration
resolvectl status
Show DNS servers
resolvectl dns

WSL

Resources

GUI