名称相同但内容不同的 Systemd 目标单元

名称相同但内容不同的 Systemd 目标单元

我正在使用 Linux Mint 20.3 Cinnamon

$ systemctl --user get-default
default.target
$ ls -al /lib/systemd/system/default.target
lrwxrwxrwx 1 root 16 Jan 10 05:56 /lib/systemd/system/default.target -> graphical.target
$ \cat /lib/systemd/system/default.target
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Graphical Interface
Documentation=man:systemd.special(7)
Requires=multi-user.target
Wants=display-manager.service
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target display-manager.service
AllowIsolate=yes
  • default.target位于两个不同的位置,内容不同,如下:
$ \cat ~/.config/systemd/user/default.target
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Main User Target
Documentation=man:systemd.special(7)
Requires=basic.target
After=basic.target
AllowIsolate=yes

问题

  1. 如何/lib/systemd/system/default.target在服务单元文件内指定用户单元?
  2. 如何知道特定服务单元文件使用哪个默认目标?
  3. systemctl --user get-default指的是这些default.target中的哪一个?

答案1

要了解目标的当前位置,只需查询状态即可,例如:

systemctl --user status default.target

● default.target - Main User Target
     Loaded: loaded (/usr/lib/systemd/user/default.target; static)
[...]


systemctl status default.target
● graphical.target - Graphical Interface
     Loaded: loaded (/lib/systemd/system/graphical.target; indirect; vendor preset: enabled)
[...]

给出了搜索单元的顺序在手册中(作为系统/用户单元搜索路径)

这意味着如果我只是在本地.config-dir 中创建一个虚拟目标并启动它,它会首先加载,因为它在搜索路径中的位置较高:

$cat ~/.config/systemd/user/default.target

[Unit]
Description=just exists

$systemctl --user daemon-reload
$systemctl --user start default.target
$systemctl --user status default.target

● default.target - just exists
     Loaded: loaded (/home/felixjn/.config/systemd/user/default.target; static)

即,单元搜索路径就像$PATHshell 中的变量一样:将采用第一个单元匹配路径。

重要的是要知道用户路径和系统路径不同并且不重叠!

相关内容