2024年6月15日发(作者:)

Linux操作系统(RHEL 7/CentOS 7)——张同光——清华大学出版社

3.3 系统和服务管理

3.3.1 系统和服务管理器:systemd

1.SysV init、Upstart init、systemd

RHEL7采用systemd作为默认init程序,所以runlevel的概念基本上也就不存在了。

2.unit

systemd开启和监督整个系统是基于unit的概念。unit是由一个与配置文件对应的名字和

类型组成的(例如:e unit有一个具有相同名字的配置文件,是守护进程Avahi的

一个封装单元)。unit有以下7种类型:

(1)service:

(2)socket:

(3)device:

(4)mount:

(5)automount:

(6)target:

(7)snapshot:

3.systemd提供以下主要特性

(1)使用socket的前卫的并行性能:

(2)D-Bus激活策略启动服务:

(3)提供守护进程的按需启动策略。

(4)保留了使用Linux cgroups进程的追踪功能:

(5)支持快照和系统状态恢复:

(6)维护挂载和自挂载点:

(7)实现了各服务间基于依赖关系的一个精细的逻辑控制:

4.systemd的主要工具

(1)systemctl命令:查询和控制systemd系统和系统服务管理器的状态。

(2)journalctl命令:查询系统的journal(日志)。

(3)systemd-cgls命令:以树形列出正在运行的进程,可以递归显示Linux控制组内容。

3.3.2 监视和控制systemd的命令:systemctl

1.chkconfig

[root@localhost ~]# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生

systemd 配置覆盖。

如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。

欲查看对特定 target 启用的服务请执行

'systemctl list-dependencies [target]'。

iprdump 0:关 1:关 2:开 3:开 4:开 5:开 6:关

iprinit 0:关 1:关 2:开 3:开 4:开 5:开 6:关

iprupdate 0:关 1:关 2:开 3:开 4:开 5:开 6:关

netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关

network 0:关 1:关 2:开 3:开 4:开 5:开 6:关

Linux操作系统(RHEL 7/CentOS 7)——张同光——清华大学出版社

pmcd

pmie

pmlogger

pmmgr

pmproxy

pmwebd

rhnsd

0:关

0:关

0:关

0:关

0:关

0:关

0:关

1:关

1:关

1:关

1:关

1:关

1:关

1:关

2:关

2:关

2:关

2:关

2:关

2:关

2:开

3:关

3:关

3:关

3:关

3:关

3:关

3:开

4:关

4:关

4:关

4:关

4:关

4:关

4:开

5:关

5:关

5:关

5:关

5:关

5:关

5:开

6:关

6:关

6:关

6:关

6:关

6:关

6:关

2.分析系统状态

$ systemctl

$ systemctl list-units

$ systemctl --failed

$ systemctl list-unit-files

//输出激活的单元,即列出所有正在运行的服务

//输出激活的单元

//输出运行失败的单元

//查看所有已安装服务

//立即激活单元

//立即停止单元

//重启单元

//命令单元重新读取配置

//输出单元运行状态

//检查单元是否配置为自动启动

//开机自动激活单元

//取消开机自动激活单元

//显示单元的手册页(必须由单元文件提供)

//重新载入systemd,扫描新的或有变动的单元

3.使用单元

# systemctl start <单元>

# systemctl stop <单元>

# systemctl restart <单元>

# systemctl reload <单元>

$ systemctl status <单元>

$ systemctl is-enabled <单元>

# systemctl enable <单元>

# systemctl disable <单元>

# systemctl help <单元>

# systemctl daemon-reload

4.电源管理

$ systemctl reboot

$ systemctl poweroff

$ systemctl suspend

$ systemctl hibernate

$ systemctl hybrid-sleep

//重启

//退出系统并停止电源

//待机

//休眠

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

systemctl是系统服务管理器命令,它实际上将service和chkconfig这两个命令组合到一

起。

示例如下:

# systemctl //列出所有正在运行的服务

# systemctl enable e //将httpd服务设为开机自动启动

# systemctl disable e //禁止httpd服务开机自动启动,等价chkconfig --level 3 httpd off

# systemctl status e //查看httpd服务的运行状态

# systemctl is-active e //检查httpd服务是否处于活动状态

