该文档目的在于帮助系统管理员了解 systemd 中可以取代原先 sysvinit 工作流程的命令。如果想要了解 systemd 的一般信息,参阅 systemd。
注意 'service' 和 'chkconfig' 命令在 systemd 环境下依然可用,该教程目的在于告诉您如何使用原生 systemctl 替代品。
服务
sysvinit 命令 | systemd 命令 | 备注 |
---|---|---|
service frobozz start | systemctl start frobozz.service | 用来启动一个服务 (并不会重启现有的) |
service frobozz stop | systemctl stop frobozz.service | 用来停止一个服务 (并不会重启现有的)。 |
service frobozz restart | systemctl restart frobozz.service | 用来停止并启动一个服务。 |
service frobozz reload | systemctl reload frobozz.service | 当支持时,重新装载配置文件而不中断等待操作。 |
service frobozz condrestart | systemctl condrestart frobozz.service | 如果服务正在运行那么重启它。 |
service frobozz status | systemctl status frobozz.service | 汇报服务是否正在运行。 |
ls /etc/rc.d/init.d/ | ls /lib/systemd/system/*.service /etc/systemd/system/*.service | 用来列出可以启动或停止的服务列表。 |
chkconfig frobozz on | systemctl enable frobozz.service | 在下次启动时或满足其他触发条件时设置服务为启用 |
chkconfig frobozz off | systemctl disable frobozz.service | 在下次启动时或满足其他触发条件时设置服务为禁用 |
chkconfig frobozz | systemctl is-enabled frobozz.service | 用来检查一个服务在当前环境下被配置为启用还是禁用。 |
chkconfig frobozz --list | ls /etc/systemd/system/*.wants/frobozz.service | 用来列出该服务在哪些运行级别下启用和禁用。 |
chkconfig frobozz --add | 不需要,没有等效命令。 |
注意以上列出的所有 /sbin/service 和 /sbin/chkconfig 在 systemd 环境下依然可以工作,并且在必要的情况下将会被翻译成原生的等效命令。唯一的例外是 chkconfig --list。
Runlevels/targets
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 sysvinit 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/graphical.target
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
).
sysvinit 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 |
Changing runlevels:
sysvinit Command | systemd Command | Notes |
---|---|---|
telinit 3 | systemctl isolate multi-user.target (OR systemctl isolate runlevel3.target OR telinit 3) | Change to multi-user run level. |
sed s/^id:.*:initdefault:/id:3:initdefault:/ | ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target | Set to use multi-user runlevel on next reboot. |