Difference between revisions of "Systemd (简体中文)"

From ArchWiki
Jump to: navigation, search
(rm temporary i18n template)
Line 1: Line 1:
[[en:Systemd]]
+
{{Lowercase title}}
 +
[[Category:简体中文]]
 +
[[Category:Daemons and system services (简体中文)]]
 +
[[Category:Boot process (简体中文)]]
 
[[fr:Systemd]]
 
[[fr:Systemd]]
 
[[it:Systemd]]
 
[[it:Systemd]]
 
[[ru:Systemd]]
 
[[ru:Systemd]]
[[Category: Daemons and system services (简体中文)]]
+
[[zh-CN:Systemd]]
[[Category:Boot process (简体中文)]]
+
{{Article summary start}}
[[Category:简体中文]]
+
{{Article summary text|'''systemd''' 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本(init scripts)。systemd的特性有:支持积极并行化任务;同时采用socket式与[[D-Bus (简体中文)|D-Bus]]总线式激活服务;按需启动守护进程(daemon);利用 Linux 的[[cgroups (简体中文)|cgroups]]监视进程;支持快照和系统恢复;维护挂载点;各服务间基于依赖关系进行精密控制。systemd 完全可以替代 Arch 默认的 sysvinit 启动系统。}}
{{Lowercase title}}
+
{{Article summary heading|相关页面}}
 +
{{Article summary wiki|Systemd (简体中文)/Services}}
 +
{{Article summary heading|相关链接}}
 +
{{Article summary text|[http://0pointer.de/blog/projects/systemd.html 开发者 Lennart 的博文]}}
 +
{{Article summary text|[http://0pointer.de/blog/projects/systemd-update.html 一篇更新报告]}}
 +
{{Article summary text|[http://0pointer.de/blog/projects/systemd-update-2.html 另一篇更新报告]}}
 +
{{Article summary text|[http://0pointer.de/blog/projects/why.html 最新动态]}}
 +
{{Article summary text|[http://en.wikipedia.org/wiki/Systemd 维基百科页面]}}
 +
{{Article summary text|[http://freedesktop.org/wiki/Software/systemd 项目主页]}}
 +
{{Article summary end}}
  
'''systemd''' 是一款Linux系统和服务管理器,它与SysV和LSB的init脚本兼容。systemd 提供并行化的socket和[[D-Bus]]启动过程,按需启动daemon,保留了Linux [[cgroups]]进程的追踪功能,支持快照和系统状态恢复,支持维护挂载和自挂载点,实现了基于依赖关系的精细的服务控制逻辑。它能够替代sysvinit系统工作。
+
== 安装 ==
 +
systemd 可以和 Arch 默认的启动系统(initscripts)共存,通过添加/删除内核参数 {{Ic|1=init=/bin/systemd}} 切换。马上开始试用 systemd:
  
具体内容参考[http://0pointer.de/blog/projects/systemd.html 作者Lennart的Blog],上面有加长版介绍,之后还有这两篇 [http://0pointer.de/blog/projects/systemd-update.html status] [http://0pointer.de/blog/projects/systemd-update-2.html updates],和[http://0pointer.de/blog/projects/why.html Why systemd?]。也可以参考[http://en.wikipedia.org/wiki/Systemd 维基百科]和[http://freedesktop.org/wiki/Software/systemd 项目主页].
+
# 从[core]仓库安装 {{Pkg|systemd}}。
 +
# 在启动引导器添加内核参数 {{Ic|1=init=/bin/systemd}}。
 +
# (可选)如果希望完全使用 systemd,可以移除 {{Pkg|initscripts}} 并安装 {{Pkg|systemd-sysvcompat}}。删除 sysvinit 后,由该包提供一些基本命令({{ic|init}}、{{ic|reboot}} 等)的链接。之后,可以删除上一步添加的内核参数。
 +
# 建议安装 {{Pkg|systemd-arch-units}},该包提供了常用系统服务的 systemd 原生服务文件。
 +
# 启用原来 rc.conf 中配置的自启动服务,systemd 无法完全兼容该文件。
  
== 安装 ==
+
{{警告|udev 和很多其他系统软件要求 {{ic|/usr}} 在启动早期就挂载。如果 {{Ic|/usr}} 和根目录不在同一分区,那么必须做一些额外设置。参见 [[Mkinitcpio (简体中文)#/usr_放到单独分区|mkinitcpio相关页面]] 以及 [http://www.freedesktop.org/wiki/Software/systemd/separate-usr-is-broken freedesktop.org#separate-usr-is-broken freedesktop.org的相关页面]。}}
在Arch上尝试systemd你需要:
+
 
 +
== 原生 systemd 配置文件 ==
 +
如果下列文件不存在,{{Pkg|systemd}} 会从 {{ic|/etc/rc.conf}} 读取相关配置。
 +
{{小贴士|某些文件可能需要手动创建。}}
 +
 
 +
=== 主机名 ===
 +
设置主机名为“myhostname”:
 +
{{hc|/etc/hostname|myhostname}}
 +
 
 +
=== 控制台和键盘布局 ===
 +
{{ic|/etc/vconsole.conf}} 文件用来配置虚拟控制台,包括键盘映射和控制台字体:
 +
{{hc|/etc/vconsole.conf|<nowiki>
 +
KEYMAP=us
 +
FONT=lat9w-16
 +
FONT_MAP=8859-1_to_uni</nowiki>}}
 +
 
 +
=== Locale ===
 +
更多内容参阅 {{ic|man locale.conf}}。
 +
{{hc|/etc/locale.conf|<nowiki>
 +
LANG=en_US.UTF-8
 +
LC_COLLATE=C</nowiki>}}
 +
 
 +
=== 时区 ===
 +
更多内容参阅 {{ic|man 5 timezone}}。
 +
{{hc|/etc/timezone|Asia/Shanghai}}
 +
{{注意|设置该文件后仍需配置 {{ic|/etc/localtime}}。}}
 +
 
 +
=== 硬件时钟 ===
 +
systemd 默认识别硬件时钟为协调世界时(UTC),也推荐这样设置。处理夏令时有些麻烦,如果夏令时调整发生在关机时,下次启动时时间会出现问题([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html 更多信息])。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 {{ic|hwclock}},内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。
 +
 
 +
如果同时安装了 Windows 操作系统([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx 默认使用地方时]),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,在注册表中设置下列DWORD键值为{{ic|1}}即可:
 +
{{bc|HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\RealTimeIsUniversal}}
 +
 
 +
{{警告|在最新的 Windows 7、 Vista SP2 上,此设置会导致网络时间同步无法使用。而早期的版本中,此设置会导致系统[http://social.msdn.microsoft.com/forums/en-US/tabletandtouch/thread/0b872d8a-69e9-40a6-a71f-45de90c6e243/ 从睡眠、待机唤醒后出现故障]。此外,Windows 还可能因此[http://support.microsoft.com/kb/2687252 无法处理夏令时调整]。}}
 +
 
 +
如果不想因此弄乱 Windows,systemd 也可以接受本地时:
 +
{{hc|/etc/adjtime|<nowiki>
 +
0.0 0.0 0.0
 +
0
 +
LOCAL</nowiki>}}
 +
{{注意|The other parameters are still needed but are ignored by systemd. (译者不理解……)}}
 +
{{注意|推荐使用[[NTP (简体中文)|NTP服务]]来确保硬件时钟同步。}}
 +
 
 +
=== 开机加载内核模块 ===
 +
systemd 读取 {{ic|/etc/modules-load.d/}} 中的配置,加载内核模块。配置文件名称通常为 {{ic|/etc/modules-load.d/<program>.conf}}。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为{{ic|#}}或{{ic|;}}的行会被忽略,如:
 +
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki>
 +
# Load virtio-net.ko at boot
 +
virtio-net</nowiki>}}
 +
另见:[[Kernel modules (简体中文)#选项]]
 +
 
 +
=== 禁用内核模块 ===
 +
禁用内核模块的方法和 Arch 默认的 {{Pkg|initscripts}} 相同,因为该过程实际由 {{Pkg|kmod}} 处理。参见:[[Kernel modules (简体中文)#黑名单]]。
 +
 
 +
=== 临时文件 ===
 +
Systemd-tmpfiles uses the configuration files in {{ic|/etc/tmpfiles.d/}} to describe the creation, cleaning and removal of volatile and temporary files and directories which usually reside in directories such as {{ic|/run}} or {{ic|/tmp}}. Each configuration file is named in the style of {{ic|/etc/tmpfiles.d/<program>.conf}}. This will also override any files in {{ic|/usr/lib/tmpfiles.d/}} with the same name.
 +
 
 +
tmpfiles are usually provided together with service files to create directories which are expected to exist by certain daemons. For example the [[Samba]] daemon expects the directory {{ic|/var/run/samba}} to exist and to have the correct permissions. The corresponding tmpfile looks like this:
 +
{{hc|/usr/lib/tmpfiles.d/samba.conf|
 +
D /var/run/samba 0755 root root
 +
}}
 +
 
 +
However, tmpfiles may also be used to write values into certain files on boot. For example, if you use {{ic|/etc/rc.local}} to disable wakeup from USB devices with {{ic|echo USBE > /proc/acpi/wakeup}}, you may use the following tmpfile instead:
 +
{{hc|/etc/tmpfiles.d/disable-usb-wake.conf|
 +
w /proc/acpi/wakeup - - - - USBE
 +
}}
 +
The tmpfiles method is recommended in this case since systemd doesn't actually support {{ic|/etc/rc.local}}.
 +
 
 +
See {{ic|man tmpfiles.d}} for details.
 +
 
 +
=== 挂载网络文件系统 ===
 +
systemd automatically makes sure that remote filesystem mounts like [[NFS]] or [[Samba]] are only started after the network has been set up. Therefore remote filesystem mounts specified in {{ic|/etc/fstab}} should work out of the box.
 +
 
 +
You may however want to use [[Systemd#Automount|Automount]] for remote filesystem mounts to mount them only when there are being accessed. Furthermore you can use the {{ic|1=x-systemd.device-timeout=#}} option in {{ic|/etc/fstab}} to specify a timeout in case the network resource is not available.
 +
 
 +
See {{ic|man systemd.mount}} for details.
 +
 
 +
=== 用 systemd 替代 acpid ===
 +
Systemd can handle some power-related ACPI events. This is configured via the following options in {{ic|/etc/systemd/logind.conf}}:
 +
* {{ic|HandlePowerKey}} : Power off the system when the power button is pressed
 +
* {{ic|HandleSleepKey}} : Suspend the system when the sleep key is pressed
 +
* {{ic|HandleLidSwitch}} : Suspend the system when the laptop lid is closed
 +
Depending on the value of these options, these events may for example only be triggered when no user is logged in ({{ic|no-session}}) or when only a single user session is active ({{ic|any-session}}). See {{ic|man logind.conf}} for details.
  
* 从[core]中安装 {{Pkg|systemd}} 以及依赖项
+
These options should not be used on desktop environments like [[Gnome]] and [[XFCE]] since these handle ACPI events by themselves. However, on systems which run no graphical setup or only a simple window manager like [[i3]] or [[awesome]], this may replace the [[acpid]] daemon which is usually used to react to these ACPI events.
* 添加 {{Ic|1=init=/bin/systemd}} 到你引导程序的kernel命令行上
+
  
:{{注意|1=如果你使用Grub2,请把内核参数添加到 {{Ic|/etc/default/grub}} - {{Ic|1=GRUB_CMDLINE_LINUX="..."}}}}
+
=== 单元(unit) ===
:{{小贴士|1=systemd 能够同标准的Arch Linux initscripts一起使用,通过添加或删除 {{Ic|1=init=/bin/systemd}} 内核参数来切换他们。如果要使用纯净的systemd可以删除initscripts并安装 {{Pkg|systemd-sysvcompat}} ,该包提供 {{Ic|1=init}} 、 {{Ic|1=reboot}} 等命令的系统链接。在此之后就可以从kernel命令行中删除 {{Ic|1=init=}} 参数了。 }}
+
A unit configuration file encodes information about a service, a socket, a device, a mount point, an automount point, a swap file or partition, a start-up target, a file system path or a timer controlled and supervised by systemd. The syntax is inspired by XDG Desktop Entry Specification .desktop files, which are in turn inspired by Microsoft Windows .ini files. See {{ic|man systemd.unit}} for more info.
  
* 要获得用systemd方式启动服务的优势,你可能还需要安装 {{Pkg|systemd-arch-units}} 包。
+
== systemd 命令 ==
  
{{警告|udev和其他许多程序组件需要 {{ic|/usr}} 启动之初就被挂载并可以读写。如果你的 {{Ic|/usr}} 在独立分区上,那么在启动阶段initramfs必须提供空间以供挂载,关机时则需要从一个转移的根目录下卸载。详情参见[[Mkinitcpio#/usr_as_a_separate_partition|the mkinitcpio wiki page]] 以及 [http://www.freedesktop.org/wiki/Software/systemd/separate-usr-is-broken freedesktop.org#separate-usr-is-broken]}}
+
*{{ic|systemctl}}: used to introspect and control the state of the systemd system and service manager.
 +
*{{ic|systemd-cgls}}: recursively shows the contents of the selected Linux control group hierarchy in a tree
 +
*{{ic|systemadm}}: a graphical frontend for the systemd system and service manager that allows introspection and control of systemd (avaiable via the {{AUR|systemd-ui-git}} package from the [[AUR]]).
  
== 使用systemd ==
+
View the man pages for more details.
  
*systemctl: 用作内省和控制 systemd 系统和服务管理器的状态。
+
{{Tip|You can use all of the following {{ic|systemctl}} commands with the {{ic|-H <user>@<host>}} switch to control a systemd instance on a remote machine. This will use [[SSH]] to connect to the remote systemd instance.}}
*systemd-cgls: 以树形递归显示选中的 Linux 控制组结构层次。
+
*systemadm: 一个 systemd 系统和服务管理器的图形化前端。
+
  
请查看man手册获得更多内容。
+
=== 评测系统状态 ===
  
列出正在运行的单元:
+
List running units:
  
 
{{bc|$ systemctl}}
 
{{bc|$ systemctl}}
  
或者
+
or:
  
 
{{bc|$ systemctl list-units}}
 
{{bc|$ systemctl list-units}}
  
所有可用的服务或单元在 {{ic|/usr/lib/systemd/system/}} 和 {{ic|/etc/systemd/system}} 中(后者优先),也可使用:
+
List failed units:
  
{{bc|$ systemctl -a}}
+
{{bc|$ systemctl --failed}}
  
启动服务:
+
The available unit files can be seen in {{ic|/usr/lib/systemd/system/}} and {{ic|/etc/systemd/system/}} (the latter takes precedence).
  
{{bc|# systemctl start <service>}}
+
=== 使用单元 ===
  
停止服务:
+
Units can be services ({{ic|.service}}), mount points ({{ic|.mount}}) or sockets ({{ic|.sockets}}). When using {{ic|systemctl}}, you always have to specify the complete name of the unit file, including its suffix, for example {{ic|netcfg.service}}. See {{ic|man systemd.unit}} for details.
  
{{bc|# systemctl stop <service>}}
+
Activate a unit immediately:
  
重启服务:
+
{{bc|# systemctl start <unit>}}
  
{{bc|# systemctl restart <service>}}
+
Deactivate a unit immediately:
  
重载服务:
+
{{bc|# systemctl stop <unit>}}
  
{{bc|# systemctl reload <service>}}
+
Restart a unit:
  
查看服务状态,是否运行:
+
{{bc|# systemctl restart <unit>}}
  
{{bc|# systemctl status <service>}}
+
Ask a unit to reload its configuration:
  
开机时启动某个服务:
+
{{bc|# systemctl reload <unit>}}
  
{{bc|# systemctl enable <service>}}
+
Show the status of a unit, including whether it is running or not:
  
开机时不启动服务:
+
{{bc|$ systemctl status <unit>}}
  
{{bc|# systemctl disable <service>}}
+
Check whether a unit is already enabled or not:
  
请查看man手册获得更多内容。
+
{{bc|$ systemctl is-enabled <unit>}}
  
提示:你需要输入完整的服务文件名,如重启avahi daemon:
+
Enable a unit to be started on bootup:
  
{{bc|# systemctl restart avahi-daemon.service}}
+
{{bc|# systemctl enable <unit>}}
  
重启计算机
+
Disable a unit to not start during bootup:
  
{{bc|# systemctl reboot}}
+
{{bc|# systemctl disable <unit>}}
  
=== 运行级/目标 ===
+
Show the manual page associated with a unit (this has to be supported by the unit file):
Systemd 中的 ''目标'' 在概念上和运行级类似,但是行为上有一些区别。每个目标都有自己的名称,而不是使用编号。目标有明确的目的,有些目标继承了其它目标并增加了额外的服务。有些目标模仿 SystemVinit 中的运行级别,所以可以使用类似的方式切换目标。例如  {{ic|telinit RUNLEVEL}} 命令。用户可以以现有的目标为基础自定义 ''目标'' 为 {{ic|/etc/systemd/system/$YOURTARGET}}(可以参考 {{ic|/usr/lib/systemd/system/graphical.target}} 作为示例),建立{{ic|/etc/systemd/system/$YOURTARGET.wants}} 目录并将需要启用的服务从 {{ic|/usr/lib/systemd/system}} 链接到此目录中。
+
  
 +
{{bc|$ systemctl help <unit>}}
 +
 +
=== 电源管理 ===
 +
 +
If you are in a local user session and no other session is active, the following commands will work without root privileges. If not, systemd will automatically ask you for the root password.
 +
 +
Shut down and reboot the system:
 +
 +
{{bc|$ systemctl reboot}}
 +
 +
Shut down and power-off the system:
 +
 +
{{bc|$ systemctl poweroff}}
 +
 +
Shut down and halt the system:
 +
 +
{{bc|$ systemctl halt}}
 +
 +
Suspend the system:
 +
 +
{{bc|$ systemctl suspend}}
 +
 +
Hibernate the system:
 +
 +
{{bc|$ systemctl hibernate}}
 +
 +
== 启动级别(runlevel)/目标(target) ==
 +
Runlevels is a legacy concept in systemd. Systemd uses ''targets'' which serve a similar purpose as runlevels but act a little different. Each ''target'' is named instead of numbered and is intended to serve a specific purpose with the possibility of having multiple ones active at the same time. Some ''targets'' are implemented by inheriting all of the services of another ''target'' and adding additional services to it. There are systemd ''target''s that mimic the common SystemVinit runlevels so you can still switch ''target''s using the familiar {{ic|telinit RUNLEVEL}} command.
 +
 +
=== 获取当前启动级别/目标 ===
 +
The following should be used under systemd instead of {{ic|runlevel}}:
 +
{{bc|1=# systemctl list-units --type=target}}
 +
 +
=== 创建新目标 ===
 +
The runlevels that are assigned a specific purpose on vanilla Fedora installs; 0, 1, 3, 5, and 6; have a 1:1 mapping with a specific systemd ''target''. Unfortunately, there is no good way to do the same for the user-defined runlevels like 2 and 4.  If you make use of those it is suggested that you make a new named systemd ''target'' as {{ic|/etc/systemd/system/<your target>}} that takes one of the existing runlevels as a base (you can look at {{ic|/usr/lib/systemd/system/graphical.target}} as an example), make a directory {{ic|/etc/systemd/system/<your target>.wants}}, and then symlink the additional services from {{ic|/usr/lib/systemd/system/}} that you wish to enable.
 +
 +
=== 目标表 ===
 
{| border="1"
 
{| border="1"
!SystemVinit 运行级别!!Systemd 目标!!附注
+
!SysV Runlevel!!Systemd Target!!Notes
 
|-
 
|-
| 0 || runlevel0.target, poweroff.target || 关闭系统
+
| 0 || runlevel0.target, poweroff.target || Halt the system.
 
|-
 
|-
| 1, s, single || runlevel1.target, rescue.target || 单用户模式
+
| 1, s, single || runlevel1.target, rescue.target || Single user mode.
 
|-
 
|-
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || 用户自定义运行级别,默认和运行级别 3 一致.
+
| 2, 4 || runlevel2.target, runlevel4.target, multi-user.target || User-defined/Site-specific runlevels. By default, identical to 3.
 
|-
 
|-
| 3 || runlevel3.target, multi-user.target || 多用户、非图形模式。用户通常使用多个终端或网络连接。
+
| 3 || runlevel3.target, multi-user.target || Multi-user, non-graphical.  Users can usually login via multiple consoles or via the network.
 
|-
 
|-
| 5 || runlevel5.target, graphical.target || 多用户图形模式,通常运行所有运行级别 3 的服务并启动图形登陆界面。
+
| 5 || runlevel5.target, graphical.target || Multi-user, graphical.  Usually has all the services of runlevel 3 plus a graphical login.
 
|-
 
|-
| 6 || runlevel6.target, reboot.target || 重启
+
| 6 || runlevel6.target, reboot.target || Reboot
 
|-
 
|-
| emergency || emergency.target || 紧急 Shell
+
| emergency || emergency.target || Emergency shell
 
|-
 
|-
 
|}
 
|}
  
改变运行级:
+
=== 切换启动级别/目标 ===
 +
In systemd runlevels are exposed via "target units". You can change them like this:
 +
{{bc|# systemctl isolate graphical.target}}
 +
This will only change the current runlevel, and has no effect on the next boot.
  
{| border="1"
+
=== 修改默认启动级别/目标 ===
!SystemVinit 命令!!Systemd 命令!!附注
+
The standard target is {{ic|default.target}}, which is aliased by default to {{ic|graphical.target}} (which roughly corresponds to the old runlevel 5). To change the default target at boot-time, append one of the following kernel parameters to your bootloader:
|-
+
* {{ic|1=systemd.unit=multi-user.target}} (which roughly corresponds to the old runlevel 3),
| telinit 3 || systemctl isolate multi-user.target (或systemctl isolate runlevel3.target 或 telinit 3) || 切换到多用户运行级别.
+
* {{ic|1=systemd.unit=rescue.target}} (which roughly corresponds to the old runlevel 1).
|-
+
 
| sed s/^id:.*:initdefault:/id:3:initdefault:/ || ln -sf /usr/lib/systemd/system/multi-user.target /etc/systemd/system/default.target || 下次登陆时切换到多用户运行级别。
+
Alternatively, you may leave the bootloader alone and change {{ic|default.target}}. This can be done using {{ic|systemctl}}:
|}
+
{{bc|# systemctl enable multi-user.target}}
 +
 
 +
The effect of this command is outputted by {{ic|systemctl}}; a symlink to the new default target is made at {{ic|/etc/systemd/system/default.target}}. This works if, and only if:
 +
[Install]
 +
Alias=default.target
 +
is in the target's configuration file. Currently, {{ic|multi-user.target}} and {{ic|graphical.target}} both have it.
  
=== 在 systemd 下运行桌面环境===
+
== 启动桌面环境 ==
  
==== 使用显示管理器 ====
+
=== 使用登录管理器 ===
运行你喜欢的 [[Display Manager (简体中文)|显示管理器]](例如 [[KDM]])可以启用图形界面登陆。目前提供了 gdm, kdm, slim 和 xdm 的服务。
+
To enable graphical login, run your preferred [[Display Manager]] daemon (e.g. [[KDM]]). At the moment, service files exist for [[GDM]], [[KDM]], [[SLiM]], [[XDM]] and [[LXDM]].
  
 
{{bc|# systemctl enable kdm.service}}
 
{{bc|# systemctl enable kdm.service}}
  
如果没有正常工作,可能 default.target 没有正确设置:
+
This should work out of the box. If not, you might have a {{ic|default.target}} set manually or from a older install:
  
 
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}
 
{{hc|# ls -l /etc/systemd/system/default.target|/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target}}
  
只需要删除符号链接, systemd 会使用自带的 default.target (例如 graphical.target).
+
Simply delete the symlink and systemd will use its stock {{ic|default.target}} (i.e. {{ic|graphical.target}}).
  
 
{{bc|# rm /etc/systemd/system/default.target}}
 
{{bc|# rm /etc/systemd/system/default.target}}
  
KDE 启动的时候如果出现 "console-kit-daemon.unit" could not be found 错误,需要安装 {{pkg|systemd-arch-units}}.
+
If {{ic|/etc/locale.conf}} is used for setting the locale, add an entry to {{ic|/etc/environment}}:
 
+
如果使用 {{ic|/etc/locale.conf}} 设置 locale,请在环境变量中加入 {{ic|/etc/environment}}
+
 
{{hc|/etc/environment|<nowiki>
 
{{hc|/etc/environment|<nowiki>
 
LANG=en_US.utf8</nowiki>}}
 
LANG=en_US.utf8</nowiki>}}
并将 {{ic|auth    required    pam_env.so}} 加入 {{ic|/etc/pam.d/kde}}以读取这个值:
 
{{hc|/etc/pam.d/kde|<nowiki>
 
auth        required    pam_unix.so
 
auth        required    pam_nologin.so
 
auth        required    pam_env.so
 
account    required    pam_unix.so
 
password    required    pam_unix.so
 
session    required    pam_unix.so
 
session    required    pam_limits.so
 
-session    optional    pam_systemd.so
 
</nowiki>}}
 
  
==== 使用服务文件 ====
+
=== 使用服务文件 ===
如果想不使用显示管理器就启动 X,请创建服务文件:
+
{{Note|Using this method there will be no PAM session created for your user. Therefore ConsoleKit (which gives you access to shutdown/reboot, audio devices etc.) will not work properly. For the recommended way, see: [[Automatic_login_to_virtual_console#With_systemd]].}}
 +
If you are only looking for a simple way to start X directly without a display manager, you can create a service file similar to this:
  
 
{{hc|/etc/systemd/system/graphical.target.wants/xinit.service|<nowiki>
 
{{hc|/etc/systemd/system/graphical.target.wants/xinit.service|<nowiki>
 
[Unit]
 
[Unit]
 
Description=Direct login to X
 
Description=Direct login to X
Requires=dev-tty7.device
+
After=systemd-user-sessions.service
After=dev-tty7.device systemd-user-sessions.service
+
  
 
[Service]
 
[Service]
Line 163: Line 291:
 
</nowiki>}}
 
</nowiki>}}
  
== Arch 整合 ==
+
== systemd 日志 ==
 +
Since version 38 systemd has an own logging system, the journal.
  
Systemd 通过 {{ic|initscripts-systemd}} 软件包与 Arch 传统配置整合。这是个可选软件包,可以简化从 SysVinit 到 systemd 的切换过程。
+
By default, running a syslog daemon is no longer required. To read the log, use:
 +
{{bc|# journalctl}}
 +
The journal writes to {{ic|/run/systemd/journal}}, meaning logs will poof on reboot. For non-volatile logs, create {{ic|/var/log/journal/}}:
 +
{{bc|# mkdir /var/log/journal/}}
  
{{ic|/etc/inittab}} 已经不被使用
+
=== 日志大小限制 ===
  
{{ic|/etc/rc.local}} {{ic|/etc/rc.local.shutdown}} 可以通过启用 rc-local.service 服务在启动和关闭时运行。
+
If the journal is made non-volatile, its size limit is set to a default value of 10% of the size of the respective file system. E.g. with {{ic|/var/log/journal}} located on a 50GiB root partition this would lead to 5GiB of journal data. The maximum size of the persistent journal can be controlled by {{ic|SystemMaxUse}} in {{ic|/etc/systemd/journald.conf}}, so to limit it for example to 50MiB uncomment and edit the corresponding line to:
 +
{{bc|1=SystemMaxUse=50M}}
 +
Look at {{ic|man journald.conf}} for more info.
  
=== rc.conf ===
+
=== 配合传统日志服务 ===
作为切换过程 {{ic|/etc/rc.conf}} 中的一些变量会被读取和使用。建议纯 systemd 设置直接使用 systemd 自己的配置文件,例如 {{ic|/etc/locale.conf}}, {{ic|/etc/vconsole.conf}}, {{ic|/etc/hostname}}, {{ic|/etc/modules-load.d/*.conf}},它们的优先级比 {{ic|/etc/rc.conf}} 高。
+
Compatibility with classic syslog implementations is provided via a
 +
socket {{ic|/run/systemd/journal/syslog}}, to which all messages are forwarded.
 +
To make the syslog daemon work with the journal, it has to bind to this socket instead of {{ic|/dev/log}} ([http://lwn.net/Articles/474968/ official announcement]). For syslog-ng change {{ic|/etc/syslog-ng/syslog-ng.conf}} source section to:
 +
{{bc|<nowiki>
 +
source src {
 +
    unix-dgram("/run/systemd/journal/syslog");
 +
    internal();
 +
    file("/proc/kmsg");
 +
};</nowiki>}}
  
支持的变量:
+
and enable (or reenable) syslog-ng:
* LOCALE
+
{{bc|# systemctl enable syslog-ng.service}}
* KEYMAP
+
* CONSOLEFONT
+
* CONSOLEMAP
+
* HOSTNAME
+
* MODULES
+
* DAEMONS (会考虑到顺序和屏蔽,如果存在与原生 systemd 服务文件同名的 daemon,将优先使用老的 daemon 文件。可以禁用 arch-daemons ''systemctl disable arch-daemons.target'',这样只会使用 systemd 服务文件)
+
  
不支持的变量及解决方法:
+
By default, journald is configured to read from {{ic|/proc/kmsg}}, but this will collide with a syslog implementation doing the same ([http://lists.freedesktop.org/archives/systemd-devel/2012-January/004310.html systemd-devel post]). Disable reading {{ic|/proc/kmsg}} by {{ic|systemd-journald}} in {{ic|/etc/systemd/journald.conf}}:
* TIMEZONE:请将 {{Ic|/etc/localtime}} 手动链接到您的时区文件
+
ImportKernel=no
* HARDWARECLOCK:请用 {{ic|hwclock --systohc --utc}} 将硬件时间设置为 utc, systemd 不支持 localtime。参见 [[Systemd#Why_does_systemd_not_support_the_RTC_being_in_localtime.3F|FAQ]]
+
* USELVM:请使用  systemd-arch-units 提供的 lvm.service
+
* USECOLOR
+
  
== Initscripts package ==
+
== 网络 ==
{{ic|initscripts-systemd}} contains unit files and scripts that are needed to emulate Arch's initscripts.
+
=== DHCP 动态获取 IP ===
 +
If you simply want to use DHCP for your ethernet connection, you can use {{ic|dhcpcd@.service}} from the {{Pkg|systemd-arch-units}} package.
 +
To enable DHCP for {{ic|eth0}}, simply use:
 +
# systemctl start dhcpcd@eth0.service
  
{{warning|Usage of this package is not recommended. In particular, arch-persistent-settings.service and arch-daemons.target are unsupported as a long term solution and will be removed in the future.}}
+
You can enable the service to automatically start at boot with:
 +
# systemctl enable dhcpcd@.service
 +
Note that this will enable the service for {{ic|eth0}} by default. If you want to use another interface, you have to create the symlink manually, e.g.:
 +
# ln -s '/usr/lib/systemd/system/dhcpcd@.service' '/etc/systemd/system/multi-user.target.wants/dhcpcd@eth1.service'
  
Most people will not need all (if any) of these units, and they can be easily disabled by doing
+
=== 其他配置方式 ===
{{bc|# systemctl disable <unitfile>}}
+
For static, wireless or advanced network configuration like bridging you can use [[netcfg]] or [[NetworkManager]] which both provide systemd service files.
if you determine that you do not want a particular unit.
+
  
The plan is to remove most of the functionality from this package as soon as it is handled elsewhere (mostly in udev/systemd/kernel).
+
If you need a static ethernet configuration, but don't want to use [[netcfg]], there is a custom service file available on the [[Systemd/Services#Network|Systemd/Services page]].
  
The following is a brief description of the functionality of each of them. Alternative solutions are provided as a migration plan away from the functionality provided by this package.
+
== Arch 整合 ==
  
=== rc-local.service ===
+
Integration with Arch's classic configuration is accomplished via the {{Pkg|initscripts-systemd}} package. This is an optional package containing unit files and scripts needed to emulate Arch's initscripts, which can be used to ease the transition from sysVinit to systemd.
Runs /etc/rc-local (resp., /etc/rc-local.shutdown) on boot (resp., shutdown).
+
  
=== arch-daemons.target ===
+
{{ic|/etc/inittab}} is not used at all.
Parses the DAEMONS array in {{ic|/etc/rc.conf}} and starts the services. If a native systemd unit exists (by the same name) for a given daemon, this is used; otherwise, the script in {{ic|/etc/rc.d/}} is used to control the unit.
+
  
Alternative: use native unit files from the {{pkg|systemd-arch-units}} package.
+
{{ic|/etc/rc.local}} and {{ic|/etc/rc.local.shutdown}} can be run at startup and shutdown by enabling {{ic|rc-local.service}} and {{ic|rc-local-shutdown.service}}.
  
=== arch-persistent-settings.service ===
+
{{warning|Usage of this package is not recommended. In particular, {{ic|arch-load-modules.service}} and {{ic|arch-daemons.target}} are unsupported as a long-term solution and will be removed in the future. When ever possible, use native systemd configuration files instead.}}
This is run at shutdown. Its aim is to make sure that any Arch Linux settings are applied on the next boot. In particular:
+
* Sets the timezone based on {{ic|/etc/rc.conf}}. Alternative: Create {{ic|/etc/localtime}} as a symlink to your timezone file in {{ic|/usr/share/zoneinfo}}.
+
* Updates modle blacklists based on {{ic|/etc/rc.conf}} (see {{ic|/etc/modprobe.d/rc.conf}}). Alternative: Create a differently named copy of this file in {{ic|/etc/modprobe.d}}.
+
* Updates list of modules to be loaded based on {{ic|/etc/rc.conf}} (see {{ic|/etc/modules-load.d/rc.conf}}). Alternative: Create a differently named copy of this file in {{ic|/etc/modules-load.d}}.
+
  
== 原生 systemd 配置文件 ==
+
Most people will not need all (if any) of these units, and they can be easily disabled with:
替代 arch-persistent-settings.service,你需要编写一些原生配置文件:
+
{{bc|# systemctl disable <unitfile>}}
  
=== 添加hostname ===
+
The plan is to remove most of the functionality from this package as soon as it is handled elsewhere (mostly in udev/systemd/kernel).
{{hc|/etc/hostname|myhostname}}
+
  
=== 控制台和键盘映射 ===
+
=== rc.conf ===
{{ic|/etc/vconsole.conf}} 中配置虚拟控制台,也就是键盘映射和控制台字体。
+
Some variables in {{ic|/etc/rc.conf}} are respected by this glue work. For a pure systemd setup it is recommended to use the [[Systemd#Native_systemd_configuration_files|native systemd configuration files]] which will take precedence over {{ic|/etc/rc.conf}}.
{{hc|/etc/vconsole.conf|<nowiki>
+
KEYMAP=us
+
FONT=lat9w-16
+
FONT_MAP=8859-1_to_uni</nowiki>}}
+
  
=== 区域设置 ===
+
Supported variables:
请阅读 {{ic|man locale.conf}} 了解更多选项
+
* LOCALE
{{hc|/etc/locale.conf|<nowiki>
+
* KEYMAP
LANG=en_US.utf8
+
* CONSOLEFONT
LC_COLLATE=C</nowiki>}}
+
* CONSOLEMAP
 +
* HOSTNAME
 +
* MODULES
 +
* DAEMONS: Ordering and blacklisting is respected, if a native systemd service file by the same name as a daemon exists, it will take precedence, this logic can be disabled by {{ic|systemctl disable arch-daemons.target}}
  
=== 配置启动时加载的内核模块 ===
+
Not supported variables and systemd configuration:
Systemd uses {{ic|/etc/modules-load.d/}} to configure kernel modules to load during boot in a static list. Each configuration file is named in the style of {{ic|/etc/modules-load.d/<program>.conf}}. The configuration files should simply contain a list of kernel module names to load, separated by newlines. Empty lines and lines whose first non-whitespace character is # or ; are ignored. Example:
+
* TIMEZONE: Please symlink {{Ic|/etc/localtime}} to your zoneinfo file manually.
{{hc|/etc/modules-load.d/virtio-net.conf|<nowiki>
+
* HARDWARECLOCK: See [[Systemd#Hardware clock time|Hardware clock time]].
# Load virtio-net.ko at boot
+
* USELVM: use {{ic|lvm.service}} provided by {{Pkg|systemd-arch-units}} instead.
virtio-net</nowiki>}}
+
* USECOLOR
See also [[Modprobe#Options]]
+
  
=== 配置内核模块黑名单 ===
+
The following is a brief description of the functionality of each of them. Alternative solutions are provided as a migration plan away from the functionality provided by this package.
Systemd uses {{ic|/etc/modprobe.d/}} to configure the blacklisting of kernel modules. Each configuration file is named in the style of {{ic|/etc/modprobe.d/<program>.conf}}. Empty lines and lines whose first non-whitespace character is # or ; are ignored. Example:
+
{{hc|/etc/modprobe.d/sound.conf|<nowiki>
+
blacklist snd_hda_intel</nowiki>}}
+
or
+
{{hc|/etc/modprobe.d/sound.conf|<nowiki>
+
install snd_hda_intel /bin/false</nowiki>}}
+
参考 [[Modprobe#Blacklisting]]
+
  
=== 描述临时文件 ===
+
==== rc-local.service / rc-local-shutdown.service ====
Systemd-tmpfiles uses the configuration files in {{ic|/etc/tmpfiles.d/}} to describe the creation, cleaning and removal of volatile and temporary files and directories which usually reside in directories such as {{ic|/run}} or {{ic|/tmp}}. Each configuration file is named in the style of {{ic|/etc/tmpfiles.d/<program>.conf}}.
+
Runs {{ic|/etc/rc.local}} (resp., {{ic|/etc/rc.local.shutdown}}) on boot (resp., shutdown).
  
== Helping out ==
+
==== arch-daemons.target ====
现在 systemd 的主要功能等同于Arch's initscripts。但是还需要大量测试工作,如果你愿出分力请到 [https://github.com/falconindy/initscripts-systemd initscripts-systemd] 或 [http://github.com/falconindy/systemd-arch-units systemd-arch-units] 上提交你的想法。
+
Parses the {{ic|DAEMONS}} array in {{ic|/etc/rc.conf}} and starts the services. If a native systemd unit exists (by the same name) for a given daemon, this is used; otherwise, the script in {{ic|/etc/rc.d/}} is used to control the unit.
  
如果你遇到任何问题,请到转到Arch论坛 [https://bbs.archlinux.org/viewtopic.php?id=96316&p=1 thread]
+
Alternative: use native unit files from the {{Pkg|systemd-arch-units}} package.
 +
 
 +
==== arch-modules-load.service ====
 +
Creates a list of modules to be loaded based on {{ic|/etc/rc.conf}} (see {{ic|/etc/modules-load.d/rc.conf}}).
 +
 
 +
Alternative: create a {{ic|*.conf}} for your modules in [[Systemd#Configure_kernel_modules_to_load_during_boot|/etc/modules-load.d/]].
  
 
== FAQ ==
 
== FAQ ==
Line 268: Line 395:
 
{{FAQ
 
{{FAQ
 
|question=Why do I get log messages on my console?
 
|question=Why do I get log messages on my console?
|answer=You must set the kernel loglevel yourself. Historically, {{ic|/etc/rc.sysinit}} did this for us and set dmesg loglevel to '3', which was a reasonably quiet loglevel. Either add 'loglevel=3' or 'quiet' to your kernel cmdline.}}
+
|answer=You must set the kernel loglevel yourself. Historically, {{ic|/etc/rc.sysinit}} did this for us and set dmesg loglevel to {{ic|3}}, which was a reasonably quiet loglevel. Either add {{ic|1=loglevel=3}} or {{ic|quiet}} to your kernel cmdline.}}
 
+
{{FAQ
+
|question=Why does systemd not support the RTC being in localtime?
+
|answer=In principle, there is nothing stopping you from adding some unit files that will allow the RTC to be in localtime, but there are a few reasons why we have not (and probably will not) implement it by default:
+
 
+
* The reason for allowing the RTC to be in localtime was to allow dualboot with Windows ([http://blogs.msdn.com/b/oldnewthing/archive/2004/09/02/224672.aspx who uses localtime]). However, for some time now, Windows has been able to deal with the RTC being in UTC by setting the following registry key
+
:{{bc|HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\RealTimeIsUniversal}}
+
:This needs to be a DWORD key with a value of 1.
+
:{{Warning|On recent systems (Windows 7, Vista SP2) this setting prevents Windows from being able to update the system clock at all, [http://social.msdn.microsoft.com/forums/en-US/tabletandtouch/thread/0b872d8a-69e9-40a6-a71f-45de90c6e243/ and earlier versions don't work correctly when resuming from suspend or hibernate]. In addition, recent systems [http://support.microsoft.com/kb/2687252 may become unresponsive during Daylight Saving Time (DST) changeover if RealTimeIsUniversal is set].}}
+
 
+
* Dealing with daylight saving time is messy. If the DST changes when your computer is off, your clock will be wrong on next boot ([http://www.cl.cam.ac.uk/~mgk25/mswish/ut-rtc.html there is a lot more to it]).
+
* Recent kernels set the system time from the RTC directly on boot without using 'hwclock', the kernel will always assume that the RTC is in UTC. This means that if the RTC is in localtime, then the system time will first be set up wrongly and then corrected shortly afterwards on every boot. This is possibly the reason for certain weird bugs (time going backwards is rarely a good thing).}}
+
 
+
{{FAQ
+
|question=How do I change the current runlevel?
+
|answer=In systemd runlevels are exposed via "target units". You can change them like this:
+
{{bc|# systemctl isolate runlevel5.target}}
+
or
+
{{bc|# systemctl isolate runlevel3.target}}
+
Note however, that the concept of runlevels is a bit out of date, and it is usually nicer to use modern names for this. e.g.:
+
{{bc|# systemctl isolate graphical.target}}
+
This will only change the current runlevel, and has no effect on the next boot.}}
+
 
+
{{FAQ
+
|question=How do I boot into a different "runlevel"?
+
|answer=The standard target (which is what runlevels are called in a systemd world) is default.target, which is aliased by default to graphical.target (which roughly corresponds to the old runlevel 5). To change the default target at boot-time, append one of the following kernel parameters to your GRUB kernel line:
+
* systemd.unit=multi-user.target (which roughly corresponds to the old runlevel 3),
+
* systemd.unit=rescue.target (which roughly corresponds to the old runlevel 1).
+
Alternatively, you may leave the bootloader alone and change default.target. This can be done using systemctl:
+
{{bc|# systemctl enable multi-user.target}}
+
The effect of this command is outputted by systemctl; a symlink to the new default target is made at /etc/systemd/system/default.target. This works if and only if
+
  [Install]
+
  Alias=default.target
+
is in the target's configuration file. Currently, multi-user.target and graphical.target both have it.}}
+
 
+
{{FAQ
+
|question=How do I change the default runlevel/target to boot into?
+
|answer=The symlink {{ic|/etc/systemd/system/default.target}} controls where we boot into by default. Link it to the target unit of your choice. For example, like this:
+
{{bc|# systemctl -f enable multi-user.target}}
+
or
+
{{bc|# systemctl -f enable graphical.target}}}}
+
 
+
{{FAQ
+
|question=How do I know the current run level?
+
|answer=runlevel command still works with systemd. You can continue using that however runlevels is a legacy concept in systemd and is emulated via 'targets' and multiple targets can be active at the same time. So the equivalent in systemd terms is
+
{{bc|1=# systemctl list-units --type=target}}}}
+
  
 
{{FAQ
 
{{FAQ
 
|question=How do I make a custom unit file?
 
|question=How do I make a custom unit file?
|answer=The unit files in {{ic|/etc/systemd/system}} take precedence over the ones in {{ic|/usr/lib/systemd/system}}. To make your own version of a unit (which will not be destroyed by an upgrade), copy the old unit file from {{ic|/lib}} to {{ic|/etc}} and make your changes there.}}
+
|answer=The unit files in {{ic|/etc/systemd/system/}} take precedence over the ones in {{ic|/usr/lib/systemd/system/}}. To make your own version of a unit (which will not be destroyed by an upgrade), copy the old unit file from {{ic|/usr/lib/}} to {{ic|/etc/}} and make your changes there. Alternatively you can use {{ic|.include}} to parse an existing service file and then add new options. For example, if you simply want to add an additional dependency to a service file, you may use:
 +
{{hc|/etc/systemd/system/<service-name>.service|
 +
<nowiki>
 +
.include /usr/lib/systemd/system/<service-name>.service
  
 +
[Unit]
 +
Requires=<new dependency>
 +
After=<new dependency>
 +
</nowiki>}}
 +
}}
 
{{FAQ
 
{{FAQ
 
|question=How do I change the number of gettys running by default?
 
|question=How do I change the number of gettys running by default?
Line 344: Line 433:
 
{{FAQ
 
{{FAQ
 
|question=How do I get more verbose output during boot?
 
|question=How do I get more verbose output during boot?
|answer=By default systemd does not give much (if any) output during boot. Firstly, lots of output from services running in parallel would be very messy, and secondly, boot is supposed to be so fast that status messages would slow it down.
+
|answer=If you see no output at all in console after the initram message, this means you have the {{ic|quiet}} parameter in your kernel line. It's best to remove it, at least the first time you boot with systemd, to see if everythin is ok. Then, You will see a list [ OK ] in green or [ FAILED ] in red.
  
If you append the kernel parameter "verbose" to your kernel line in GRUB, you will get lots of output during boot. However, this is only really meant as a debugging tool as it is not very useful during normal use. Any messages are logged to the system log and if you want to find out about the status of your system
+
Any messages are logged to the system log and if you want to find out about the status of your system run {{ic|$ systemctl}} or look at the boot/system log with {{ic|journalctl}}.
{{bc|$ systemctl}}
+
}}
is your friend.}}
+
 
 +
{{FAQ
 +
|question=How to avoid the console to be cleared after boot ?
 +
|answer=Create a custom getty@tty1.service file
 +
 
 +
Copy /usr/lib/systemd/system/getty@.service to /etc/systemd/system/getty.target.wants/getty@tty1.service, and then edit the file:
 +
* add --noclear to the ExecStart line after agetty
 +
* switch TTYVTDisallocate to no
 +
}}
  
 
{{FAQ
 
{{FAQ
Line 357: Line 454:
  
 
{{bc|<nowiki>
 
{{bc|<nowiki>
CONFIG_DEVTMPFS=y
+
CONFIG_AUDIT=y (recommended)
 +
CONFIG_AUDIT_LOGINUID_IMMUTABLE=y (not required, may break sysvinit compat)
 
CONFIG_CGROUPS=y
 
CONFIG_CGROUPS=y
 +
CONFIG_IPV6=[y|m] (highly recommended)
 +
CONFIG_UEVENT_HELPER_PATH="" (if you don't use an initramfs)
 +
CONFIG_DEVTMPFS=y
 +
CONFIG_DEVTMPFS_MOUNT=y (recommended, if you don't use an initramfs)
 +
CONFIG_RTC_DRV_CMOS=y (highly recommended)
 +
CONFIG_FANOTIFY=y (required for readahead)
 
CONFIG_AUTOFS4_FS=[y|m]
 
CONFIG_AUTOFS4_FS=[y|m]
CONFIG_IPV6=[y|m], optional, but highly recommended
+
CONFIG_TMPFS_POSIX_ACL=y (recommended, if you want to use pam_systemd.so)
CONFIG_RTC_DRV_CMOS=y, optional, but highly recommended
+
</nowiki>}}}}
CONFIG_FANOTIFY=y, optional, required for systemd readahead.
+
CONFIG_UEVENT_HELPER_PATH should be empty, if you want to use systemd without initramfs
+
CONFIG_AUDIT=y optional, but recommended
+
CONFIG_TMPFS_POSIX_ACL=y recommended, if you want pam_systemd.so to setup your "seats"
+
CONFIG_AUDIT_LOGINUID_IMMUTABLE=y
+
</nowiki>}}
+
 
+
{{Note|Schedulers such as BFS which avoid cgroup facilities are unsupported by systemd.}}}}
+
  
 
{{FAQ
 
{{FAQ
 
|question=What other units does a unit depend on?
 
|question=What other units does a unit depend on?
|answer=For example, if you want to figure out which services a target like multi-user.target pulls in, use something like this:  
+
|answer=For example, if you want to figure out which services a target like {{ic|multi-user.target}} pulls in, use something like this:  
 
{{hc|$ systemctl show -p "Wants" multi-user.target|2=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 cups.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 netfs.service}}
 
{{hc|$ systemctl show -p "Wants" multi-user.target|2=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 cups.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 netfs.service}}
  
Instead of "Wants" you might also try "WantedBy", "Requires", "RequiredBy", "Conflicts", "ConflictedBy", "Before", "After" for the respective types of dependencies and their inverse.}}
+
Instead of {{ic|Wants}} you might also try {{ic|WantedBy}}, {{ic|Requires}}, {{ic|RequiredBy}}, {{ic|Conflicts}}, {{ic|ConflictedBy}}, {{ic|Before}}, {{ic|After}} for the respective types of dependencies and their inverse.}}
  
 
== 优化 ==
 
== 优化 ==
=== 精简输出信息 ===
+
=== systemd-analyze ===
Change 'verbose' to 'quiet' on the kernel line in GRUB. For some systems, particularly those with an SSD, the slow performance of the TTY is actually a bottleneck, and so less output means faster booting.
+
Systemd provides a tool called {{ic|systemd-analyze}} that allows you to analyze your boot process so you can see which unit files are causing your boot process to slow down. You can then optimize your system accordingly. You have to install {{Pkg|python2-dbus}} to use it.
  
=== Early start ===
+
To see how much time was spent in kernel-/userspace on boot, simply use:
One central feature of systemd is dbus and socket activation, this causes services to be started when they are first accessed, and is generally a good thing. However, if you know that a service (like console-kit) will always be started during boot, then the overall boot time might be reduced by starting it as early as possible. This can be achieved (if the service file is set up for it, which in most cases it is) by issuing:
+
systemd-analyze
 +
{{Tip|If you add the {{ic|timestamp}} hook to your {{ic|HOOKS}} array in {{ic|/etc/mkinitcpio.conf}} and rebuild your initramfs, {{ic|systemd-analyze}} will also be able to show you how much time was spent in the intramfs.}}
  
{{bc|# systemctl enable console-kit-daemon.service}}
+
To list the started unit files, sorted by the time each of them took to start up:
 +
systemd-analyze blame
  
This will cause systemd to start console-kit as soon as possible, without causing races with the socket or dbus activation.
+
You can also create a SVG file which describes your boot process grapically, similiar to [[Bootchart]]:
 +
systemd-analyze plot > plot.svg
  
=== Automount ===
+
==== 使用 bootchart ====
The default setup will fsck and mount all filesystems before starting most daemons and services. If you have a large {{ic|/home}} partition, it might be better to allow services that do not depend on {{ic|/home}} to start while {{ic|/home}} is being fsck'ed. This can be achieved by adding the following options to the fstab entry of your {{ic|/home}} partition
+
You can use a version of bootchart to visualize the boot sequence.
 +
Since you are not able to put a second init into the kernel cmdline you won't be able to use any of the standard bootchart setups. However the {{AUR|bootchart2}} package from [[AUR]] comes with an undocumented systemd service. After you've installed bootchart2 do:
 +
{{bc|# systemctl enable bootchart.service}}
 +
Read the [https://github.com/mmeeks/bootchart bootchart documentation] for further details on using this version of bootchart.
  
comment=systemd.automount
+
=== shell 短命令 ===
 +
Systemd daemon management requires a bit more text entry to accomplish tasks such as start, stopped, enabling, checking status, etc.  The following functions can be added one's {{ic|~/.bashrc}} to help streamline interactions with systemd and to improve the overall experience.
  
This will fsck and mount {{ic|/home}} when it is first accessed, and the kernel will buffer all file access to {{ic|/home}} until it is ready.
+
<pre>if ! systemd-notify --booted; then  # not using systemd
 +
  start() {
 +
    sudo rc.d start $1
 +
  }
  
=== /etc/mtab ===
+
  restart() {
{{note|As of filesystem-2011.12, /etc/mtab is now symlinked to /proc/self/mounts.}}  
+
    sudo rc.d restart $1
 +
  }
  
systemd requires that {{ic|/etc/mtab}} be a symlink to {{ic|/proc/self/mounts}}, or the following warning will be printed:
+
  stop() {
 +
    sudo rc.d stop $1
 +
  }
 +
else
 +
  start() {
 +
    sudo systemctl start $1.service
 +
  }
  
''/etc/mtab is not a symlink or not pointing to /proc/self/mounts. This is not supported anymore. Please make sure to replace this file by a symlink to avoid incorrect or misleading mount(8) output.''
+
  restart() {
 +
    sudo systemctl restart $1.service
 +
  }
  
Replace the file with a symlink with {{ic|ln}}:
+
  stop() {
 +
    sudo systemctl stop $1.service
 +
  }
  
{{bc|# ln -fs /proc/self/mounts /etc/mtab}}
+
  enable() {
 +
    sudo systemctl enable $1.service
 +
  }
  
Without doing this, features such as automounting through {{ic|/etc/fstab}} will be unavailable.
+
  status() {
 +
    sudo systemctl status $1.service
 +
  }
  
=== 禁用原生 mount ===
+
  disable() {
With v12 or later, you can disable the native mount and fsck facility in {{ic|/etc/systemd/system.conf}}.
+
    sudo systemctl disable $1.service
MountAuto=no
+
  }
SwapAuto=no
+
fi
{{Note|These options are enabled by default.}}
+
</pre>
  
=== 静态网络连接 ===
+
=== 精简输出信息 ===
Create /etc/conf.d/network file with following content:
+
Change {{ic|verbose}} to {{ic|quiet}} on the kernel line in GRUB. For some systems, particularly those with an SSD, the slow performance of the TTY is actually a bottleneck, and so less output means faster booting.
address=
+
netmask=
+
broadcast=
+
gateway=
+
Change a values as you need.
+
  
Then create network@.service file in /etc/systemd/system/
+
=== Early start ===
 +
One central feature of systemd is dbus and socket activation, this causes services to be started when they are first accessed, and is generally a good thing. However, if you know that a service (like console-kit) will always be started during boot, then the overall boot time might be reduced by starting it as early as possible. This can be achieved (if the service file is set up for it, which in most cases it is) by issuing:
  
[Unit]
+
{{bc|# systemctl enable console-kit-daemon.service}}
Description=Network Connectivity
+
Wants=network.target
+
Before=network.target
+
+
[Service]
+
Type=oneshot
+
RemainAfterExit=yes
+
EnvironmentFile=/etc/conf.d/network
+
ExecStart=/sbin/ip link set dev %I up
+
ExecStart=/sbin/ip addr add ${address}/${netmask} broadcast ${broadcast} dev %I
+
ExecStart=/sbin/ip route add default via ${gateway}
+
ExecStop=/sbin/ip addr flush dev %I
+
ExecStop=/sbin/ip link set dev %I down
+
+
[Install]
+
Alias=multi-user.target.wants/network@%I.service
+
  
Start service:
+
This will cause systemd to start console-kit as soon as possible, without causing races with the socket or dbus activation.
# systemctl start network@eth0.service
+
  
Add to start at boot:
+
=== Automount ===
# ln -sf /etc/systemd/system/network@.service /etc/systemd/system//multi-user.target.wants/network@eth0.service
+
The default setup will fsck and mount all filesystems before starting most daemons and services. If you have a large {{ic|/home}} partition, it might be better to allow services that do not depend on {{ic|/home}} to start while {{ic|/home}} is being fsck'ed. This can be achieved by adding the following options to the fstab entry of your {{ic|/home}} partition:
  
Replace eth0 with other device name if needed.
+
noauto,x-systemd.automount
  
=== Remote filesystem(NFS) mounts ===
+
This will fsck and mount {{ic|/home}} when it is first accessed, and the kernel will buffer all file access to {{ic|/home}} until it is ready.
If you have NFS mounts listed in {{ic|/etc/fstab}} then systemd will attempt to mount them but will typically do so too early (before networking has been configured). To get the timing correct we need to tell systemd explicitly that the mount depends on networking and on rpc.statd. To do this, create a file under {{ic|/etc/systemd/system}} named {{ic|<mount-unit-name>.mount}} with contents as follows.
+
 
+
[Unit]
+
Description=<mountpoint>
+
Wants=network.target rpc-statd.service
+
After=network.target rpc-statd.service
+
+
[Mount]
+
What=<server>:<share>
+
Where=<mountpoint>
+
Type=nfs
+
StandardOutput=syslog
+
StandardError=syslog
+
 
+
In the above
+
*mount-unit-name is the full path to the mountpoint in an escaped format. For example, a mount unit for {{ic|/usr/local}} must be named {{ic|usr-local.mount}}.
+
*mountpoint is the local mountpoint
+
*server:share specify the remote filesystem in the same manner as for {{ic|/etc/fstab}}
+
  
See systemd.unit(5) and systemd.mount(5) for further details.
+
If you have encrypted filesystems with keyfiles, you can also add the {{ic|noauto}} parameter to the corresponding entries in {{ic|/etc/crypttab}}. systemd will then not open the encrypted device on boot, but instead wait until it is actually accessed and then automatically open it with the specified keyfile before mounting it. This might save a few seconds on boot if you are using an encrypted RAID device for example, because systemd doesn't have to wait for the device to become available. For example:
 
+
{{hc|/etc/crypttab|data /dev/md0 /root/key noauto}}
A similar approach will probably be required for other remote filesystem types such as nfs4 and cifs.
+
 
+
Alternatively, you can mark these entries in {{ic|/etc/fstab}} with the option ''comment=systemd.automount''. Make sure that if you also include 'defaults' as a mount option, that you override the implicit 'auto' with 'noauto'. This will cause the device to be mounted on first access, similar to [[Autofs]].
+
  
 
=== Readahead ===
 
=== Readahead ===
 
systemd comes with its own readahead implementation, this should in principle improve boot time. However, depending on your kernel version and the type of your hard drive, your mileage may vary (i.e. it might be slower). To enable, do:
 
systemd comes with its own readahead implementation, this should in principle improve boot time. However, depending on your kernel version and the type of your hard drive, your mileage may vary (i.e. it might be slower). To enable, do:
  
{{bc|<nowiki># systemctl enable systemd-readahead-collect.service
+
{{bc|<nowiki># systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service</nowiki>}}
# systemctl enable systemd-readahead-replay.service</nowiki>}}
+
  
 
Remember that in order for the readahead to work its magic, you should reboot a couple of times.
 
Remember that in order for the readahead to work its magic, you should reboot a couple of times.
  
 
=== User sessions ===
 
=== User sessions ===
systemd can divide user sessions into cgroups. Add "session optional pam_systemd.so" to your relevant {{ic|/etc/pam.d}} files (e.g., login for tty logins, sshd for remote access, kde for password kdm logins, kde-np for automatic kdm logins).
+
systemd can divide user sessions into cgroups. Add {{ic|session optional pam_systemd.so}} to your relevant {{ic|/etc/pam.d/}} files (e.g., {{ic|login}} for tty logins, {{ic|sshd}} for remote access, {{ic|kde}} for password kdm logins, {{ic|kde-np}} for automatic kdm logins).
  
 
Before:
 
Before:
Line 505: Line 583:
 
[…]}}
 
[…]}}
  
=== Systemd 日志 ===
+
Further, you can replace [[ConsoleKit]]'s functionality with systemd. To do this, {{Pkg|polkit}} needs to be rebuilt from [[ABS]] with systemd enabled ({{ic|--enable-systemd}}), and stuff like USB automounting will work without consolekit. DBus supports systemd since version 1.6.0, so there's no longer need to build it from Git.
Since version 38 systemd has an own logging system.<br>
+
To activate it you need to uncheck:
+
{{ic|<nowiki> LogTarget=journal-or-kmsg</nowiki>}} in {{ic|/etc/systemd/system.conf}}.<br>
+
The next thing you have to do, except you want a volatile log in {{ic| /var/run/log/}}, is to create a directory named "journal" in {{ic| /var/log}} <br>
+
where systemd will log instead.
+
After a restart of systemd ({{ic|$ systemctl daemon-reexec}}) systemd will start logging.<br>
+
To read the log enter: {{ic|$ systemd-journalctl}} or {{ic|$ systemd-journalctl -a}} if you want to see unprintable characters and full lines instead of shortened lines.
+
  
 +
== Troubleshooting ==
 +
=== Shutdown/Reboot takes terribly long ===
 +
If the shutdown process takes a very long time (or seems to freeze) most likely a service not exiting is to blame. systemd waits some time for each service to exit before trying to kill it.
 +
To find out if you are affected see [http://fedoraproject.org/wiki/How_to_debug_Systemd_problems#Diagnosing_shutdown_problems this article] in the fedora wiki.
 +
==== SLiM and xfce-session ====
 +
One setup that can produce a shutdown freeze is Xfce in conjunction with SLiM: Shutting down/rebooting using xfce-session will cause slim.service to hang for half a minute until systemd kills it the hard way.
 +
One workaround is to create a modified slim.service:
 +
{{hc|/etc/systemd/system/slim.service|<nowiki>
 +
[Unit]
 +
Description=SLiM Simple Login Manager
 +
After=systemd-user-sessions.service
 +
 +
[Service]
 +
Type=forking
 +
PIDFile=/var/lock/slim.lock
 +
ExecStart=/usr/bin/slim -d
 +
ExecStop=/bin/kill -9 $MAINPID
 +
ExecStopPost=/bin/rm /var/lock/slim.lock
 +
 +
[Install]
 +
WantedBy=graphical.target</nowiki>}}
 +
This causes SLiM to be terminated using SIGKILL. Since the lock file is also removed this does not cause a problem.
 
== See also==
 
== See also==
 
*[http://www.freedesktop.org/wiki/Software/systemd Official Web Site]
 
*[http://www.freedesktop.org/wiki/Software/systemd Official Web Site]
 
*[http://0pointer.de/public/systemd-man/ Manual Pages]
 
*[http://0pointer.de/public/systemd-man/ Manual Pages]
 +
*[http://freedesktop.org/wiki/Software/systemd/Optimizations systemd Optimizations]
 
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions FAQ]
 
*[http://www.freedesktop.org/wiki/Software/systemd/FrequentlyAskedQuestions FAQ]
 
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks Tips And Tricks]
 
*[http://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks Tips And Tricks]
Line 525: Line 619:
 
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems How to debug Systemd problems]
 
*[http://fedoraproject.org/wiki/How_to_debug_Systemd_problems How to debug Systemd problems]
 
*[https://docs.google.com/document/pub?id=1IC9yOXj7j6cdLLxWEBAGRL6wl97tFxgjLUEHIX3MSTs Background information about systemd journal]
 
*[https://docs.google.com/document/pub?id=1IC9yOXj7j6cdLLxWEBAGRL6wl97tFxgjLUEHIX3MSTs Background information about systemd journal]
 +
*[http://www.h-online.com/open/features/Booting-up-Tools-and-tips-for-systemd-1570630.html Booting up: Tools and tips for systemd, a Linux init tool. In The H]

Revision as of 08:02, 14 July 2012

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary heading Template:Article summary text Template:Article summary text Template:Article summary text Template:Article summary text Template:Article summary text Template:Article summary text Template:Article summary end

安装

systemd 可以和 Arch 默认的启动系统(initscripts)共存,通过添加/删除内核参数 init=/bin/systemd 切换。马上开始试用 systemd:

  1. 从[core]仓库安装 systemd
  2. 在启动引导器添加内核参数 init=/bin/systemd
  3. (可选)如果希望完全使用 systemd,可以移除 initscripts 并安装 systemd-sysvcompat。删除 sysvinit 后,由该包提供一些基本命令(initreboot 等)的链接。之后,可以删除上一步添加的内核参数。
  4. 建议安装 systemd-arch-units,该包提供了常用系统服务的 systemd 原生服务文件。
  5. 启用原来 rc.conf 中配置的自启动服务,systemd 无法完全兼容该文件。
警告: udev 和很多其他系统软件要求 /usr 在启动早期就挂载。如果 /usr 和根目录不在同一分区,那么必须做一些额外设置。参见 mkinitcpio相关页面 以及 freedesktop.org#separate-usr-is-broken freedesktop.org的相关页面

原生 systemd 配置文件

如果下列文件不存在,systemd 会从 /etc/rc.conf 读取相关配置。

小贴士: 某些文件可能需要手动创建。

主机名

设置主机名为“myhostname”:

/etc/hostname
myhostname

控制台和键盘布局

/etc/vconsole.conf 文件用来配置虚拟控制台,包括键盘映射和控制台字体:

/etc/vconsole.conf
KEYMAP=us
FONT=lat9w-16
FONT_MAP=8859-1_to_uni

Locale

更多内容参阅 man locale.conf

/etc/locale.conf
LANG=en_US.UTF-8
LC_COLLATE=C

时区

更多内容参阅 man 5 timezone

/etc/timezone
Asia/Shanghai
注意: 设置该文件后仍需配置 /etc/localtime

硬件时钟

systemd 默认识别硬件时钟为协调世界时(UTC),也推荐这样设置。处理夏令时有些麻烦,如果夏令时调整发生在关机时,下次启动时时间会出现问题(更多信息)。最新的内核直接从实时时钟芯片(RTC)读取时间,不使用 hwclock,内核把从 RTC 读取的时间当作 UTC 处理。所以如果硬件时间是地方时,系统启动一开始识别的时间是错误的,之后很快会进行矫正。这可能导致一些问题(尤其是时间倒退时)。

如果同时安装了 Windows 操作系统(默认使用地方时),那么一般 RTC 会被设置为地方时。Windows 其实也能处理 UTC,在注册表中设置下列DWORD键值为1即可:

HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\RealTimeIsUniversal
警告: 在最新的 Windows 7、 Vista SP2 上,此设置会导致网络时间同步无法使用。而早期的版本中,此设置会导致系统从睡眠、待机唤醒后出现故障。此外,Windows 还可能因此无法处理夏令时调整

如果不想因此弄乱 Windows,systemd 也可以接受本地时:

/etc/adjtime
 
0.0 0.0 0.0
0
LOCAL
注意: The other parameters are still needed but are ignored by systemd. (译者不理解……)
注意: 推荐使用NTP服务来确保硬件时钟同步。

开机加载内核模块

systemd 读取 /etc/modules-load.d/ 中的配置,加载内核模块。配置文件名称通常为 /etc/modules-load.d/<program>.conf。格式很简单,一行一个要读取的模块名,而空行以及第一个非空格字符为#;的行会被忽略,如:

/etc/modules-load.d/virtio-net.conf
# Load virtio-net.ko at boot
virtio-net

另见:Kernel modules (简体中文)#选项

禁用内核模块

禁用内核模块的方法和 Arch 默认的 initscripts 相同,因为该过程实际由 kmod 处理。参见:Kernel modules (简体中文)#黑名单

临时文件

Systemd-tmpfiles uses the configuration files in /etc/tmpfiles.d/ to describe the creation, cleaning and removal of volatile and temporary files and directories which usually reside in directories such as /run or /tmp. Each configuration file is named in the style of /etc/tmpfiles.d/<program>.conf. This will also override any files in /usr/lib/tmpfiles.d/ with the same name.

tmpfiles are usually provided together with service files to create directories which are expected to exist by certain daemons. For example the Samba daemon expects the directory /var/run/samba to exist and to have the correct permissions. The corresponding tmpfile looks like this:

/usr/lib/tmpfiles.d/samba.conf
D /var/run/samba 0755 root root

However, tmpfiles may also be used to write values into certain files on boot. For example, if you use /etc/rc.local to disable wakeup from USB devices with echo USBE > /proc/acpi/wakeup, you may use the following tmpfile instead:

/etc/tmpfiles.d/disable-usb-wake.conf
w /proc/acpi/wakeup - - - - USBE

The tmpfiles method is recommended in this case since systemd doesn't actually support /etc/rc.local.

See man tmpfiles.d for details.

挂载网络文件系统

systemd automatically makes sure that remote filesystem mounts like NFS or Samba are only started after the network has been set up. Therefore remote filesystem mounts specified in /etc/fstab should work out of the box.

You may however want to use Automount for remote filesystem mounts to mount them only when there are being accessed. Furthermore you can use the x-systemd.device-timeout=# option in /etc/fstab to specify a timeout in case the network resource is not available.

See man systemd.mount for details.

用 systemd 替代 acpid

Systemd can handle some power-related ACPI events. This is configured via the following options in /etc/systemd/logind.conf:

  • HandlePowerKey : Power off the system when the power button is pressed
  • HandleSleepKey : Suspend the system when the sleep key is pressed
  • HandleLidSwitch : Suspend the system when the laptop lid is closed

Depending on the value of these options, these events may for example only be triggered when no user is logged in (no-session) or when only a single user session is active (any-session). See man logind.conf for details.

These options should not be used on desktop environments like Gnome and XFCE since these handle ACPI events by themselves. However, on systems which run no graphical setup or only a simple window manager like i3 or awesome, this may replace the acpid daemon which is usually used to react to these ACPI events.

单元(unit)

A unit configuration file encodes information about a service, a socket, a device, a mount point, an automount point, a swap file or partition, a start-up target, a file system path or a timer controlled and supervised by systemd. The syntax is inspired by XDG Desktop Entry Specification .desktop files, which are in turn inspired by Microsoft Windows .ini files. See man systemd.unit for more info.

systemd 命令

  • systemctl: used to introspect and control the state of the systemd system and service manager.
  • systemd-cgls: recursively shows the contents of the selected Linux control group hierarchy in a tree
  • systemadm: a graphical frontend for the systemd system and service manager that allows introspection and control of systemd (avaiable via the systemd-ui-gitAUR package from the AUR).

View the man pages for more details.

Tip: You can use all of the following systemctl commands with the -H <user>@<host> switch to control a systemd instance on a remote machine. This will use SSH to connect to the remote systemd instance.

评测系统状态

List running units:

$ systemctl

or:

$ systemctl list-units

List failed units:

$ systemctl --failed

The available unit files can be seen in /usr/lib/systemd/system/ and /etc/systemd/system/ (the latter takes precedence).

使用单元

Units can be services (.service), mount points (.mount) or sockets (.sockets). When using systemctl, you always have to specify the complete name of the unit file, including its suffix, for example netcfg.service. See man systemd.unit for details.

Activate a unit immediately:

# systemctl start <unit>

Deactivate a unit immediately:

# systemctl stop <unit>

Restart a unit:

# systemctl restart <unit>

Ask a unit to reload its configuration:

# systemctl reload <unit>

Show the status of a unit, including whether it is running or not:

$ systemctl status <unit>

Check whether a unit is already enabled or not:

$ systemctl is-enabled <unit>

Enable a unit to be started on bootup:

# systemctl enable <unit>

Disable a unit to not start during bootup:

# systemctl disable <unit>

Show the manual page associated with a unit (this has to be supported by the unit file):

$ systemctl help <unit>

电源管理

If you are in a local user session and no other session is active, the following commands will work without root privileges. If not, systemd will automatically ask you for the root password.

Shut down and reboot the system:

$ systemctl reboot

Shut down and power-off the system:

$ systemctl poweroff

Shut down and halt the system:

$ systemctl halt

Suspend the system:

$ systemctl suspend

Hibernate the system:

$ systemctl hibernate

启动级别(runlevel)/目标(target)

Runlevels is a legacy concept in systemd. Systemd uses targets which serve a similar purpose as runlevels but act a little different. Each target is named instead of numbered and is intended to serve a specific purpose with the possibility of having multiple ones active at the same time. Some targets are implemented by inheriting all of the services of another target and adding additional services to it. There are systemd targets that mimic the common SystemVinit runlevels so you can still switch targets using the familiar telinit RUNLEVEL command.

获取当前启动级别/目标

The following should be used under systemd instead of runlevel:

# systemctl list-units --type=target

创建新目标

The runlevels that are assigned a specific purpose on vanilla Fedora installs; 0, 1, 3, 5, and 6; have a 1:1 mapping with a specific systemd target. Unfortunately, there is no good way to do the same for the user-defined runlevels like 2 and 4. If you make use of those it is suggested that you make a new named systemd target as /etc/systemd/system/<your target> that takes one of the existing runlevels as a base (you can look at /usr/lib/systemd/system/graphical.target as an example), make a directory /etc/systemd/system/<your target>.wants, and then symlink the additional services from /usr/lib/systemd/system/ that you wish to enable.

目标表

SysV Runlevel Systemd Target Notes
0 runlevel0.target, poweroff.target Halt the system.
1, s, single runlevel1.target, rescue.target Single user mode.
2, 4 runlevel2.target, runlevel4.target, multi-user.target User-defined/Site-specific runlevels. By default, identical to 3.
3 runlevel3.target, multi-user.target Multi-user, non-graphical. Users can usually login via multiple consoles or via the network.
5 runlevel5.target, graphical.target Multi-user, graphical. Usually has all the services of runlevel 3 plus a graphical login.
6 runlevel6.target, reboot.target Reboot
emergency emergency.target Emergency shell

切换启动级别/目标

In systemd runlevels are exposed via "target units". You can change them like this:

# systemctl isolate graphical.target

This will only change the current runlevel, and has no effect on the next boot.

修改默认启动级别/目标

The standard target is default.target, which is aliased by default to graphical.target (which roughly corresponds to the old runlevel 5). To change the default target at boot-time, append one of the following kernel parameters to your bootloader:

  • systemd.unit=multi-user.target (which roughly corresponds to the old runlevel 3),
  • systemd.unit=rescue.target (which roughly corresponds to the old runlevel 1).

Alternatively, you may leave the bootloader alone and change default.target. This can be done using systemctl:

# systemctl enable multi-user.target

The effect of this command is outputted by systemctl; a symlink to the new default target is made at /etc/systemd/system/default.target. This works if, and only if:

[Install]
Alias=default.target

is in the target's configuration file. Currently, multi-user.target and graphical.target both have it.

启动桌面环境

使用登录管理器

To enable graphical login, run your preferred Display Manager daemon (e.g. KDM). At the moment, service files exist for GDM, KDM, SLiM, XDM and LXDM.

# systemctl enable kdm.service

This should work out of the box. If not, you might have a default.target set manually or from a older install:

# ls -l /etc/systemd/system/default.target
/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target

Simply delete the symlink and systemd will use its stock default.target (i.e. graphical.target).

# rm /etc/systemd/system/default.target

If /etc/locale.conf is used for setting the locale, add an entry to /etc/environment:

/etc/environment
LANG=en_US.utf8

使用服务文件

Note: Using this method there will be no PAM session created for your user. Therefore ConsoleKit (which gives you access to shutdown/reboot, audio devices etc.) will not work properly. For the recommended way, see: Automatic_login_to_virtual_console#With_systemd.

If you are only looking for a simple way to start X directly without a display manager, you can create a service file similar to this:

/etc/systemd/system/graphical.target.wants/xinit.service
[Unit]
Description=Direct login to X
After=systemd-user-sessions.service

[Service]
ExecStart=/bin/su <username> -l -c "/bin/bash --login -c xinit"

[Install]
WantedBy=graphical.target

systemd 日志

Since version 38 systemd has an own logging system, the journal.

By default, running a syslog daemon is no longer required. To read the log, use:

# journalctl

The journal writes to /run/systemd/journal, meaning logs will poof on reboot. For non-volatile logs, create /var/log/journal/:

# mkdir /var/log/journal/

日志大小限制

If the journal is made non-volatile, its size limit is set to a default value of 10% of the size of the respective file system. E.g. with /var/log/journal located on a 50GiB root partition this would lead to 5GiB of journal data. The maximum size of the persistent journal can be controlled by SystemMaxUse in /etc/systemd/journald.conf, so to limit it for example to 50MiB uncomment and edit the corresponding line to:

SystemMaxUse=50M

Look at man journald.conf for more info.

配合传统日志服务

Compatibility with classic syslog implementations is provided via a socket /run/systemd/journal/syslog, to which all messages are forwarded. To make the syslog daemon work with the journal, it has to bind to this socket instead of /dev/log (official announcement). For syslog-ng change /etc/syslog-ng/syslog-ng.conf source section to:

source src {
    unix-dgram("/run/systemd/journal/syslog");
    internal();
    file("/proc/kmsg");
};

and enable (or reenable) syslog-ng:

# systemctl enable syslog-ng.service

By default, journald is configured to read from /proc/kmsg, but this will collide with a syslog implementation doing the same (systemd-devel post). Disable reading /proc/kmsg by systemd-journald in /etc/systemd/journald.conf:

ImportKernel=no

网络

DHCP 动态获取 IP

If you simply want to use DHCP for your ethernet connection, you can use dhcpcd@.service from the systemd-arch-units package. To enable DHCP for eth0, simply use:

# systemctl start dhcpcd@eth0.service

You can enable the service to automatically start at boot with:

# systemctl enable dhcpcd@.service

Note that this will enable the service for eth0 by default. If you want to use another interface, you have to create the symlink manually, e.g.:

# ln -s '/usr/lib/systemd/system/dhcpcd@.service' '/etc/systemd/system/multi-user.target.wants/dhcpcd@eth1.service'

其他配置方式

For static, wireless or advanced network configuration like bridging you can use netcfg or NetworkManager which both provide systemd service files.

If you need a static ethernet configuration, but don't want to use netcfg, there is a custom service file available on the Systemd/Services page.

Arch 整合

Integration with Arch's classic configuration is accomplished via the initscripts-systemd package. This is an optional package containing unit files and scripts needed to emulate Arch's initscripts, which can be used to ease the transition from sysVinit to systemd.

/etc/inittab is not used at all.

/etc/rc.local and /etc/rc.local.shutdown can be run at startup and shutdown by enabling rc-local.service and rc-local-shutdown.service.

Warning: Usage of this package is not recommended. In particular, arch-load-modules.service and arch-daemons.target are unsupported as a long-term solution and will be removed in the future. When ever possible, use native systemd configuration files instead.

Most people will not need all (if any) of these units, and they can be easily disabled with:

# systemctl disable <unitfile>

The plan is to remove most of the functionality from this package as soon as it is handled elsewhere (mostly in udev/systemd/kernel).

rc.conf

Some variables in /etc/rc.conf are respected by this glue work. For a pure systemd setup it is recommended to use the native systemd configuration files which will take precedence over /etc/rc.conf.

Supported variables:

  • LOCALE
  • KEYMAP
  • CONSOLEFONT
  • CONSOLEMAP
  • HOSTNAME
  • MODULES
  • DAEMONS: Ordering and blacklisting is respected, if a native systemd service file by the same name as a daemon exists, it will take precedence, this logic can be disabled by systemctl disable arch-daemons.target

Not supported variables and systemd configuration:

  • TIMEZONE: Please symlink /etc/localtime to your zoneinfo file manually.
  • HARDWARECLOCK: See Hardware clock time.
  • USELVM: use lvm.service provided by systemd-arch-units instead.
  • USECOLOR

The following is a brief description of the functionality of each of them. Alternative solutions are provided as a migration plan away from the functionality provided by this package.

rc-local.service / rc-local-shutdown.service

Runs /etc/rc.local (resp., /etc/rc.local.shutdown) on boot (resp., shutdown).

arch-daemons.target

Parses the DAEMONS array in /etc/rc.conf and starts the services. If a native systemd unit exists (by the same name) for a given daemon, this is used; otherwise, the script in /etc/rc.d/ is used to control the unit.

Alternative: use native unit files from the systemd-arch-units package.

arch-modules-load.service

Creates a list of modules to be loaded based on /etc/rc.conf (see /etc/modules-load.d/rc.conf).

Alternative: create a *.conf for your modules in /etc/modules-load.d/.

FAQ

For an up-to-date list of known issues, look at the upstream TODO.

Template:FAQ

Template:FAQ

Template:FAQ Template:FAQ

Template:FAQ

Template:FAQ

Template:FAQ

Template:FAQ

优化

systemd-analyze

Systemd provides a tool called systemd-analyze that allows you to analyze your boot process so you can see which unit files are causing your boot process to slow down. You can then optimize your system accordingly. You have to install python2-dbus to use it.

To see how much time was spent in kernel-/userspace on boot, simply use:

systemd-analyze
Tip: If you add the timestamp hook to your HOOKS array in /etc/mkinitcpio.conf and rebuild your initramfs, systemd-analyze will also be able to show you how much time was spent in the intramfs.

To list the started unit files, sorted by the time each of them took to start up:

systemd-analyze blame

You can also create a SVG file which describes your boot process grapically, similiar to Bootchart:

systemd-analyze plot > plot.svg

使用 bootchart

You can use a version of bootchart to visualize the boot sequence. Since you are not able to put a second init into the kernel cmdline you won't be able to use any of the standard bootchart setups. However the bootchart2AUR package from AUR comes with an undocumented systemd service. After you've installed bootchart2 do:

# systemctl enable bootchart.service

Read the bootchart documentation for further details on using this version of bootchart.

shell 短命令

Systemd daemon management requires a bit more text entry to accomplish tasks such as start, stopped, enabling, checking status, etc. The following functions can be added one's ~/.bashrc to help streamline interactions with systemd and to improve the overall experience.

if ! systemd-notify --booted; then  # not using systemd
  start() {
    sudo rc.d start $1
  }

  restart() {
    sudo rc.d restart $1
  }

  stop() {
    sudo rc.d stop $1
  }
else
  start() {
    sudo systemctl start $1.service
  }

  restart() {
    sudo systemctl restart $1.service
  }

  stop() {
    sudo systemctl stop $1.service
  }

  enable() {
    sudo systemctl enable $1.service
  }

  status() {
    sudo systemctl status $1.service
  }

  disable() {
    sudo systemctl disable $1.service
  }
fi

精简输出信息

Change verbose to quiet on the kernel line in GRUB. For some systems, particularly those with an SSD, the slow performance of the TTY is actually a bottleneck, and so less output means faster booting.

Early start

One central feature of systemd is dbus and socket activation, this causes services to be started when they are first accessed, and is generally a good thing. However, if you know that a service (like console-kit) will always be started during boot, then the overall boot time might be reduced by starting it as early as possible. This can be achieved (if the service file is set up for it, which in most cases it is) by issuing:

# systemctl enable console-kit-daemon.service

This will cause systemd to start console-kit as soon as possible, without causing races with the socket or dbus activation.

Automount

The default setup will fsck and mount all filesystems before starting most daemons and services. If you have a large /home partition, it might be better to allow services that do not depend on /home to start while /home is being fsck'ed. This can be achieved by adding the following options to the fstab entry of your /home partition:

noauto,x-systemd.automount

This will fsck and mount /home when it is first accessed, and the kernel will buffer all file access to /home until it is ready.

If you have encrypted filesystems with keyfiles, you can also add the noauto parameter to the corresponding entries in /etc/crypttab. systemd will then not open the encrypted device on boot, but instead wait until it is actually accessed and then automatically open it with the specified keyfile before mounting it. This might save a few seconds on boot if you are using an encrypted RAID device for example, because systemd doesn't have to wait for the device to become available. For example:

/etc/crypttab
data /dev/md0 /root/key noauto

Readahead

systemd comes with its own readahead implementation, this should in principle improve boot time. However, depending on your kernel version and the type of your hard drive, your mileage may vary (i.e. it might be slower). To enable, do:

# systemctl enable systemd-readahead-collect.service systemd-readahead-replay.service

Remember that in order for the readahead to work its magic, you should reboot a couple of times.

User sessions

systemd can divide user sessions into cgroups. Add session optional pam_systemd.so to your relevant /etc/pam.d/ files (e.g., login for tty logins, sshd for remote access, kde for password kdm logins, kde-np for automatic kdm logins).

Before:

$ systemd-cgls systemd:/system/getty@.service
systemd:/system/getty@.service:
├ tty5
│ └ 904 /sbin/agetty tty5 38400
├ tty2
│ ├ 13312 /bin/login --
│ └ 15765 -zsh
[…]

After:

$ systemd-cgls systemd:/user/example/
systemd:/user/example/:
├ 4
│ ├   902 /bin/login --
│ └ 16016 -zsh
[…]

Further, you can replace ConsoleKit's functionality with systemd. To do this, polkit needs to be rebuilt from ABS with systemd enabled (--enable-systemd), and stuff like USB automounting will work without consolekit. DBus supports systemd since version 1.6.0, so there's no longer need to build it from Git.

Troubleshooting

Shutdown/Reboot takes terribly long

If the shutdown process takes a very long time (or seems to freeze) most likely a service not exiting is to blame. systemd waits some time for each service to exit before trying to kill it. To find out if you are affected see this article in the fedora wiki.

SLiM and xfce-session

One setup that can produce a shutdown freeze is Xfce in conjunction with SLiM: Shutting down/rebooting using xfce-session will cause slim.service to hang for half a minute until systemd kills it the hard way. One workaround is to create a modified slim.service:

/etc/systemd/system/slim.service
[Unit]
Description=SLiM Simple Login Manager
After=systemd-user-sessions.service

[Service]
Type=forking
PIDFile=/var/lock/slim.lock
ExecStart=/usr/bin/slim -d
ExecStop=/bin/kill -9 $MAINPID
ExecStopPost=/bin/rm /var/lock/slim.lock

[Install]
WantedBy=graphical.target

This causes SLiM to be terminated using SIGKILL. Since the lock file is also removed this does not cause a problem.

See also