I understand that systemd stores unit files at different locations for different versions of Linux. On RHEL, it's at /usr/lib/systemd/system/, whereas on Debian-based machines it's at /lib/systemd/system/.

However, on my Ubuntu 18.04 machine, I just installed Elasticsearch using a .deb file, and its systemd unit file was installed under /usr/lib/systemd/system/, but systemd is still able to pick it up.

$ uname -a
Linux nucleolus 4.15.0-46-generic #49-Ubuntu SMP Wed Feb 6 09:33:07 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

$ sudo systemctl status elasticsearch.service
● elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: http://www.elastic.co

Note the path is /usr/lib/systemd/system/elasticsearch.service.

So why does a systemd unit file at /usr/lib/systemd/system/ still works for Ubuntu? What is the real unit file load path for Debian/Ubuntu systems?

  • Why would they differ with different versions of Linux? Can you use the right terminology or not? Do you know there's thing called man page or not? freedesktop.org/software/systemd/man/systemd.unit.html – 炸鱼薯条德里克 Mar 24 '19 at 1:58
  • 1
    @炸鱼薯条德里克 The man page for Ubuntu 18.04 does indeed list the path as /lib/systemd/system/ instead of /usr/lib/systemd/system/. – Haxiel Mar 24 '19 at 5:09
  • Many systems have /lib/ simply as a symlink to /usr/lib/ to maintain compatibility with programs that use /lib/ as the library directory. Do ls -l / and ls -l /usr/ to see which lib/ you have as a directory. – Mio Rin Mar 24 '19 at 6:56

The paths systemd looks up for unit files is read from UnitPath and can be queried with systemctl.

# systemctl --no-pager --property=UnitPath show | tr ' ' '\n'

As you can see, this does not include /usr/lib/systemd/system, which is the output on a Ubuntu 18.04 system. The UnitPath is generated during runtime and only directories that actually exist are shown here.

# mkdir -p /usr/lib/systemd/system
# systemctl daemon-reload
# systemctl --no-pager --property=UnitPath show | tr ' ' '\n' | grep "/usr/lib/systemd/system"

So creating the directory was enough to add /usr/lib/systemd/system to UnitPath, which was likely done by installing Elasticsearch.

Which directories are taken into account when constructing UnitPath can be queried with pkg-config and the variables systemdsystemunitdir and systemdsystemunitpath.

# pkg-config systemd --variable=systemdsystemunitdir 

# pkg-config systemd --variable=systemdsystemunitpath | tr ':' '\n'

In src/core/systemd.pc.in the systemdsystemunitpath is as follows.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.