There are many different places where systemd unit files may be placed. Is there a quick and easy way to ask systemd where it read a service’s declaration from, given just the service name?
4 Answers
For units that are defined in actual, static files, this can be seen in systemctl status:
$ systemctl status halt-local.service ● halt-local.service - /usr/sbin/halt.local Compatibility Loaded: loaded (/lib/systemd/system/halt-local.service; static) Active: inactive (dead) But there are units that are not defined by files, e.g. with systemd-cron installed. These have no useful location listed with status:
$ systemctl status cron-jojo-0.timer ● cron-jojo-0.timer - [Cron] "*/10 * * * * ..." Loaded: loaded (/var/spool/cron/crontabs/jojo) Active: active (waiting) since Mon 2015-05-18 14:53:01 UTC; 9min ago In either case, though, the FragmentPath field is educating:
$ systemctl show -P FragmentPath cron-daily.service /lib/systemd/system/cron-daily.service $ systemctl show -P FragmentPath cron-jojo-0.service /run/systemd/generator/cron-jojo-0.service $ systemctl show -P FragmentPath halt-local.service /lib/systemd/system/halt-local.service - How about the path of some mask service? (not all of them are in /lib/systemd/system or /usr/lib/systemd/system)desgua– desgua2020-10-31 15:28:49 +00:00Commented Oct 31, 2020 at 15:28
- Good, but partial, answer. But FragmentPath can be empty, e.g: systemctl show -p FragmentPath subsystem-net-devices-eth0.deviceBobHy– BobHy2022-03-31 19:09:12 +00:00Commented Mar 31, 2022 at 19:09
- 1You can also use
-pinstead of-Pif you prefer to seeFragmentPath=before the response.Joachim Breitner– Joachim Breitner2023-11-09 12:28:37 +00:00Commented Nov 9, 2023 at 12:28 - This answer is good. But got generated services - not that useful. I want to see how a service was generated. Is this possible? Presently I only see
Loaded: loaded (/etc/containers/systemd/users/1002/pgsql.container; generated). Ok I found.systemctl cat my.serviceorsystemctl show my.service. Thanks to serverfault.com/q/1091541/70242akostadinov– akostadinov2024-09-06 13:23:07 +00:00Commented Sep 6, 2024 at 13:23 - I noticed you can also use a wild card, like this:
systemctl show -P FragmentPath *.service --no-pagernot2qubit– not2qubit2025-01-06 10:43:03 +00:00Commented Jan 6 at 10:43
You could cat the systemd unit. This shows the file location as comments. Bonus: It also shows overrides.
systemctl cat sssd # /lib/systemd/system/sssd.service [Unit] ... # /etc/systemd/system/sssd.service.d/override.conf [Unit] ... - 1Neat, especially with the override! As my other solution it does not work for stuff like
net-devices-eth0.device.Joachim Breitner– Joachim Breitner2022-06-15 18:40:27 +00:00Commented Jun 15, 2022 at 18:40 - Use a wildcard:
systemctl cat *.service --no-pagernot2qubit– not2qubit2025-01-06 10:44:19 +00:00Commented Jan 6 at 10:44
You could do this (using nullmailer as an example):
systemctl show nullmailer | grep FragmentPath | awk -F'=' '{print $2}' That will produce something like this:
/lib/systemd/system/nullmailer.service Then to see the service file content, you could do this:
cat $(systemctl show nullmailer | grep FragmentPath | awk -F'=' '{print $2}') And that would produce something like this:
[Unit] Description=nullmailer [Unit] Description=Nullmailer relay-only MTA ... stuff omitted for brevity ... [Install] WantedBy=multi-user.target Hope it helps.
PS. I typically put those commands in an alias file just for convenience.
P.PS. As Joaquin mentioned, you can use the -P flag instead of using the grep|awk combo I was using/mentioning.
systemctl show nullmailer -P FragmentPath - 1Why do you use
grepandawkwhensystemctlsupports-Pto print just one field?Joachim Breitner– Joachim Breitner2023-03-03 10:04:17 +00:00Commented Mar 3, 2023 at 10:04 - 1@JoachimBreitner - well, I learned something today. Thanks!luis.espinal– luis.espinal2023-03-03 14:46:36 +00:00Commented Mar 3, 2023 at 14:46
This below one gives multiple file locations
show-- Show properties of one or more units/jobs or the manager
-p--property=NAMEShow only properties by this name
$ systemctl show -p FragmentPath {accounts-daemon,ntp,sshd} FragmentPath=/lib/systemd/system/accounts-daemon.service FragmentPath=/lib/systemd/system/ntp.service FragmentPath=/lib/systemd/system/ssh.service - Note that
systemctl show -p FragmentPath {accounts-daemon,ntp,sshd}becomessystemctl show -p FragmentPath accounts-daemon ntp sshdafter the shell performs brace expansion.David Winiecki– David Winiecki2024-03-06 18:42:15 +00:00Commented Mar 6, 2024 at 18:42