背景: centos7 和 6的重大改变

对于系统管理员而言,系统的服务管理是一件很日常和很重要的工作,而7在6的基础上有了很大的改变,就连命令都完全不一样了。所以要拥抱变化,学习7是如何进行服务的管理和控制的。

system和systemctl的初探

Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。

Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。

在Linux生态系统中,Systemd被部署到了大多数的标准Linux发行版中,只有为数不多的几个发行版尚未部署。Systemd通常是所有其它守护进程的父进程,但并非总是如此。

【第一步】

查看systemd的版本

1
2
3
4
5
\# systemctl --version

systemd 219

+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

查看进程是否存在

1
ps -ef | grep [s]ystemd

注意:systemd是作为父进程(pid=1)运行的

分析systemd的启动进程

1
2
3
\# systemd-analyze

Startup finished in 639ms (kernel) + 931ms (initrd) + 12.432s (userspace) = 14.003s

分析各个进程启动发费的时间

1
\#systemd-analyze blame

分析启动时候的关键链

1
systemd-analyze critical-chain

重要:Systemctl接受服务(.service),挂载点(.mount),套接口(.socket)和设备(.device)作为单元。

列出所有服务可用单元

1
systemctl list-unit-files

(200多个)

列出所有运行中的单元

1
systemctl list-units

列出失败的单元?

1
systemctl --failed

列出某个单元是否启动

1
2
3
\# systemctl is-enabled crond.service

enabled

或者

1
2
3
\# systemctl is-enabled crond

enabled

检查某个单元或服务是否运行

1
2
3
\# systemctl is-active crond 

active

或者

1
\# systemctl status crond   

这个信息更详细

############################# 控制服务 ############################

列出所有服务(包括启用的和禁用的)

1
systemctl list-unit-files  --type=service

(120+)

以httpd为例

1
yum install httpd

会生成以下文件

1
/usr/lib/systemd/system/httpd.service 

Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
[root@Centos7-node2 ~]# systemctl status httpd

● httpd.service - The Apache HTTP Server

Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

Active: inactive (dead)

​ Docs: man:httpd(8)

​ man:apachectl(8)

[root@Centos7-node2 ~]# systemctl start httpd

[root@Centos7-node2 ~]# systemctl status httpd

● httpd.service - The Apache HTTP Server

Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

Active: active (running) since Sun 2016-10-23 00:48:43 CST; 3s ago

​ Docs: man:httpd(8)

​ man:apachectl(8)

Main PID: 26811 (httpd)

Status: "Processing requests..."

CGroup: /system.slice/httpd.service

​ ├─26811 /usr/sbin/httpd -DFOREGROUND

​ ├─26812 /usr/sbin/httpd -DFOREGROUND

​ ├─26813 /usr/sbin/httpd -DFOREGROUND

​ ├─26814 /usr/sbin/httpd -DFOREGROUND

​ ├─26815 /usr/sbin/httpd -DFOREGROUND

​ └─26816 /usr/sbin/httpd -DFOREGROUND



Oct 23 00:48:17 Centos7-node2 systemd[1]: Starting The Apache HTTP Server...

Oct 23 00:48:33 Centos7-node2 httpd[26811]: AH00558: httpd: Could not reliably determine the server's fully qua...ssage

Oct 23 00:48:43 Centos7-node2 systemd[1]: Started The Apache HTTP Server.

Hint: Some lines were ellipsized, use -l to show in full.

[root@Centos7-node2 ~]# systemctl reload httpd

[root@Centos7-node2 ~]# systemctl stop httpd

注意:

1
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

在loader中第一个disabled表示系统启动是否自启动,为非自动。 第二个表示工作状态

1
Active: active (running) since Sun 2016-10-23 00:48:43 CST; 3s ago

Active 绿色表示服务运行正常

如何激活服务并在启动时启用或禁用服务(即系统启动时自动启动服务)

1
2
3
[root@Centos7-node2 ~]# systemctl enable httpd

Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

禁用

1
2
3
4
5
systemctl disabled httpd

[root@Centos7-node2 ~]# systemctl disable httpd

Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.

使用systemctl命令杀死服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@Centos7-node2 ~]# systemctl kill httpd

[root@Centos7-node2 ~]# systemctl status httpd

● httpd.service - The Apache HTTP Server

Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

