Table of Contents
This practical guide explains how to create a new systemd service on linux with clear steps and useful context. It also covers common requirements, potential problems, and the details that can help you get better results.
If you need to create a task or program that automatically starts executing every time you boot or reboot your system, you might consider creating a new service. Learn how to create a new, custom systemd service on Linux.
Step 1: Create the Service File
There are several steps required to create a new systemd service file. The first thing is to create a unit file for the service. Before creating a service file, you must understand its structure.
Let's study the service file by getting a real, working service file from your Linux system. Below you can see the service file for the vmtools service daemon.

This particular service may not be available on your system unless you are also running Linux on VMware with VMware tools installed. The service in question is not important because all services, or rather unit service files, have the same basic structure with some customizations required.
All systemd service files must have 3 sections: [Service], [Unit] and [Install] and a few parameters in each section. Here's what each section contains and why they're important:
1. Unit
The Unit section includes important metadata such as the service's description and dependencies. It has 3 parameters: Description, Before and After. Likewise, the Description parameter provides some context of the service and its functionality.
The Before and After parameters define what conditions must be met for the service to execute. For example, if you are starting a web server service, you will want it to start only after the network service comes online. So you will set the value of the After parameter for the network service.
2. Service
The Service section contains two required parameters: ExecStart, Type and a few other optional parameters like ExecReload, etc.
ExecStart defines the command to be executed when the service starts, while the Type parameter defines the type of process that will appear.
3. Install
This section and its data are called whenever you enable or disable the service using the systemctl command.
It has a few parameters. One of the popular and necessary ones is WantedBy. The WantedBy parameter defines the target units to start whenever the service is enabled. The default value is multi-user.target.
Note : The parameters mentioned here are not the only ones you can set in the unit. You can get the full list of parameters from the official systemd.exec documentation or by typing man systemd.exec in a terminal.
The systemd unit service file always ends with the ".service" extension and must be stored in the /etc/systemd/system/ directory. Create the service file with touch command with elevated privileges by prefixing it with the sudo command:
sudo touch /etc/systemd/system/.service
Now that you have created the service file, let's start filling in the syntax needed to make the service valid and working.
Step 2: Configure the Service File

You will create a sample service that executes Nmap that scans the ports on the machine and saves the output in a file every 30 seconds. To achieve this task, here is how the unit service file should be structured:
[Unit] Description=Demonstration of custom nmap service. After=network.target [Service] Type=simple User=root ExecStart=/usr/bin/nmap -sS -O -oN /home//results.txt localhost Restart=always RestartSec=30 [Install] WantedBy=multi-user.target
Although the mentioned parameters have been explained before, find out how they affect the newly created service and also explore the new parameters introduced in this section: Restart, RestartSec and User .
Here is the meaning of the parameters in each section:
- Description : Human-readable text that describes the functionality of the service.
- After=network.target : Tells systemd that this service depends on network.target and should only start after the network.target service has been started. Note that After is not used to establish any direct dependencies, it only acts as a trigger.
- Type=simple : There are many types of services. However, the service in this demo is a regular process. You can find all the different values for this on the official documentation page linked earlier.
- Restart=always : This means that whenever the service exits, it will always restart.
- RestartSec=30 : This sets the interval between each service start to 30 seconds.
- User=root : This specifies that the service will run as user. In this case, this is the right step because Nmap will not be able to run without root privileges.
- ExecStart : This command holds the absolute path to the program to be executed along with all required flags or arguments required for the program to function properly.
- WantedBy=multi-user.target : This parameter in the service file specifies which target to include or "want" the service. When a service is included in a target, it means that the service will start when the system reaches that target during boot. In this case, the service will start when the system enters multi-user mode. Multi-user mode is a state in which the system is fully booted, allowing multiple users to log in and use the system.
Step 3: Activate and Start the Service

Now that you have created the unit file, the only steps left are to enable and run the service. You can enable and start your service with the systemctl command.
Here's how to use systemctl to enable, start, and check the status of your service:
sudo systemctl enable .service sudo systemctl start .service sudo systemctl status .service
Now your custom service will be up and running! In this case, you will see that the Nmap scan runs every 30 seconds and the output is stored in the results.txt file in the home directory.
Conclusion
Understanding Linux makes it easier to compare options, avoid common mistakes, and apply the information in this guide more effectively. Review the relevant requirements before making changes or choosing a solution.
FAQ
How do you create a new systemd service on linux?
Follow the steps in this guide in order, confirm the required settings or tools, and verify the result before making additional changes.
Is it safe to create a new systemd service on linux?
It is generally safe when you follow the recommended instructions, use trusted tools, and avoid changing settings you do not understand.
What should you do if the process does not work?
Recheck the requirements, restart the device or application when appropriate, and review each step for missed settings or compatibility issues.
Reader Comments 0
Sign in with email or Google to join the discussion.