Pages

Saturday, April 2, 2016

How to Manage Systemd Services on Linux

How to Manage Systemd Services
on Linux



Composants systemd

Systemd is now used by default in most Linux distributions, from Fedora and Red Hat to Ubuntu, Debian, openSUSE, and Arch. The systemctl command allows you to get information about systemd's status and control running services.

Despite the controversy, this at least introduces some standardization across Linux distributions. The same commands will allow you to manage services in the same way on any Linux distribution using systemd.

Note: To modify your system configuration on Linux distribution like Ubuntu that uses sudo, you'll need to prefix the commands here with sudo. On other Linux distributions, you'll need to become the root user with the su command first.

What's Systemd ?

systemd is a system and service manager for Linux, compatible with SysV and LSB init scripts.
systemd

  • Provides aggressive parallelization capabilities
  • Uses socket and D-Bus activation for starting services
  • Offers on-demand starting of daemons
  • Implements transactional dependency-based service control logic
  • Tracks processes using Linux cgroups
  • Supports snapshotting and restoring
  • Maintains mount and automount points

  • Check If Your Linux System Is Using Systemd

    If you're not sure whether your Linux distribution is using systemd, open a Terminal window and run the following command. This shows you the version number of systemd on your Linux system, if it does have systemd installed:

    systemd --version
    OR

    systemctl --version

    Output :

    systemd 215 +PAM +AUDIT +SELINUX +IMA +SYSVINIT +LIBCRYPTSETUP +GCRYPT +ACL +XZ -SECCOMP -APPARMOR

    Systemctl

    systemctl is the main tool used to introspect and control the state of the "systemd" system and service manager. You can use systemctl for instance to enable/disable services permanently or only for the current session.See man systemctl for more details.

    Tip:
    • You can use all of the following systemctl commands with the
      -H user@host switch to control a systemd instance on a remote machine. This will use SSH to connect to the remote systemd instance.
    • systemadm is the official graphical frontend for systemctl. It is provided by systemd-ui from the official repositories or by systemd-ui-gitAUR from the AUR for the development version.
    • Plasma users can install systemd-kcm as a graphical fronted for systemctl. After installing the module will be added under System administration.

    Analyze the Boot Process

    The systemd-analyze command allows you to view information about your boot process, such as how long it took and which services (and other processes) added the most time to the boot process.

    To view information about the startup process in general, run this command:

    systemd-analyze

    To view how long each process took to start, run this command:

    systemd-analyze blame

    Analyzing the system state

    Show system status using:

    systemctl status

    Using units

    Systemd uses "units", which can be services (.service), mount points (.mount), devices (.device), or sockets (.socket). The same systemctl command manages all these types of units.

    List running units:

    systemctl
    or: systemctl list-units

    List failed units:

    systemctl --failed

    To view all available unit files on your system:

    The available unit files can be seen in /usr/lib/systemd/system/ and /etc/systemd/system/ (the latter takes precedence). List installed unit files with:

    systemctl list-unit-files

    Managing services with systemd

    To view a list of enabled and disabled services, you use the same systemctl command as above, but tell it to only list services:

    systemctl list-unit-files --type=service

    List all running services:

    systemctl

    Note:

    When using systemctl, you generally have to specify the complete name of the unit file, including its suffix, for example sshd.socket. There are however a few short forms when specifying the unit in the following systemctl commands:

    • If you do not specify the suffix, systemctl will assume .service. For example, netctl and netctl.service are equivalent.
    • Mount points will automatically be translated into the appropriate .mount unit. For example, specifying /home is equivalent to home.mount.
    • Similar to mount points, devices are automatically translated into the appropriate .device unit, therefore specifying /dev/sda2 is equivalent to dev-sda2.device.

    Activates the service named "example1" immediately:

    systemctl start example1

    Deactivates the service "example1" immediately:

    systemctl stop example1

    Restarts the service "example1" immediately:

    systemctl restart example1

    Shows status of the service "example1":

    systemctl status example1

    Enables "example1" to be started on bootup:

    systemctl enable example1

    Disables "example1" to not start during bootup:

    systemctl disable example1

    Power management using Systemd

    polkit is necessary for power management as an unprivileged user. If you are in a local systemd-logind user session and no other session is active, the following commands will work without root privileges. If not (for example, because another user is logged into a tty), systemd will automatically ask you for the root password.


    Shut down and reboot the system:

    systemctl reboot

    Shut down and power-off the system:

    systemctl poweroff

    Suspend the system:

    systemctl suspend

    Put the system into hibernation:

    systemctl hibernate

    Put the system into hybrid-sleep state (or suspend-to-both):

    systemctl hybrid-sleep