The systemd process replaces the SysV init. It runs as the first process after the kernel boot and is responsible for bringing the Linux host up to the state where it can be used. It is responsible for starting and managing the services, mounting filesystems, managing hardware, generating the login prompt, and much more. A key benefit over SysV is that systemd starts as many services as possible in parallel, thus speeding up the startup process, and that brings up the login screen faster.
Units
The items that are managed by the systemd are called units. The unit files are located in /lib/systemd/system.
Service Units
For service management, the target units are service units, which have unit files with a suffix of .service.
Managing systemd services
The command to manage systemd units is systemctl.
Starting and Stopping Services
To start a systemd service, use the systemctl start command: You can leave .service suffix. For example, to start the apache server on Ubuntu: To stop a running service: $ sudo systemctl stop name.service So, to stop the apache server on Ubuntu:
Restarting and Reloading services
To restart a running service, use restart command: And where the only reloading configuration file is required
Enabling and Disabling Services
If you want a service to start automatically at system boot, use enable command: To disable a service from starting at system boot: Disabling does not stop a running service.
Viewing service status
To view information about a service: This will show you the status of the service and the first few lines of the log file. So, while the service is running is, the output of is To check if a service is active: $ sudo systemctl is-active name.service So, while the apache2 service is running, the output of systemctl is-active command is : To check if a service is enabled: $ sudo systemctl is-enabled name.service.
Viewing System State
All commands you have seen till now are used to manage a single service. When you want an overview of the system state, use the following set of commands: To see all unit types To list all installed units, use list-unit-files The output has only two columns Unit File and State. The state will usually be enabled, disabled, static or masked.
Static: This means the unit cannot be enabled, performs a one-off action, or is a dependency of another unit and cannot be run by itself. Masked: A unit listed as masked means it is completely unstartable, as it is linked to /dev/null. This is called masking the unit. This prevents the service from being started, manually or automatically.
List all installed services
The systemctl list-unit-files command with -t or –type service filter shows the state of installed services only. To see all active service units, use list-units with -t service filter The output has the following columns :
UNIT: The systemd service unit name LOAD: Shows whether the unit definition was properly read and loaded ACTIVE: Describes if the unit is active. SUB: Low-level activation state of the unit, giving more detailed information about the unit. This will vary by unit type. DESCRIPTION: The service unit’s description.
Conclusion
I hope this gives you an idea about using systemctl to manage services on Linux. If interested in learning more, you can check out this Linux Mastery course.