systemd (简体中文)

From ArchWiki
Revision as of 14:34, 3 April 2012 by Eshizhan (talk | contribs) (Created page with "{{i18n|Systemd}} fr:Systemd Category: Daemons and system services (简体中文) Category:Boot process (简体中文) Category:简体中文 {{Lowercase title}}...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어

External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

systemd 是一款Linux系统和服务管理器,它与SysV和LSB的init脚本兼容。systemd 提供并行化的socket和D-Bus启动过程,按需启动daemon,保留了Linux cgroups进程的追踪功能,支持快照和系统状态恢复,支持维护挂载和自挂载点,实现了基于依赖关系的精细的服务控制逻辑。它能够替代sysvinit系统工作。

具体内容参考作者Lennart的Blog,上面有加长版介绍,之后还有这两篇 status updates,和Why systemd?。也可以参考维基百科项目主页.



  • 从[extra]中安装 systemd 以及依赖项
  • 添加 init=/bin/systemd 到你引导程序的kernel命令行上
Note: 如果你使用Grub2,请把内核参数添加到 /etc/default/grub - GRUB_CMDLINE_LINUX="..."
Tip: systemd 能够同标准的Arch Linux initscripts一起使用,通过添加或删除 init=/bin/systemd 内核参数来切换他们。
  • 要获得用systemd方式启动服务的优势,你可能还需要安装 systemd-arch-units 包。
Warning: systemd expects /usr to be mounted and available at bootup. If your /usr is on a separate partition, you will need to make accommodations to mount it from the initramfs and unmount it from a pivoted root on shutdown. See upstream's article: separate-usr-is-broken


  • systemctl: 用作内省和控制 systemd 系统和服务管理器的状态。
  • systemd-cgls: 以树形递归显示选中的 Linux 控制组结构层次。
  • systemadm: 一个 systemd 系统和服务管理器的图形化前端。



$ systemctl


$ systemctl list-units

所有可用的服务或单元在 /lib/systemd/system/etc/systemd/system 中(后者优先),也可使用:

$ systemctl -a


# systemctl start <service>


# systemctl stop <service>


# systemctl restart <service>


# systemctl reload <service>


# systemctl status <service>


# systemctl enable <service>


# systemctl disable <service>


Notice that you need to use the full name of a service file. E.g., in order to restart the avahi daemon, issue:

# systemctl restart avahi-daemon.service

Shut down and reboot the system

# systemctl reboot


Systemd has a concept of 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. 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 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's 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/$YOURTARGET that takes one of the existing runlevels as a base (you can look at /lib/systemd/system/ as an example), make a directory /etc/systemd/system/$YOURTARGET.wants, and then symlink the additional services that you want to enable into that directory. (The service unit files that you symlink live in /lib/systemd/system).

SystemVinit 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

Changing runlevels:

SystemVinit Command Systemd Command Notes
telinit 3 systemctl isolate (OR systemctl isolate OR telinit 3) Change to multi-user run level.
sed s/^id:.*:initdefault:/id:3:initdefault:/ ln -sf /lib/systemd/system/ /etc/systemd/system/ Set to use multi-user runlevel on next reboot.

Running DEs under systemd

To enable graphical login, run your preferred Display Manager daemon (e.g. KDM). At the moment, service files exist for gdm, kdm and slim, but there isn't one for xdm.

# 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/ -> /lib/systemd/system/

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

# rm /etc/systemd/system/

On KDE start an error message will appear saying "console-kit-daemon.unit" could not be found. To solve this problem, install systemd-arch-units.

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


and make the display manager read it by adding the entry auth required to /etc/pam.d/kde:

auth        required
auth        required
auth        required
account     required
password    required
session     required
session     required
-session    optional

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
After=dev-tty7.device systemd-user-sessions.service

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


Arch integration

Integration with Arch's classic configuration is accomplished via the initscripts-systemd package. This is an optional package 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.

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 (such as /etc/locale.conf, /etc/vconsole.conf, /etc/hostname, /etc/modules-load.d/*.conf) which will take precedence over /etc/rc.conf.


  • 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

  • TIMEZONE (symlink /etc/localtime to your zoneinfo file manually)
  • HARDWARECLOCK (use 'hwclock --systohc --utc' to set your hardware clock to utc, localtime is not supported, see FAQ)
  • USELVM (use lvm.service provided by systemd-arch-units instead)

Initscripts package

initscripts-systemd contains unit files and scripts that are needed to emulate Arch's initscripts.

Warning: Unless you require the functionality from lvm.service or dmraid.service, usage of this package is not recommended. In particular, arch-persistent-settings.service and are unsupported as a long term solution and will be removed in the future.

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

# systemctl disable <unitfile>

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

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.

  • lvm.service
Copies Arch's handling of LVM. Only needed if you use non-root LVM. In the future systemd will probably deal with this natively (in a much cleaner and more robust way).
  • rc-local.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.
  • arch-persistent-settings.service
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 /etc/rc.conf
    • alternative: Create /etc/localtime as a symlink to your timezone file in /usr/share/zoneinfo.
  • updates module blacklists based on /etc/rc.conf (see /etc/modprobe.d/rc.conf)
    • alternative: Create a differently named copy of this file in /etc/modprobe.d.
  • updates list of modules to be loaded based on /etc/rc.conf (see /etc/modules-load.d/rc.conf)
    • alternative: Create a differently named copy of this file in /etc/modules-load.d.

Native systemd configuration files

Instead of arch-persistent-settings.service you may configure some system settings with the native configuration files:

  • Add a hostname
  • Console and keymap settings
The /etc/vconsole.conf file configures the virtual console, i.e. keyboard mapping and console font.
  • OS info
/etc/os-release contains data that is defined by the operating system vendor and should not be changed by the administrator.
  • Locale settings (read man locale.conf for more options)
  • Configure kernel modules to load during boot
systemd uses /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 /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:
# Load virtio-net.ko at boot

See also Modprobe#Options

  • Configure kernel modules blacklist
systemd uses /etc/modprobe.d/ to configure the blacklisting of kernel modules. Each configuration file is named in the style of /etc/modprobe.d/<program>.conf. Empty lines and lines whose first non-whitespace character is # or ; are ignored. Example:
blacklist snd_hda_intel


install snd_hda_intel /bin/false

See also Modprobe#Blacklisting

  • Describe temporary files
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.

Helping out

Currently, systemd is mostly at feature parity with Arch's initscripts. However, a lot more testing is needed. If you'd like to help out, you can fork the initscripts-systemd or systemd-arch-units git repos and submit pull requests for your additions.

If you have any questions, ask in the thread in the Arch forums.


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














Less output

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.


Note: As of filesystem-2011.12, /etc/mtab is now symlinked to /proc/self/mounts.

systemd requires that /etc/mtab be a symlink to /proc/self/mounts, or the following warning will be printed:

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

Replace the file with a symlink with ln:

# ln -fs /proc/self/mounts /etc/mtab

Without doing this, features such as automounting through /etc/fstab will be unavailable.

Disabling native mount

With v12 or later, you can disable the native mount and fsck facility in /etc/systemd/system.conf.

Note: These options are enabled by default.

Static network

Create /etc/conf.d/network file with following content:


Change a values as you need.

Then create network@.service file in /etc/systemd/system/

Description=Network Connectivity

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


Start service:

# systemctl start network@eth0.service

Add to start at boot:

# ln -sf /etc/systemd/system/network@.service /etc/systemd/system//

Replace eth0 with other device name if needed.

Remote filesystem mounts

If you have NFS mounts listed in /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 /etc/systemd/system named <mount-unit-name>.mount with contents as follows.

Description=<mountpoint> rpc-statd.service rpc-statd.service 


In the above

  • mount-unit-name is the full path to the mountpoint in an escaped format. For example, a mount unit for /usr/local must be named usr-local.mount.
  • mountpoint is the local mountpoint
  • server:share specify the remote filesystem in the same manner as for /etc/fstab

See systemd.unit(5) and systemd.mount(5) for further details.

A similar approach will probably be required for other remote filesystem types such as nfs4 and cifs.

Alternatively, you can mark these entries in /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.


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
# systemctl enable 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

Systemd Journal

Since version 38 systemd has an own logging system.
To activate it you need to uncheck: LogTarget=journal-or-kmsg in /etc/systemd/system.conf.
The next thing you have to do, except you want a volatile log in /var/run/log/, is to create a directory named "journal" in /var/log
where systemd will log instead. After a restart of systemd ($ systemctl daemon-reexec) systemd will start logging.
To read the log enter: $ systemd-journalctl or $ systemd-journalctl -a if you want to see unprintable characters and full lines instead of shortened lines.

See also