Managing Services

Topic Progress:

5.4. Managing Services

Kali uses systemd as its init system, which is not only responsible for the boot sequence, but also permanently acts as a full featured service manager, starting and monitoring services.

systemd can be queried and controlled with systemctl. Without any argument, it runs the systemctl list-units command, which outputs a list of the active units. If you run systemctl status, the output shows a hierarchical overview of the running services. Comparing both outputs, you immediately see that there are multiple kinds of units and that services are only one among them.

Each service is represented by a service unit, which is described by a service file usually shipped in /lib/systemd/system/ (or /run/systemd/system/, or /etc/systemd/system/; they are listed by increasing order of importance, and the last one wins). Each is possibly modified by other service-name.service.d/*.conf files in the same set of directories. Those unit files are plain text files whose format is inspired by the well-known "*.ini" files of Microsoft Windows, with key = value pairs grouped between [section] headers. Here we see a sample service file for /lib/systemd/system/ssh.service:

Description=OpenBSD Secure Shell server auditd.service

ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/bin/kill -HUP $MAINPID


Target units are another part of systemd's design. They represent a desired state that you want to attain in terms of activated units (which means a running service in the case of service units). They exist mainly as a way to group dependencies on other units. When the system starts, it enables the units required to reach the (which is a symlink to, and which in turn depends on So all the dependencies of those targets get activated during boot.

Such dependencies are expressed with the Wants directive on the target unit. But you don't have to edit the target unit to add new dependencies, you can also create a symlink pointing to the dependent unit in the /etc/systemd/system/ directory. And this is exactly what systemctl enable foo.service does. When you enable a service, you tell systemd to add a dependency on the targets listed in the WantedBy entry of the [Install] section of the service unit file. Conversely, systemctl disable foo.service drops the same symlink and thus the dependency.

The enable and disable commands do not change anything regarding the current status of the services. They only influence what will happen at next boot. If you want to run the service immediately, you should execute systemctl start foo.service. Conversely, you can stop it with systemctl stop foo.service. You can also inspect the current status of a service with systemctl status foo.service, which usefully includes the latest lines of the associated log. After having changed the configuration of a service, you may wish to reload it or restart it: those operations are done with systemctl reload foo.service and systemctl restart foo.service respectively.

# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
# ls -al /etc/systemd/system/
ls: cannot access '/etc/systemd/system/': No such file or directory
# systemctl enable postgresql
# ls -al /etc/systemd/system/
lrwxrwxrwx 1 root root 38 Jan 26 19:09 /etc/systemd/system/ -> /lib/systemd/system/postgresql.service
# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
# systemctl start postgresql
# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
     Active: active (exited) since Tue 2021-01-26 19:10:31 EST; 5s ago
    Process: 1495 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 1495 (code=exited, status=0/SUCCESS)

Jan 26 19:10:31 kali-rolling systemd[1]: Starting PostgreSQL RDBMS...
Jan 26 19:10:31 kali-rolling systemd[1]: Started PostgreSQL RDBMS.