systemd/FAQ

来自 ArchWiki
(重定向自 Systemd FAQ)

FAQ

有关已知问题的最新列表,请查看上游 TODO

为什么我在控制台上收到日志消息?

您必须自行设置内核日志级别。历史上,/etc/rc.sysinit 为我们执行此操作,并将 dmesg 的日志级别设置为 3,这是一个相当安静的日志级别。您可以将 loglevel=3quiet 添加到您的 内核参数 中。

如何更改 getty 的默认数量?

目前,默认只启动一个 getty。如果您切换到另一个 TTY,则会在那里启动一个 getty(套接字激活样式)。换句话说,Ctl+Alt+F2 将在 tty2 上启动一个新的 getty。

默认情况下,自动激活的 getty 数量上限为六个。因此 F7F12 不会启动 getty。

如果您想更改此行为,请编辑 /etc/systemd/logind.conf 并更改 NAutoVTs 的值。如果您希望所有 Fx 键都启动一个 getty,请将 NAutoVTs 的值增加到 12。如果您正在 将 journald 转发到 tty12,请将 NAutoVTs 的值增加到 11(从而使 tty12 空闲)。

您还可以预先激活将在启动时运行的 getty。

要添加另一个预先激活的 getty,请启用启动 getty@ttyX.service

要删除 getty,请禁用停止相关的 getty@ttyX.service

systemd 不使用 /etc/inittab 文件。

如何在启动期间获得更详细的输出?

如果在 initram 消息后在控制台中看不到任何输出,则表示您的内核行中存在 quiet 参数。最好删除它,至少在第一次使用 systemd 启动时删除它,以查看一切是否正常。然后,您将看到绿色 [ OK ] 或红色 [ FAILED ] 的列表。

任何消息都会记录到系统日志中,如果您想了解系统的状态,请使用 systemctl 或使用 journalctl 查看启动/系统日志。

如何避免在启动后清除控制台?

创建一个名为 /etc/systemd/system/getty@.service.d 的目录,并将 nodisallocate.conf 放在其中以覆盖 TTYVTDisallocate 选项为 no

/etc/systemd/system/getty@.service.d/nodisallocate.conf
[Service]
TTYVTDisallocate=no

systemd 需要哪些内核选项?

不支持 3.0 之前的内核。

如果您使用自定义内核,则需要确保选择了 systemd 的选项。

如果您正在编译新内核以与已安装的 systemd 版本一起使用,则所需和推荐的选项列在 systemd README 文件 /usr/share/doc/systemd/README 中。

如果您正准备安装新版本的 systemd 并且正在运行自定义内核,则可以在 systemd GitHub 存储库中找到该文件的最新版本。

一个单元依赖于哪些其他单元?

例如,如果您想弄清楚 multi-user.target 之类的目标拉取了哪些服务,请使用如下命令

$ systemctl show -p "Wants" multi-user.target
Wants=rc-local.service avahi-daemon.service rpcbind.service NetworkManager.service acpid.service dbus.service atd.service crond.service auditd.service ntpd.service udisks.service bluetooth.service org.cups.cupsd.service wpa_supplicant.service getty.target modem-manager.service portreserve.service abrtd.service yum-updatesd.service upowerd.service test-first.service pcscd.service rsyslog.service haldaemon.service remote-fs.target plymouth-quit.service systemd-update-utmp-runlevel.service sendmail.service lvm2-monitor.service cpuspeed.service udev-post.service mdmonitor.service iscsid.service livesys.service livesys-late.service irqbalance.service iscsi.service

除了 Wants,您还可以尝试 WantedByRequiresRequiredByConflictsConflictedByBeforeAfter,以了解各自类型的依赖关系及其逆关系。

我的电脑关机了,但电源仍然开着

使用 systemctl poweroff 而不是 systemctl halt

如何让脚本在启动过程中启动?

创建一个新文件,命名为 /etc/systemd/system/myscript.service 并添加以下内容

/etc/systemd/system/myscript.service
[Unit]
Description=My script
 
[Service]
ExecStart=/usr/bin/my-script
 
[Install]
WantedBy=multi-user.target

此示例假定您希望脚本在启动目标 multi-user 时启动。确保脚本是可执行的

启用 myscript.service 以在启动时启动服务。

注意: 如果您想启动 shell 脚本,请确保脚本的第一行中有 #!/bin/sh不要编写类似 ExecStart=/bin/sh /path/to/script.sh 的内容,因为这不起作用。

服务单元活动状态为绿色的“活动 (已退出)”

对于也使用 RemainAfterExit=yes 的 oneshot 服务,这是预期的行为。有关更多信息,请参阅 systemd.service(5) § OPTIONS。以这种方式运行的 oneshot 服务的一些示例是 systemd-user-sessions.servicesnftables.serviceiptables.service

由于预先存在的符号链接而无法启用单元

当您尝试启用单元时,可能会遇到以下错误

Failed to enable unit: File /etc/systemd/system/symlink already exists and is a symlink to file.

当启用单元创建的 symlink 已存在于 /etc/systemd/system/ 中时,可能会发生这种情况。这通常发生在从一个显示管理器切换到另一个显示管理器时(例如 GDMSDDM,它们可以分别使用 gdm.servicesddm.service 启用),并且相应的符号链接 /etc/systemd/system/display-manager.service 已经存在。

要解决此问题,要么先禁用相关的显示管理器,然后再启用新的显示管理器,要么在启用新的显示管理器时使用 -f/--force 选项来覆盖任何现有的冲突符号链接(根据 systemctl(1) § OPTIONS)。