3

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 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 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. – Mioriin Mar 24 at 6:56
3

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'
UnitPath=/etc/systemd/system.control
/run/systemd/system.control
/run/systemd/transient
/etc/systemd/system
/run/systemd/system
/run/systemd/generator
/lib/systemd/system
/run/systemd/generator.late

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"
/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 
/lib/systemd/system

# pkg-config systemd --variable=systemdsystemunitpath | tr ':' '\n'
/etc/systemd/system
/etc/systemd/system
/run/systemd/system
/usr/local/lib/systemd/system
/lib/systemd/system
/usr/lib/systemd/system
/lib/systemd/system

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

systemdsystemunitpath=${systemdsystemconfdir}:/etc/systemd/system:/run/systemd/system:/usr/local/lib/systemd/system:${systemdsystemunitdir}:/usr/lib/systemd/system:/lib/systemd/system

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.