systemd (简体中文)/FAQ (简体中文)

From ArchWiki

翻译状态:本文是 Systemd_FAQ翻译。上次翻译日期:2016-03-11。如果英文版本有所更改,则您可以帮助同步翻译。

常见问题

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

为什么控制台上会显示日志信息?

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

如何修改默认的 tty 控制台(getty)数量?

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

默认情况下,最多可以自动启动 6 个 gettty. [F7] 到 [F12] 不会启动 getty. 要修改最大值,在 /etc/systemd/logind.conf 中修改 NAutoVTs. 如果要启用所有的 [Fx],可以将其设置为 12. 如果要把 Systemd (简体中文)/Journal (简体中文)#转发 journald 到 /dev/tty12,可以把 NAutoVTs 设置为 11.

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

添加新的 getty:

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

# ln -sf /usr/lib/systemd/system/[email protected] /etc/systemd/system/getty.target.wants/[email protected]
# systemctl start [email protected]

移除 getty:

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

# rm /etc/systemd/system/getty.target.wants/[email protected] /etc/systemd/system/getty.target.wants/[email protected]
# systemctl stop [email protected] [email protected]

systemd 不使用 /etc/inittab 文件。

怎样输出更详细的开机信息?

如果内核信息输出后就什么信息都不输出了,很可能是因为你在内核参数中添加了 quiet。删除即可,然后你就可以看到一列列绿色的 [ OK ] 和红色的 [ FAILED ]了。

所有信息都记录在系统日志,可以通过 $ systemctl 查看系统状态,通过 journalctl 查看日志。

开机后控制台信息会被清空,如何避免?

自己写一个 [email protected] 文件

/usr/lib/systemd/system/[email protected] 复制到 /etc/systemd/system/[email protected],修改 TTYVTDisallocateno.

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.

切换到 systemd 后,为什么 fakeRAID 没有挂载?

请确保使用了

# systemctl enable dmraid.service

如何在启动的时候,运行自定义的一个脚本?

/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)

这很正常,本例中的 iptables 并不是守护进程,而是由内核控制,所以装载完规则后自动退出了。通过下面命令检查规则是否正确加载

# iptables --list

Failed to issue method call: File exists 错误

此错误一般发生在systemctl enable创建系统连接到/etc/systemd/system/的时候。一般是在切换显示管理器(例如从 GDM 到 KDM)时出现,这时/etc/systemd/system/display-manager.service 已经存在。

要解决此问题,禁用原来的显示管理器或者使用 systemctl -f enable 覆盖原有链接。