Active: failed (Result: exit-code) since Sun 2016-10-23 00:56:59 CST; 1s ago

​ Docs: man:httpd(8)

​ man:apachectl(8)

Process: 26901 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)

Main PID: 26869 (code=exited, status=0/SUCCESS)

Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"



Oct 23 00:51:38 Centos7-node2 systemd[1]: Starting The Apache HTTP Server...

Oct 23 00:51:53 Centos7-node2 httpd[26869]: AH00557: httpd: apr_sockaddr_info_get() failed for Centos7-node2

Oct 23 00:51:53 Centos7-node2 httpd[26869]: AH00558: httpd: Could not reliably determine the server's fully qua...ssage

Oct 23 00:52:03 Centos7-node2 systemd[1]: Started The Apache HTTP Server.

Oct 23 00:56:59 Centos7-node2 kill[26901]: kill: cannot find process ""

Oct 23 00:56:59 Centos7-node2 systemd[1]: httpd.service: control process exited, code=exited status=1

Oct 23 00:56:59 Centos7-node2 systemd[1]: Unit httpd.service entered failed state.

Oct 23 00:56:59 Centos7-node2 systemd[1]: httpd.service failed.

Hint: Some lines were ellipsized, use -l to show in full.

注意:Active: failed (Result: exit-code) since Sun 2016-10-23 00:56:59 CST; 1s ago

这个的failed表示的是kill掉的?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
\##########################  使用Systemctl控制并管理挂载点 #################3

[root@Centos7-node2 ~]# systemctl list-unit-files --type=mount

UNIT FILE STATE

dev-hugepages.mount static

dev-mqueue.mount static

proc-sys-fs-binfmt_misc.mount static

sys-fs-fuse-connections.mount static

sys-kernel-config.mount static

sys-kernel-debug.mount static

tmp.mount disabled

挂载、卸载、重新挂载、重载系统挂载点并检查系统中挂载点状态

1
2
3
4
5
systemctl start tmp.mount

systemctl status tmp.mount

systemctl reload tmp.mount

在启动时激活、启用或禁用挂载点(系统启动时自动挂载)

1
2
3
4
5
\# systemctl is-active tmp.mount

\# systemctl enable tmp.mount

\# systemctl disable tmp.mount

在Linux中屏蔽(让它不能启用)或可见挂载点

1
2
3
4
5
6
7
\# systemctl mask tmp.mount

ln -s '/dev/null''/etc/systemd/system/tmp.mount'

\# systemctl unmask tmp.mount

rm '/etc/systemd/system/tmp.mount'

####################  控制系统运行等级  ##################

启动系统救援模式

1
2
3
4
5
\# systemctl rescue

Broadcast message from root@tecmint on pts/0(Wed2015-04-2911:31:18 IST):

The system is going down to rescue mode NOW!

进入紧急模式

1
2
3
4
5
6
7
\# systemctl emergency   

Welcome to emergency mode!After logging in, type "journalctl -xb" to view

system logs,"systemctl reboot" to reboot,"systemctl default" to try again

to boot intodefault mode.

列出当前使用的运行等级

注意:init 1也是可以使用的

1
2
3
\# systemctl get-default

multi-user.target

注意:who -r 也是可以查看的

启动运行等级5,即图形模式

1
\# systemctl isolate runlevel5.target

1
\# systemctl isolate graphical.target

启动运行等级3,即多用户模式(命令行)

1
\# systemctl isolate runlevel3.target

1
\# systemctl isolate multiuser.target

设置多用户模式或图形模式为默认运行等级

1
2
3
4

\# systemctl set-default runlevel3.target

\# systemctl set-default runlevel5.target

重启、停止、挂起、休眠系统或使系统进入混合睡眠

1
2
3
4
5
6
7
8
9
\# systemctl reboot             

\# systemctl halt

\# systemctl suspend

\# systemctl hibernate

\# systemctl hybrid-sleep

对于不知运行等级为何物的人,说明如下。

1
2
3
4
5
6
7
8
9
10
11
Runlevel 0 : 关闭系统

Runlevel 1 : 救援?维护模式

Runlevel 3 : 多用户,无图形系统

Runlevel 4 : 多用户,无图形系统

Runlevel 5 : 多用户,图形化系统

Runlevel 6 : 关闭并重启机器

*注意:在centos7 中仍然可以使用init 0 关机 init 6 启动。