How to modify an existing systemd unit file

In this example, we will show how to modify an existing unit file.

There are three main directories where unit files are stored on the system but the ‘/etc/systemd/system/’ directory is reserved for unit files created or customized by the system administrator.

  • /usr/lib/systemd/system/ – systemd unit files dropped when the package has installed.
  • /run/systemd/system/ – systemd unit files created at run time.
  • /etc/systemd/system/ – systemd unit files created by ‘systemctl enable’ command as well as unit files added for extending a service.

See the following articles if you would like to learn more about systemd unit file:

What is a unit file?

A unit file contains configuration instructions that describe the unit and define its behavior. Several systemctl commands work with unit files in background to make the unit file function as expected.

Unit file uses the following general syntax:

unit_name.type_extension
httpd.service
  • unit_name (httpd) stands for the name of the unit.
  • type_extension (service) identifies the unit type.

To demonstrate this, we will add the following two parameters to the ‘httpd.service’ file:

Restart=on-failure
RestartSec=5s

Modifying existing systemd unit file can be done in two ways:

  • Extending the default unit configuration
  • Overridding the default unit configuration

1) Extending the default unit configuration

This section describes how to extend the default unit file with additional configuration options.

For example, to edit ‘httpd.service’ unit file, run:

$ sudo systemctl edit httpd.service

This creates an override snippet file under ‘/etc/systemd/system/httpd.service.d/override.conf’ and opens it in your text editor. Add new parameters to the httpd.service unit file and the new parameters will be added to the existing service file when the file saved.

To apply changes made to the unit, execute:

$ sudo systemctl daemon-reload

Restart the httpd service to loads the new service configuration (Unit file must be restated if you modify the running unit file).

$ sudo systemctl restart httpd

To revert the changes, execute the following steps:

To remove a snippet, run:

$ sudo rm -r /etc/systemd/system/httpd.service.d

To apply changes to unit files without rebooting the system, execute: The ‘daemon-reload’ option reloads all unit files and recreates the entire dependency tree.

$ sudo systemctl daemon-reload

To check extended or modified unit files, run: The following command output clearly shows that the file ‘apache2.service’ is extended.

$ systemd-delta

[EQUIVALENT] /etc/systemd/system/default.target → /usr/lib/systemd/system/default.target
[OVERRIDDEN] /etc/systemd/system/sshd.service → /usr/lib/systemd/system/sshd.service

Files /usr/lib/systemd/system/sshd.service and /etc/systemd/system/sshd.service are identical

[EXTENDED]   /usr/lib/systemd/system/NetworkManager.service → /usr/lib/systemd/system/NetworkManager.service.d/NetworkManager-ovs.conf
[EXTENDED]   /usr/lib/systemd/system/apache2.service → /etc/systemd/system/apache2.service.d/override.conf
[EXTENDED]   /usr/lib/systemd/system/btrfs-balance.timer → /etc/systemd/system/btrfs-balance.timer.d/schedule.conf
[EXTENDED]   /usr/lib/systemd/system/btrfs-scrub.timer → /etc/systemd/system/btrfs-scrub.timer.d/schedule.conf
[EXTENDED]   /usr/lib/systemd/system/nfs-client.target → /usr/lib/systemd/system/nfs-client.target.d/nfs.conf
[EXTENDED]   /usr/lib/systemd/system/rpc-gssd.service → /usr/lib/systemd/system/rpc-gssd.service.d/options.conf
[EXTENDED]   /usr/lib/systemd/system/rpc-statd-notify.service → /usr/lib/systemd/system/rpc-statd-notify.service.d/options.conf
[EXTENDED]   /usr/lib/systemd/system/rpc-statd.service → /usr/lib/systemd/system/rpc-statd.service.d/options.conf
[EXTENDED]   /usr/lib/systemd/system/rpc-svcgssd.service → /usr/lib/systemd/system/rpc-svcgssd.service.d/options.conf
[EXTENDED]   /usr/lib/systemd/system/systemd-sysctl.service → /usr/lib/systemd/system/systemd-sysctl.service.d/50-kernel-uname_r.conf

12 overridden configuration files found.

2) Overridding the default unit configuration

This section describes how to override the default unit configuration.

If you want to keep the changes after updating the package that provides the unit file, edit the full unit file as shown below:

$ sudo systemctl edit --full httpd.service