# systemctl start e //启动httpd服务,等价于service httpd start

# systemctl stop e //停止httpd服务,等价于service httpd stop

# systemctl restart e //重新启动httpd服务

# systemctl list-units --type=service //显示所有已启动的服务,等价于chkconfig --list

# chkconfig --list | grep httpd //用chkconfig命令检测一下服务是否运行成功

5.救援

# systemctl rescue

# systemctl emergency

//在救援模式,执行命令# systemctl default,返回桌面环境

6.预读功能

# systemctl enable e

# systemctl enable e

3.3.3 改变目标(运行级别)

systemd使用比sysvinit的运行级更为自由的target概念作为替代。

第3运行级用替代。

第5运行级用替代。

Linux操作系统(RHEL 7/CentOS 7)——张同光——清华大学出版社

和分别是指向和的符号链

接。

注意:

runlevel还是可以使用,但是systemd不使用/etc/inittab文件,修改/etc/inittab文件不

会更改默认运行级别。所以严格来说不再有运行级别了。所谓默认的运行级别指的就是

/etc/systemd/system/文件,而查看这个文件我们会发现它是一个软链接,如下:

/etc/systemd/system/ -> /lib/systemd/system/

1.获取当前目标

# systemctl list-units --type=target

UNIT LOAD ACTIVE SUB DESCRIPTION

loaded active active Basic System

loaded active active Encrypted Volumes

loaded active active Login Prompts

LOAD = Reflects whether the unit definition was properly loaded.

ACTIVE = The high-level unit activation state, i.e. generalization of SUB.

SUB = The low-level unit activation state, values depend on unit type.

17 loaded units listed. Pass --all to see loaded but inactive units, too.

To show all installed unit files use 'systemctl list-unit-files'.

[root@localhost ~]# runlevel

N 5

[root@localhost ~]#

2.创建新目标

3.目标表

Systemd目标及其含义见表3-28。

4.使用命令切换运行级别/目标

# systemctl isolate

# systemctl isolate

# systemctl isolate

# systemctl isolate

//切换到运行级别3,该命令对下次启动无影响,等价于telinit 3

//切换到运行级别3,该命令对下次启动无影响,等价于telinit 3

//切换到运行级别5,该命令对下次启动无影响,等价于telinit 5

//切换到运行级别5,该命令对下次启动无影响,等价于telinit 5

//文字模式

//图形模式

5.修改默认运行级别/目标

# ln -sf /lib/systemd/system/ /etc/systemd/system/

# ln -sf /lib/systemd/system/ /etc/systemd/system/

也可以执行systemctl命令,设置启动时默认进入文本模式或图形模式。

# systemctl -f enable

# systemctl -f enable

//文字模式

//图形模式

3.3.4 用户自定义开机启动脚本

系统默认已经生成/lib/systemd/system/e文件,该文件内容如下:

# This file is part of systemd.

# systemd is free software; you can redistribute it and/or modify it

# under the terms of the GNU Lesser General Public License as published by

# the Free Software Foundation; either version 2.1 of the License, or

# (at your option) any later version.

# This unit gets pulled automatically into by

# systemd-rc-local-generator if /etc/rc.d/ is executable.

[Unit]

Description=/etc/rc.d/ Compatibility

ConditionFileIsExecutable=/etc/rc.d/

After=

[Service]

Type=forking

Linux操作系统(RHEL 7/CentOS 7)——张同光——清华大学出版社

ExecStart=/etc/rc.d/ start

TimeoutSec=0

RemainAfterExit=yes

SysVStartPriority=99

是用户希望在系统启动时自动启动一些脚本或命令,可以修改/etc/rc.d/文件。

然后,添加执行权限:chmod +x /etc/rc.d/。

接着,启用脚本:systemctl enable e && reboot。

3.3.5 日志:journalctl

1.过滤输出

示例如下:

# journalctl -b

# journalctl -f

# journalctl /usr/lib/systemd/systemd

# journalctl _PID=1

# journalctl -u netcfg

//显示本次启动后的所有日志

//动态跟踪最新信息

//显示特定程序的所有消息

//显示特定进程的所有消息

//显示指定单元的所有消息

详情参阅man journalctl、man l-fields。

2.日志大小限制