Booting and managing services with systemd (Should know)

Systemd provides us with a more modern approach of booting. It also benefits from the modern multicore processors and relies on very aggressive parallelization to get the job done very quickly. Arch Linux provides systemd by default. Systemd uses "the so-called service files" to define how and when a certain service or "a so-called daemon" has to be started.

Getting ready

The following list describes the main tasks that we will perform in this recipe:

  • Installing systemd
  • Setting the default target: The default target can be compared with the runlevel when using initscripts. It is not the same, but flows from the same idea that you want some stuff being started in certain cases. "A so-called runlevel" is called a target in the systemd world.
  • Starting a service manually: Using the systemctl command, we can start and stop services on demand.
  • Enabling services during boot time: Of course systemd can start services during boot time. It is very simple to enable or disable these services.

How to do it...

Let's list the steps required to install systemd:

  1. Install both systemd and systemd-arch-units to make the installation work by running pacman -S systemd systemd-arch-units.
  2. Add init=/bin/systemd to your kernel command line by editing /boot/syslinux/syslinux.cfg:
    APPEND initrd=/initramfs-linux.img root=/dev/sda2rootfstype=ext4 ro init=/bin/systemd
    

Let's list the steps required to set the default target:

  1. If you want to end up with a graphical system, enable the graphical target:
    systemctl enable graphical.target
    
  2. When a terminal is sufficient, the multiuser target will suffice:
    systemctl enable multi-user.target
    
  3. Start a service manually by running the following command:
    systemctl start service
    
  4. Enable services during boot time by running the following command:
    systemctl enable service
    

    Note

    For a detailed description of the main tasks performed, refer to the Getting ready section of this recipe.

How it works...

We need the systemd-arch-units package, because not all packages providing services are providing the service files needed to be able to use them with systemd. Due to the fact that we added init=/bin/systemd to the kernel command line, the system will use systemd for startup.

These days only two targets can be set automatically as the default target, as only graphical.target and multi-user.target provide the default target installation.

In the systemctl start service command, service is actually the name of a file. For example, NetworkManager has a systemd service file called NetworkManager.service, and it is this full name we need to pass to the systemctl command. For example:

systemctl start NetworkManager.service

To be able to know what services to start during boot time, systemctl will create symlinks to the specific service files in the correct locations where systemd will search them during boot.

There's more...

Once we are satisfied with systemd booting our system, we can eventually fine-tune it a little more.

Systemd only initialization

The following command will provide symlinks for initscripts' compatibility:

pacman -S systemd-sysvcompat

The result is that you can omit the extra parameter init=/bin/systemd on your kernel command line.

We can easily change the desired target by changing the kernel command line in our boot loader configuration. This makes it easy to test if some specific target suits our needs.

Set the target on the kernel command line

Similar to the appending of a number to the kernel command line when using initscripts, we can also do this for systemd using the systemd.unit parameter.

For example, considering Syslinux as the boot loader, open /boot/syslinux/syslinux.cfg and add systemd.unit=multi-user.target to the kernel command line:

APPEND initrd=/initramfs-linux.img root=/dev/sda2 rootfstype=ext4ro systemd.unit=multi-user.target

The previous example is valid for a system with only systemd, otherwise we also would require init=/bin/systemd.

List all available services

We might want to know what services we have available on our system, and of course we want to know what we can do with those services.

We can list all the used services or choose the list all the available services by running the following commands:

systemctl list-units --type=service
systemctl list-units -a --type=service

Default actions for services

By default systemd supports actions such as start, stop, restart, reload, and status. There are more actions available, which can be found by issuing man systemctl.

Check if a service will be started at boot

Before enabling a service, we might want to check if the service is not already enabled. We can check if a service is already enabled for startup during the boot process by using the is-enabled action:

systemctl is-enabled service

Disable a service from starting during boot

If we find some service we no longer need, we would want to disable it. Sometimes we no longer want some services being started during the boot process, so we need to disable them:

systemctl disable service