Systemd is a powerful initialization system and service manager that is utilized by the vast majority of current Linux systems. It makes it easy to manage services, automate activities, and boost system efficiency.
In this article, we will lead you through the process of creating a new systemd service on Linux.
Table of Contents
- A Linux distribution that has systemd installed (for example, Ubuntu, Fedora, or CentOS).
- Access to the system as root or sudo.
Steps to Create a New systemd Service
There are various steps involved in creating a new systemd service on Linux. To set up your service, follow the steps below:
Make a Service File
The first step is to build the systemd service file, which defines the configuration and behaviour of your service. The service file must be located in the
/etc/systemd/system/ directoryand must have a.service extension.
Launch a terminal or command prompt.
Create a new service file with a text editor. You can, for example, use the nano editor:
sudo nano /etc/systemd/system/<filename>.service
Define the sections and parameters in the service file based on your requirements. To get you started, here’s a basic template:
Description=Service for virtual machines hosted on VMware
After=systemd-remount-fs.service systemd-tmpfiles-setup.service systemd-modules-load.service
Provide a description of your service and any dependencies it requires in the
[Unit] section. once=network.target, for example, ensures that the service begins once the network is operational.
Enter the command or program to be performed by ExecStart in the
[Service] section. Set Restart to specify how systemd handles service restarts (for example, always, on-failure).
Define the target that should activate your service in the
[Install] section. default.target is a popular option.
Setting Up the Service File
To demonstrate, let’s develop a sample service that uses Nmap to scan your machine’s ports and saves the results to a file every thirty seconds. Here’s an example of a service unit file structure:
Description=Demonstration of custom nmap service.
ExecStart=/usr/bin/nmap -sS -O -oN /home/<user>/results.txt localhost
In this example:
- The Description gives a quick overview of the service.
- After=network.target ensures that the service begins only after the network is operational.
- A normal process service is indicated by type=simple.
- Restart=always guarantees that the service is restarted when it terminates.
- RestartSec=30 specifies a 30-second time between restarts.
- User=root instructs the service to operate as the root user.
- ExecStart contains the program’s command and any relevant arguments.
cloudbooklet@ubuntu:~$ sudo systemctl enable nmapper.service
Created symlink /etc/systemd/system/multi-user.target.wants/nmapper.service _ /etc/systemd/system/nmapper.service
cloudbooklet@ubuntu:~$ sudo systemctl start nmapper.service
cloudbooklet@ubuntu:~$ sudo systemctl status nmapper.service
_ nmapper.service - Demonstration of custom nmap service
Loaded: loaded (/etc/systemd/system/nmapper.service; enabled; preset: enabled)
Active: active (running) since Tue 2023-06-27 02:40:10 IST; 3s ago
Main PID: 3160 (nmap)
Tasks: 1 (limit: 4590)
L160 /usx/bin/nmap -sS -A -0 localhost -oN /home/debxrshi/results.txt
Jun 27 02:40:10 debian systemd: Started nmapper.service - Demonstration of custom nmap service..
Jun 27 02:40:11 debian nmap: Starting Nmap 7.93 ( https://nmap.org ) at 2023-06-27 02:40 IST
Enable and Start the Service
To enable and start your service, use the systemctl command. Take the following steps:
Allow the service to start automatically when the computer boots:
sudo systemctl enable <filename>.service
Start the service:
sudo systemctl start <filename>.service
Check the service’s status to confirm it is operational and error-free:
sudo systemctl status <filename>.service
Unit Types of systemd Service
|A service on the system that includes instructions for starting, restarting, and halting the service.
|A network socket that is linked to a service.
|A device that is explicitly handled by systemd.
|A mountpoint that is maintained by systemd.
|At boot, a mountpoint is automatically mounted.
|Change the system’s space.
|A point of synchronisation for other units. Typically used to launch enabled services when the computer boots.
|A path for activation depending on a path. You can, for example, launch services based on the state of a specific path, such as whether it exists or not.
|A timer that allows you to arrange the activation of another unit.
|A snapshot of the present condition of systemd. Typically used to undo temporary changes to systemd.
|Resource restriction via Linux Control Group nodes (cgroups).
|Data from systemd bus interfaces. Typically, it is used to handle external system processes.
Also read: You might also find useful our guide on How to Open Port in Linux: Simple Step-by-Step Guide
Adding a new systemd service on Linux is a simple step that can dramatically improve automation and efficiency. You can easily develop custom services that run in the background and start automatically when the system boots by following the step-by-step guide. Give your Linux system self-executing tasks to reap the benefits of streamlined operations.
Please feel free to share your thoughts and feedback in the comment section below.