This will load the current unit file into the editor. When the file is saved, systemctl will create a file at ‘/etc/systemd/system/httpd.service’.

To apply changes made to the unit, execute:

$ sudo systemctl daemon-reload

Restart the httpd service to loads the new service configuration (Unit file must be restated if you modify the running unit file).

$ sudo systemctl restart httpd

Make a note: Any unit file in ‘/etc/systemd/system’ will override the corresponding file in ‘/lib/systemd/system’.

To revert the changes or return to the default configuration of the unit, delete the following custom configuration files:

To remove a full modified unit file, run:

$ sudo rm /etc/systemd/system/httpd.service

To apply changes to unit files without rebooting the system, execute: The ‘daemon-reload’ option reloads all unit files and recreates the entire dependency tree.

$ sudo systemctl daemon-reload

To check overridden or modified unit files, run: The following command clearly shows that the file ‘apache2.service’ is overwritten.

$ systemd-delta

[OVERRIDDEN] /etc/systemd/system/apache2.service → /usr/lib/systemd/system/apache2.service

--- /usr/lib/systemd/system/apache2.service     2021-06-22 22:20:27.000000000 +0530
+++ /etc/systemd/system/apache2.service 2021-07-08 14:36:06.989928469 +0530
@@ -13,6 +13,8 @@
 KillMode=mixed
 TasksMax=infinity
 NotifyAccess=all
+Restart=on-failure
+RestartSec=5s

 [Install]
 WantedBy=multi-user.target

[EQUIVALENT] /etc/systemd/system/default.target → /usr/lib/systemd/system/default.target
[OVERRIDDEN] /etc/systemd/system/sshd.service → /usr/lib/systemd/system/sshd.service

Files /usr/lib/systemd/system/sshd.service and /etc/systemd/system/sshd.service are identical

[EXTENDED]   /usr/lib/systemd/system/NetworkManager.service → /usr/lib/systemd/system/NetworkManager.service.d/NetworkManager-ovs.conf
[EXTENDED]   /usr/lib/systemd/system/btrfs-balance.timer → /etc/systemd/system/btrfs-balance.timer.d/schedule.conf
[EXTENDED]   /usr/lib/systemd/system/btrfs-scrub.timer → /etc/systemd/system/btrfs-scrub.timer.d/schedule.conf
[EXTENDED]   /usr/lib/systemd/system/nfs-client.target → /usr/lib/systemd/system/nfs-client.target.d/nfs.conf
[EXTENDED]   /usr/lib/systemd/system/rpc-gssd.service → /usr/lib/systemd/system/rpc-gssd.service.d/options.conf
[EXTENDED]   /usr/lib/systemd/system/rpc-statd-notify.service → /usr/lib/systemd/system/rpc-statd-notify.service.d/options.conf
[EXTENDED]   /usr/lib/systemd/system/rpc-statd.service → /usr/lib/systemd/system/rpc-statd.service.d/options.conf
[EXTENDED]   /usr/lib/systemd/system/rpc-svcgssd.service → /usr/lib/systemd/system/rpc-svcgssd.service.d/options.conf
[EXTENDED]   /usr/lib/systemd/system/systemd-sysctl.service → /usr/lib/systemd/system/systemd-sysctl.service.d/50-kernel-uname_r.conf

13 overridden configuration files found.

Over to You

In this guide, we have explained how to modify an existing systemd unit file using two different methods.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

3 Comments on “How to modify an existing systemd unit file”

  1. I wish I had seen this before doing it the wrong way.

    I set up a service to run an in-house version of Apache on RHEL 7.9. That worked fine. Today I installed an updated version of Apache into a parallel location to the first one. I figured, incorrectly, that all I needed to do was to modify /etc/systemd/system/httpd.service and run “systemctl daemon-reload” and it should work.

    It didn’t. There are two problems.

    1. I commented out and copied parts of the specification which had a path name to the old version, and just changed the pathname in the uncommented version to point to the new version. I am now getting error messages about multiple Exec lines.

    2. Even when I use “systemctl edit –full httpd.service” and remove the commented lines and clean it up, followed by “systemctl daemon-reload” it is still not seeing the correct httpd.service file. It looks like data is being cached somewhere and I can’t figure out how to clear that.

Leave a Reply

Your email address will not be published. Required fields are marked *