/usr/lib/systemd/system 和 /etc/systemd/system 之间有什么区别?

Meh*_*hul 82 ubuntu debian centos systemd

在所有单元文件都在之前,/etc/systemd/system/但现在一些出现在/usr/lib/systemd/system(<- 在 CentOS 上,或/lib/systemd/system<- 在 Debian/Ubuntu 上),这些文件夹之间有什么区别?

Mir*_*osz 59

这个问题已经在man 7 file-hierarchysystemd 中得到了解答(也有在线版):

        /etc
           System-specific configuration.
 (…)
 VENDOR-SUPPLIED OPERATING SYSTEM RESOURCES
       /usr
            Vendor-supplied operating system resources. 
            Usually read-only, but this is not required. Possibly 
            shared between multiple hosts. This directory should not
            be modified by the administrator, except when installing 
            or removing vendor-supplied packages.
Run Code Online (Sandbox Code Playgroud)

基本上,从分发存储库下载的包中附带的文件进入/usr/lib/systemd/. 系统管理员(用户)所做的修改进入/etc/systemd/system/.

系统特定的单位覆盖供应商提供的单位。使用插件,您可以仅覆盖单元文件的特定部分,将其余部分留给供应商(从 systemd 一开始就可以使用插件,但仅在 v219 中正确记录;请参阅 参考资料man systemd.unit)。

  • 好的,这解释了 `/etc/` 与 `/usr/lib/` 的区别,但在我的 Ubuntu 23.04 上我有:`/lib/systemd/` 和 `/usr/lib/systemd` 和 `/etc/systemd` (2认同)

slm*_*slm 53

背景

如果您查看手册页,man systemd.unit它有一个解释差异的表格。这是来自 CentOS 7.x 系统。

   UNIT LOAD PATH
          Unit files are loaded from a set of paths determined during 
          compilation, described in the two tables below. Unit files found 
          in directories listed earlier override files with the same name 
          in directories lower in the list.

           Table 1.  Load path when running in system mode (--system).
           ????????????????????????????????????????????????????????
           ?Path                    ? Description                 ?
           ????????????????????????????????????????????????????????
           ?/etc/systemd/system     ? Local configuration         ?
           ????????????????????????????????????????????????????????
           ?/run/systemd/system     ? Runtime units               ?
           ????????????????????????????????????????????????????????
           ?/usr/lib/systemd/system ? Units of installed packages ?
           ????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

当他们说“已安装的包”时,他们指的是通过 RPM 安装的任何东西。对于 Debian/Ubuntu 也可以假设相同,其中 DEB 文件将是“已安装的包”。

注意:上面来自 Debian/Ubuntu 系统的表格略有不同。

 Table 1.  Load path when running in system mode (--system).
       ????????????????????????????????????????????????????
       ?Path                ? Description                 ?
       ????????????????????????????????????????????????????
       ?/etc/systemd/system ? Local configuration         ?
       ????????????????????????????????????????????????????
       ?/run/systemd/system ? Runtime units               ?
       ????????????????????????????????????????????????????
       ?/lib/systemd/system ? Units of installed packages ?
       ????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

分析 /usr/lib/systemd/system

/usr/lib/systemd/system在 CentOS/Fedora/RHEL 系统上,您可以像这样判断哪些包拥有哪些单元文件:

$ rpm -qf /usr/lib/systemd/system/* |sort -u | head
abrt-2.1.11-50.el7.centos.x86_64
abrt-addon-ccpp-2.1.11-50.el7.centos.x86_64
abrt-addon-kerneloops-2.1.11-50.el7.centos.x86_64
abrt-addon-pstoreoops-2.1.11-50.el7.centos.x86_64
abrt-addon-vmcore-2.1.11-50.el7.centos.x86_64
abrt-addon-xorg-2.1.11-50.el7.centos.x86_64
accountsservice-0.6.45-7.el7.x86_64
acpid-2.0.19-8.el7.x86_64
alsa-utils-1.1.3-2.el7.x86_64
anaconda-core-21.48.22.134-1.el7.centos.x86_64
Run Code Online (Sandbox Code Playgroud)

分析 /etc/systemd/system

如果我们对 执行同样的操作/etc/systemd/system,我们希望找不到 RPM 拥有的文件(实际上在我的 CentOS 7.x 系统上就是这种情况。):

$ rpm -qf /etc/systemd/system/* /etc/systemd/system/*/* | grep -v 'not owned'
$
Run Code Online (Sandbox Code Playgroud)

异常值

请记住,您可能会在 下找到偶尔的杂散文件/usr/lib/systemd/system,例如 Virtualbox (vboxadd*):

$ rpm -qf /usr/lib/systemd/system/* |sort -u | grep 'not owned'
file /usr/lib/systemd/system/initrd.target.wants is not owned by any package
file /usr/lib/systemd/system/shutdown.target.wants is not owned by any package
file /usr/lib/systemd/system/vboxadd.service is not owned by any package
file /usr/lib/systemd/system/vboxadd-service.service is not owned by any package
file /usr/lib/systemd/system/vboxadd-x11.service is not owned by any package
Run Code Online (Sandbox Code Playgroud)

还有其他人。

结论

期望是/usr/lib/systemd/system一个目录应该只包含由包管理器 (YUM/DNF/RPM/APT/etc) 放在那里的 systemd 单元文件。

文件/etc/systemd/system由系统操作员手动放置在此处,用于非包形式的临时软件安装。这将包括 tarball 类型的软件安装或自制脚本。

  • 我不愿意点击这个 google 结果,因为我对 `/lib/systemd/system` 和 `/usr/lib/systemd/system` 很好奇。我很高兴我找到了这个答案。 (5认同)