Skip to content

This document is a WORK IN PROGRESS.
This is just a quick personal cheat sheet: treat its contents with caution!


SystemD

SystemD is a suite of basic building blocks for a Linux system. It provides a system and service manager that runs as PID 1 and starts the rest of the system.

Reference(s)

Table of contents

TODO


Services

Basic services control

afficher directement à la fin des logs

$ journalctl -xeu service-name

afficher les derniers logs en direct

$ journalctl -xefu service-name

editer le contenu d(un service systemd existant (très important, pour éviter d'écrire par dessus les services gérés par le système avec les )

$ systemctl edit service-name

afficher le contenu d(un service systemd (incluant aussi les modifications faites via edit, qui se retrouveront dans le dossier override.conf)

$ systemctl cat service-name

afficher les valeurs de toutes les variables associées à un service :

$ systemctl show service-name

afficher les variables d(environnement associées à un service :

$ systemctl show-environment service-name

après avoir créé ou modifié un service, il faut penser à lancer :

$ systemctl daemon-reload

affiche tous les emplacements de documentation pour trouver différentes informations

$ man 7 systemd.directives

affiche les dépendances d'un service

$ systemctl list-dependencies service-name

affiche les dépendances inverses d'un service

$ systemctl list-dependencies --reverse service-name

affiche les services qui ont pris le plus de temps à s'activer

systemd-analyze blame

affiche la chaine critique de démarrage du système, et mets en évidence qui est resté le plus longtemps sur le chemin critique de démarrage:

systemd-analyze critical-chain

créé un fichier .svg qui affiche graphiquement comment sont séquencés les services au démarrage et en combien de temps:

systemd-analyze plot

créé un fichier .svg qui affiche graphiquement les dépendences et reverses dépendances d'un service:

systemd-analyze dot "service-name.service" | dot  -Tx11

  • Start the service_name service:

    # systemctl start service_name
    

  • Stop the service_name service:

    # systemctl stop service_name
    

  • Restart the service_name service:

    # systemctl restart service_name
    

  • Reload service_name configuration without stopping the service_name service:

    # systemctl reload service_name
    

  • Check the service_name service status:

    # systemctl status service_name
    

  • Enable the service_name service on system boot:

    # systemctl enable service_name
    

  • Disable the service_name service on system boot:

    # systemctl disable service_name
    

  • Check if the service_name service is enable or disable on system boot:

    # systemctl is-enabled service_name
    

Inspect services

  • Check if any SystemD services have entered in a failed state:

    $ systemctl --failed
    

  • List SystemD services that have failed:

    $ systemctl list-units --state=failed
    

Override services

Let's you want the ntpd service to automatically restart if a failure happens. You could "manually" edit the associated .service file (/usr/lib/systemd/system/ntpd.service), but this file could be overritten after a system update or package install. This is where the systemctl edit command is interresting, it will create an associated override.conf file that will always apply:

  • Edit the override file (e.g. of ntpd.service, located in /etc/systemd/system/ntpd.service.d/override.conf and not in /usr/lib/systemd/system/ntpd.service) :

    $ EDITOR=vi systemctl edit ntpd.service
      + > [Service]
      + > Restart=on-failure
    

  • Check the syntax e.g. of /usr/lib/systemd/system/ntpd.service and /etc/systemd/system/ntpd.service.d/override.conf:

    $ systemd-analyze verify ntpd.service
    

  • Print the full ntpd service (ntpd.service and ntpd.servce.d/override.conf) :

    $ systemctl cat ntpd.service
        > # /usr/lib/systemd/system/ntpd.service
        > [Unit]
        > Description=Network Time Service
        > After=syslog.target ntpdate.service sntp.service
        >
        > [Service]
        > Type=forking
        > EnvironmentFile=-/etc/sysconfig/ntpd
        > ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS
        > PrivateTmp=true
        >
        > [Install]
        > WantedBy=multi-user.target
        >
        > # /etc/systemd/system/ntpd.service.d/override.conf
        > [Service]
        > Restart=on-failure
    

Analyze services

Reference(s)
  • Create an SVG graphic detailing which system services have been started at what time, highlighting the time they spent on initialization:
    $ systemd-analyze plot > systemd_analyze_plot.svg
    

Run levels

TODO


Tips and tricks

  • Start system in a non-graphical mode:

    $ systemctl get-default
        > graphical.target
    
    $ sudo systemctl set-default multi-user.target
    

  • Check systemd journal for a service:

    $ sudo journalctl -xeu service-name
    

    -x, --catalog: Augment log lines with explanation texts from the message catalog. This will add explanatory help texts to log messages in the output where this is available. These short help texts will explain the context of an error or log event, possible solutions, as well as pointers to support forums, developer documentation, and any other relevant manuals.

    -e, --pager-end Immediately jump to the end of the journal inside the implied pager tool.

    -u, --unit=UNIT|PATTERN Show messages for the specified systemd unit UNIT (such as a service unit), or for any of the units matched by PATTERN.

  • Check systemd journal for every services:

    $ sudo journalctl -xb -p 3
    

    -x, --catalog Augment log lines with explanation texts from the message catalog. This will add explanatory help texts to log messages in the output where this is available.

    -b [[ID][±offset]|all], --boot[=[ID][±offset]|all] Show messages from a specific boot. This will add a match for "_BOOT_ID=". The argument may be empty, in which case logs for the current boot will be shown.

    -p 3, --priority= Filter output by message priorities or priority ranges. Takes either a single numeric or textual log level (i.e. between 0 "emerg" and 7 "debug").

    The log levels are the usual syslog log levels as documented in syslog, i.e.:
    - "emerg" (0)
    - "alert" (1)
    - "crit" (2)
    - "err" (3),
    - "warning" (4)
    - "notice" (5)
    - "info" (6)
    - "debug" (7)
    
    If a single log level is specified, all messages with this log level or a lower (hence more
    important) log level are shown. If a range is specified, all messages within the range are
    shown.
    

If this cheat sheet has been useful to you, then please consider leaving a star here.