A unit file is responsible for specifying how systemd starts and runs. In this article, we discuss unit files, the contents of these files, service and target unit and unit files, and how to create and edit unit files.
Table of contents.
- The unit file.
- Service units.
- Target units.
- Creating a unit file.
systemd is an array of components that make up the Linux OS. Usually, the PID for this component is 1 and therefore is responsible for starting other services.
A unit file is a file that determines how systemd will start and run. In Linux unit files are stored in the:
- /etc/systemd/system - stores unit files for local configurations.
$ ls /etc/systemd/system
- /run/systemd/system - directory for runtime unit files.
$ ls /run/systemd/system
- /usr/lib/systemd/system - directory for unit files for installed packages.
$ ls /usr/lib/systemd/system
We can also list unit files of a user:
$ ls /etc/systemd/user
The unit file.
A unit file has the following three sections:
- [Unit]: Describes the service as well as its dependencies.
- [Service]: specifies the commands and environment variables that are required for the service to start.
- [Install]: describes the target for a service.
Let's execute the following command:
$ sudo systemctl cat apache2.service
Service units are used to start, stop, reload, restart, enable and disable services, other unit types include,
- device, a device recognized by the kernel. Unit files for devices have the file extension .device so as all other unit files for other unit types. For example, the unit files for mount unit types have a .mount file extension while the unit files for socket unit types have a .socket file extension and so.
- mount, a file system mount point.
- automount, a file system's auto-mount point.
- path, a file or directory in the file system.
- timer, a timer for systemd.
- socket, a socket for inter-process communication.
- target. a group comprising of systemd units.
- scope unit, a process created externally.
- slice unit, hierarchically organized units that manage system processes.
- snapshot unit, the saved state of systemd.
- swap unit, a swap file or device.
Let's look at the unit file for mariadb service:
$ sudo systemctl edit --full mariadb.service
These link and group other units together to describe the desired system state.
The following is an example of a multi-user.target unit file:
[Unit] Description=Multi-User System Documentation=man:systemd.special(7) Requires=basic.target Conflicts=rescue.service rescue.target After=basic.target rescue.service rescue.target AllowIsolate=yes
Here the target unit file doesn't contain a command to be executed. This is because its function is to connect other units.
In the above case:
- multi-user.target requires basic.target to run successfully when multi-user.target is run.
- If the rescue.service or rescue.target units run, they cause this unit to stop.
- multi-user.target unit starts after basic.target and after rescue.service and rescue.target are run, that is, if they are started.
An a.wants directory can also be specified links to units that will be started along with their targets.
Creating a unit file.
In this section, we will create a unit file that scans a network subnet for example 192.168.100.0/24.
Create a file scan.sh:
$ vim /usr/sbin/scan .sh
Add the following nmap command:
Make sure the file permissions for scan.sh file are changed:
$ sudo chmod a+x /usr/sbin/scan .sh
Now, let's create a systemd unit file.
$ sudo vim /etc/systemd/system/scanner.service
Place the following in the file:
Now, we change permissions as follows:
$ sudo chmod a+x /etc/systemd/system/scanner.service
Next, we add this to systemd.
$ sudo systemctl daemon-reload
We start the service:
$ sudo systemctl start scanner.service
We check the status as follows:
$ sudo systemctl status scanner.service
Now, to confirm that the service indeed was successful.
$ cat /home/lumunge/mal-scan.txt
We can also decide to start this service when the system boots up:
$ sudo systemctl enable scanner.service
To reverse the above action, we write:
$ sudo systemctl disable scanner.service
Now to stop the scanner service:
$ sudo systemctl stop scanner.service
We've seen how to get information regarding services from unit files and how to edit and create a unit file.
Unit files offer users flexibility since one can create and edit them.
Unit files define how systemd starts and runs.
Execute the command $ man systemd.unit, $ man systemd.service, $ man systemd.target for the manuals.