systemd (简体中文)

From ArchWiki
Revision as of 08:02, 14 July 2012 by Cuihao (Talk | contribs)

Jump to: navigation, search

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的相关页面

原生 systemd 配置文件

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

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





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



更多内容参阅 man locale.conf



更多内容参阅 man 5 timezone

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


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

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

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

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

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


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

# Load virtio-net.ko at boot

另见: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:

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:

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.


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


$ 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


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/ 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, Halt the system.
1, s, single, Single user mode.
2, 4,, User-defined/Site-specific runlevels. By default, identical to 3.
3, Multi-user, non-graphical. Users can usually login via multiple consoles or via the network.
5, Multi-user, graphical. Usually has all the services of runlevel 3 plus a graphical login.
6, Reboot
emergency Emergency shell


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

# systemctl isolate

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


The standard target is, which is aliased by default to (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:

  • (which roughly corresponds to the old runlevel 3),
  • (which roughly corresponds to the old runlevel 1).

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

# systemctl enable

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


is in the target's configuration file. Currently, and 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 set manually or from a older install:

# ls -l /etc/systemd/system/
/etc/systemd/system/ -> /usr/lib/systemd/system/

Simply delete the symlink and systemd will use its stock (i.e.

# rm /etc/systemd/system/

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



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:

Description=Direct login to X

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


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:


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 {

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:



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


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


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:

  • 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

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.

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

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.


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


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



Template:FAQ Template:FAQ







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:

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


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.


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:


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:

data /dev/md0 /root/key noauto


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


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


$ systemd-cgls 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.


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:

Description=SLiM Simple Login Manager

ExecStart=/usr/bin/slim -d
ExecStop=/bin/kill -9 $MAINPID
ExecStopPost=/bin/rm /var/lock/slim.lock


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

See also