In this guide, I will go over how you can modify Systemd service step-by-step.
Now before we go into the actual step, I will first explain the core components of the Systemd service.
This is so that you can understand how it all comes together.
Let’s dive right in!
What is Systemd Service?
Systemd is a service manager across numerous Linux distributions.
It is used to initialize and manage system processes effectively, thereby replacing the conventional init system.
The 4 Core Components of Systemd Service
Let’s delve into the four essential components which the Systemd service comprises of.
1. Unit Files
Unit Files serve as the foundation of Systemd services and play a key role in defining the characteristics and operational behavior of the services.
These configuration files are placed in distinct directories, each serving a specific purpose:
- /etc/systemd/system: It is designated for user-defined and customized unit files, holding priority over those supplied by the system.
- /usr/lib/systemd/system: It contains the default unit files supplied by the operating system, defining the default settings and services.
- /run/systemd/system: Reserved for runtime unit files, this path accommodates files generated by active processes and doesn’t remain after a reboot.
2. systemctl: Command-Line Management
systemctl
is the principal command-line tool known for its functionalities for managing Systemd services.
It allows users to manage and state the status of services.
Through systemctl
, users can access critical data regarding service status and logs, thereby enhancing system administration.
3. Journald: Log Management
Journald, managed through the journalctl
command, is responsible for:
- capturing and storing service logs
- offering configurable storage solutions to suit varying resource requirements.
4. Targets: System State Maintenance
Targets play a pivotal role in maintaining the system’s operational state.
They facilitate the shift between various states, and a set default target determines the system’s starting state.
What You’ll Need
- A fully functioning Linux distribution
- Familiarity with the service name and its configuration file location is essential (check out the previous section for better understanding)
- Proficiency in using a text editor is required for modifying the systemd service unit file (guide)
If you have the above prerequisites, proceed further to the steps to modify the systemd service.
How To Modify Systemd Service: Step-by-Step Guide
Step 1: Identify the Name of the Service
Before modifying a systemd service, it’s essential to accurately identify the service’s name.
This ensures you can locate its configuration file within the appropriate directory.
For example, if you’re looking to modify sshd.service, it’s typically found in the /etc/systemd/system/ directory. If you are not sure, cross-check it using the below steps:
- Open the terminal and to verify the existence of the service in a specific directory, execute the following command:
ls /etc/systemd/system/ | grep sshd
For demonstration purpose, I have verified the sshd.service in /etc/systemd/system/ directory.
Regarding the command, ls lists files in a directory, and grep filters the output to show requested entries.
Step 2: Edit Systemd Service Using Text Editor
To modify the systemd service, a text editor is needed. I have considered Nano for demonstration, which is a popular and straightforward text editor.
Before proceeding with editing, provide the correct path to the systemd service unit file to ensure you’re editing the correct configuration.
Here’s how to proceed:
- Open the terminal and enter the below command to open the systemd service file on Nano, and ensure you enter the right password:
sudo nano /etc/systemd/system/sshd.service
- Now, the service configuration file will be displayed, revealing its INI-style format with sections like [Unit], [Service], and [Install]. Ensure you make modifications in the appropriate section.
- Once done making modifications, remember to save [Ctrl+o] the changes and exit [Ctrl+x] the editor.
Note: The changes you make will directly affect how the Systemd service operates, so proceed with caution and ensure you understand the implications of your modifications before saving the file.
Step 3: Reload and Verify the Modified Systemd Service
After modifying the systemd service, it is important to execute the steps below for the system to recognize and implement the changes.
- Start by reloading the systemd daemon using the following command, which instructs the systemd daemon to recognize the updates made to service configurations:
sudo systemctl daemon-reload
- After the systemd daemon is reloaded, use the following command to restart the systemd serviceyou have made changes to:
sudo systemctl restart sshd.service
Note: The output will not be generated for the above steps. Also, ensure you get the name of the systemd service right that you have made changes to in the command.
If you’re worried whether you have done it right, proceed further to verify the status.
- Enter the following command to verify the status of the systemd service you have edited:
systemctl status sshd.service
From the above screenshot, you can notice that the status is active.
If there are any issues or errors in the modifications, the output might not be generated, or they’ll be highlighted in the output with the ‘– inactive’ status.
Also Read: How to Run a Script Before Shutdown Under Systemd
Conclusion
That’s pretty much all there is to modifying the Systemd service!
As you’re modifying the Systemd service, the changes made are direct and affect the operation of your system processes.
So, ensure you take a backup of the systemd service file before proceeding with modification.