How to Locate the Source File of a Specific systemd Service Unit


3 views

When working with systemd services, unit files can be stored in multiple standard locations. The main directories systemd searches include:

/etc/systemd/system/       # Local administrator configuration
/usr/lib/systemd/system/   # Distribution-provided units
/run/systemd/system/       # Runtime units

The simplest way to locate a service's unit file is using systemctl with the show command:

systemctl show --property=FragmentPath SERVICE_NAME.service

For example, to find the nginx service unit file:

systemctl show --property=FragmentPath nginx.service

This will output the full path to the unit file:

FragmentPath=/lib/systemd/system/nginx.service

If you need more comprehensive information about a unit:

systemctl cat SERVICE_NAME.service

This command displays the complete content of the unit file, with the location shown in the header.

For services with override configurations, you might want to check additional locations:

/etc/systemd/system/SERVICE_NAME.service.d/*.conf
/run/systemd/system/SERVICE_NAME.service.d/*.conf

To see all applied configuration fragments:

systemd-analyze cat-config systemd/SERVICE_NAME.service

Let's say you modified httpd.service but it's not behaving as expected. First locate the main unit file:

systemctl show --property=FragmentPath httpd.service

Then check for overrides:

ls /etc/systemd/system/httpd.service.d/

Finally, view all merged configurations:

systemd-analyze cat-config systemd/httpd.service

For frequent use, create a shell function in your ~/.bashrc:

function find-systemd-unit() {
    systemctl show --property=FragmentPath "$1" | cut -d= -f2
}

Usage:

find-systemd-unit nginx.service

When working with systemd services, unit files can be scattered across multiple directories. These include:

  • /usr/lib/systemd/system/ - Default system unit files (packaged)
  • /etc/systemd/system/ - System administrator's modified unit files
  • /run/systemd/system/ - Runtime unit files
  • /usr/local/lib/systemd/system/ - Locally installed software units

The fastest way to find where systemd loaded a service's unit file is:

systemctl show servicename --property=FragmentPath

For example, to find the nginx service unit file:

systemctl show nginx --property=FragmentPath

Sample output:

FragmentPath=/lib/systemd/system/nginx.service

If you need more detailed information about the unit file, try:

systemctl status servicename.service

Or to see all possible locations where systemd looks for unit files:

systemctl show --property=UnitPath

Let's say you want to modify the sshd service:

# First locate the unit file
systemctl show sshd --property=FragmentPath

# Output shows /lib/systemd/system/ssh.service
# To modify, create an override:
sudo systemctl edit sshd

# This creates /etc/systemd/system/ssh.service.d/override.conf

To list all unit files of a specific type (service, socket, etc.):

systemctl list-unit-files --type=service

Or to find all unit files for a particular application:

systemctl list-unit-files | grep nginx

For services using template units (like getty@.service), you might need to specify the full unit name:

systemctl show getty@tty1 --property=FragmentPath

Remember that systemd merges configurations from multiple locations, with /etc/systemd/system/ taking precedence.