systemd/FAQ

来自 Arch Linux 中文维基

常见问题[编辑 | 编辑源代码]

最新的已知问题,参见:TODO

为什么控制台上会显示日志信息?[编辑 | 编辑源代码]

请自行设置内核日志等级(loglevel)。以前,/etc/rc.sysinit 帮我们把 dmesg 的日志等级设置为 3,是比较合适的。 内核参数中加入 loglevel=3quiet 即可。

如何修改默认的 tty 控制台(getty)数量?[编辑 | 编辑源代码]

目前默认仅启动一个 getty,如果切换到其它 tty, 一改新的 getty 会通过 socket 激活方式启动。例如 [Ctl] [Alt] [F2] 会在 tty2 启动 getty。

默认情况下,最多可以自动启动 6 个 getty。[F7] 到 [F12] 不会启动 getty。要修改最大值,在 /etc/systemd/logind.conf 中修改 NAutoVTs。如果要启用所有的 [Fx],可以将其设置为 12。如果要把 systemd/日志#转发 journald 到 /dev/tty12[损坏的链接:无效的章节],可以把 NAutoVTs 设置为 11。

修改启动时启用的 tty 个数,可以采取下面方法:

添加新的 getty:

/etc/systemd/system/getty.target.wants/ 添加新的软链接即可:

# ln -sf /usr/lib/systemd/system/getty@.service /etc/systemd/system/getty.target.wants/getty@tty9.service
# systemctl start getty@tty9.service

移除 getty:

/etc/systemd/system/getty.target.wants/ 删除对应的软链接即可:

# rm /etc/systemd/system/getty.target.wants/getty@tty5.service /etc/systemd/system/getty.target.wants/getty@tty6.service
# systemctl stop getty@tty5.service getty@tty6.service

systemd 不使用 /etc/inittab 文件。

怎样输出更详细的开机信息?[编辑 | 编辑源代码]

如果内核信息输出后就什么信息都不输出了,很可能是因为你在内核参数中添加了 quiet。删除即可,然后你就可以看到一列列绿色的 [ 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 需要哪些内核模块?[编辑 | 编辑源代码]

systemd 不支持 3.0 版本之前的内核。

如果是自己编译内核,需要开启一些选项。请参考/usr/share/doc/systemd/README.

自己编译 Systemd,请参考systemd git 中的版本.

怎样知道一个目标需要哪些进程服务?[编辑 | 编辑源代码]

例如,你可能想搞明白目标单元 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) 然后在其中写入如下内容:

[Unit]
Description=My script

[Service]
ExecStart=/usr/bin/my-script

[Install]
WantedBy=multi-user.target 

然后开启该守护进程

# systemctl enable myscript.service

本例是说当目标multi-usr载入的时候,会启动你这个自定义脚本。脚本需要有可执行权限。

注意: 如果要启动 shell 脚本,请把 #!/bin/sh 加到脚本的第一行,下面的做法不会成功ExecStart=/bin/sh /path/to/script.sh

.service 状态显示绿色的 "active (exited)" (例如 iptables)[编辑 | 编辑源代码]

如果单次执行的程序配置了 RemainAfterExit=yes,这是正常的状态。详情请参考 systemd.service(5) § OPTIONSsystemd-user-sessions.servicesnftables.serviceiptables.service 等单次执行服务均是这样。

因为软链接已经存在,无法启用服务[编辑 | 编辑源代码]

启用 单元时,可能出现如下错误:

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

此错误一般发生在启用指令要创建的/etc/systemd/system/软链已经存在的时候。典型的例子是在切换显示管理器的时候(例如切换 GDMSDDM需要启用 gdm.servicesddm.service),这时/etc/systemd/system/display-manager.service 已经存在。

要解决此问题,先禁用原来的显示管理器或者使用 -f/--force 选项强制覆盖原有链接,请参考 systemctl(1) § OPTIONS