系统管理员

systemd 的主要命令行工具是 systemctl。

多数管理员应该都已经非常熟悉系统服务和 init 系统的管理,比如 service、chkconfig 以及 telinit 命令的使用。systemd 也完成同样的管理任务,只是命令工具 systemctl 的语法有所不同而已,因此用表格来对比 systemctl 和传统的系统管理命令会非常清晰。

表 2. Systemd 命令和 sysvinit 命令的对照表

Sysvinit 命令

Systemd 命令

备注

service foo start

systemctl start foo.service

用来启动一个服务 (并不会重启现有的)

service foo stop

systemctl stop foo.service

用来停止一个服务 (并不会重启现有的)。

service foo restart

systemctl restart foo.service

用来停止并启动一个服务。

service foo reload

systemctl reload foo.service

当支持时,重新装载配置文件而不中断等待操作。

service foo condrestart

systemctl condrestart foo.service

如果服务正在运行那么重启它。

service foo status

systemctl status foo.service

汇报服务是否正在运行。

ls /etc/rc.d/init.d/

systemctl list-unit-files –type=service

用来列出可以启动或停止的服务列表。

chkconfig foo on

systemctl enable foo.service

在下次启动时或满足其他触发条件时设置服务为启用

chkconfig foo off

systemctl disable foo.service

在下次启动时或满足其他触发条件时设置服务为禁用

chkconfig foo

systemctl is-enabled foo.service

用来检查一个服务在当前环境下被配置为启用还是禁用。

chkconfig –list

systemctl list-unit-files –type=service

输出在各个运行级别下服务的启用和禁用情况

chkconfig foo –list

ls /etc/systemd/system/*.wants/foo.service

用来列出该服务在哪些运行级别下启用和禁用。

chkconfig foo –add

systemctl daemon-reload

当您创建新服务文件或者变更设置时使用。

telinit 3

systemctl isolate multi-user.target (OR systemctl isolate runlevel3.target OR telinit 3)

改变至多用户运行级别。

除了表 2 列出的常见用法,系统管理员还需要了解其他一些系统配置和管理任务的改变。

首先我们了解 systemd 如何处理电源管理,命令如下表所示:

表 3,systemd 电源管理命令

命令

操作

systemctl reboot

重启机器

systemctl poweroff

关机

systemctl suspend

待机

systemctl hibernate

休眠

systemctl hybrid-sleep

混合休眠模式(同时休眠到硬盘并待机)

关机不是每个登录用户在任何情况下都可以执行的,一般只有管理员才可以关机。正常情况下系统不应该允许 SSH 远程登录的用户执行关机命令。否则其他用户正在工作,一个用户把系统关了就不好了。为了解决这个问题,传统的 Linux 系统使用 ConsoleKit 跟踪用户登录情况,并决定是否赋予其关机的权限。现在 ConsoleKit 已经被 systemd 的 logind 所替代。