Linux系统服务管理技术

GO


有些服务我们日常用不到则要把它们停掉,一来可以节省资源,二来可以减少安全隐患。下面就介绍一下Linux(尤其是红帽系统Linux系统)中的服务管理工具。


1. ntsysv服务配置工具

如果没有这个命令请使用yum install -y ntsysv来安装它。这个工具用来配置哪些服务开启或者关闭,有点类似图形界面,不过是使用键盘来控制的。

直接运行命令ntsysv回车后弹出一个配置界面,如图:
ntsysv

按键盘的上下方向键可以调节红色光标,按空格可以选择开启或者不开启,如果前面的括号内显示有*,则表示开启,否则不开启。通过这个工具也可以查看的到目前系统中所有的服务。

建议除”crond、iptables、network、sshd、syslog、irqbalance、sendmail、microcode_ctl”外的其他服务全部停掉。

选择好后,按tab键选择”确定”,然后回车,需要重启机器才能生效。


2. chkconfig服务管理工具

CentOS6上的服务管理工具为chkconfig。在CentOS7上也可以使用这个命令,但是通过这个命令来查看的时候只有屈指可数的服务,这是因为CentOS7已经不再延续CentOS6版本的服务管理方案了。

总结:

  • Linux系统所有的预设服务可以查看/etc/init.d/目录得到:ls /etc/init.d/
  • 系统预设服务都是可以通过这样的命令实现:service 服务名 "start|stop|restart"来控制。
  • 除了可以使用servuce crond start启动crond外,还可以使用/etc/init.d/crond start来启动。
  • 使用chkconfig --list列出所有的服务以及每个级别事都开启。
    • 注意:该输出结果只显示SysV服务,并不包含CentOS7中原生的systemd服务。SysV配置数据可能被原生systemd配置覆盖。
  • 我们还可以使用grep命令把我们想要查看的服务过滤出来:chkconfig --list | grep cron
  • 更改某个级别下服务的开启(一个例子):chkconfig --level 3 crond off
    • 用–level指定级别,后面是服务名,然后是off或者on,–level后面还可以跟多个级别:chkconfig --level 345 crond off
    • 另外还可以省略级别,默认是针对2,3,4,5级别操作:chkconfig crond on

关于chkconfig:

  • 还有一个功能就是可以把某个服务加入到系统服务,即可以使用service 服务名 start这样的形式,并且可以在chkconfig --list中查找到。当然也能删除掉。
  • 下面的这个功能常用在把自定义的启动脚本加入到系统服务当中。
    • chkconfig --del crond
    • chkconfig --list | grep cron
    • chkconfig --add crond
    • chkconfig --list | grep cron

3. systemd 服务管理

3.1. 介绍

CentOS7不适用SysV而改为systemd了,这是因为systemd支持多个服务并发启动,而SysV只能一个一个地启动,这样最终导致的结果是systemd方式启动会块很多。

可以用这个命令列出系统所有的服务,systemctl list-units --all --type=service,示例如下:

1
2
3
4
5
6
7
8
9
10
[root@theshu ~]# systemctl list-units --all --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
aegis.service loaded active running LSB: aegis update.
agentwatch.service loaded active exited SYSV: Starts and stops g
aliyun.service loaded active running auto run aliyunservice o
atd.service loaded active running Job spooling tools
auditd.service loaded active running Security Auditing Servic
brandbot.service loaded inactive dead Flexible Branding Servic
cloud-config.service loaded active exited Apply the settings speci
内容省略...

那么这些服务对应的启动脚本文件在哪里呢?是在/usr/lib/systemd/system/这个目录下面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@theshu ~]# ls /usr/lib/systemd/system
system/ systemd-quotacheck
systemd systemd-random-seed
systemd-ac-power systemd-readahead
systemd-activate systemd-remount-fs
systemd-backlight systemd-reply-password
systemd-binfmt systemd-rfkill
systemd-bootchart systemd-shutdown
systemd-cgroups-agent systemd-shutdownd
systemd-coredump systemd-sleep
systemd-cryptsetup systemd-socket-proxyd
systemd-fsck systemd-sysctl
systemd-hibernate-resume systemd-sysv-install
systemd-hostnamed systemd-timedated
systemd-importd systemd-udevd
systemd-initctl systemd-update-done
内容省略...

3.2. 与服务相关的命令

下面是一些常用的与服务相关的命令的整理:

  • systemctl enable crond.service 让某个服务开机启动(.service可以省略)
  • systemctl disable crond.service 不让开机启动
  • systemctl status crond.service 查看服务状态
  • systemctl start crond.service 启动某个服务
  • systemctl stop crond.service 停止某个服务
  • systemctl restart crond.service 重启某个服务
  • systemctl is-enabled crond 查看某个服务是否开机启动

其实关于服务的用法还有不少,但是有上面这些就足够了,足以应对日常的运维工作。

3.3. 一个重要的概念-unit

介绍一个很重要的概念,那就是unit。刚刚在上面执行命令ls /usr/lib/systemd/system/的时候,下面有很多文件,其实可以把它们归类为下面这几大类:

  • service:系统服务
  • target:多个unit组成的组
  • device:硬件设备
  • mount:文件系统挂载点
  • automount:自动挂载点
  • path:文件或路径
  • scope:不是由systemd启动的外部进程
  • slice:进程组
  • snapshot:systemd快照
  • socket:进程间通信的套接字
  • swap:swap文件
  • timer:定时器

以上每种类型的文件都为一个unit,正是这些unit才组成了系统的各个资源(各个服务,各个设备等)。

下面介绍几个和unit相关的命令(关于unit,在工作中几乎用不到它,所以不多做介绍):

  • systemctl list-units 列出正在运行(active)的unit
  • systemctl list-units --all 列出所有的unit(包括失败的、inactive的)
  • systemctl list-units --all --state=inactive 列出所有inactive的unit
  • systemctl list-units --all --type=service 列出所有状态的service
  • systemctl list-units --type=service 列出状态为active的service
  • suytemctl is-active crond.service 查看某个unit是否active

3.4. target

再来介绍target的概念。target类似与CentOS6里面的启动级别,但target支持多个target同时启动。target其实是多个unit的组合,系统启动说白了就是启动多个unit,而乐管理方便,就使用target来管理这些unit。

查看当前系统的所有target:systemctl list-unit-files --type=target

1
2
3
4
5
6
7
[root@theshu ~]# systemctl list-unit-files --type=target
UNIT FILE STATE
basic.target static
bluetooth.target static
cloud-config.target static
cryptsetup-pre.target static
后面省略...

查看一个target包含的所有unit:systemctl list-dependencies *.target

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@theshu ~]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─aegis.service
● ├─agentwatch.service
● ├─atd.service
● ├─auditd.service
● ├─brandbot.path
● ├─cloud-config.service
● ├─cloud-final.service
● ├─cloud-init-local.service
● ├─cloud-init-upgrade.service
● ├─cloud-init.service
● ├─crond.service
● ├─dbus.service
● ├─ecs_mq.service
● ├─eni.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
● ├─ntpd.service
● ├─plymouth-quit-wait.service
后面省略...

下面还有几个关于target的命令:

  • systemctl get-default 查看系统默认的target
  • systemctl set-default multi-user.target 设置默认的target

上面提到的multi-user.target等同于CentOS6中的运行级别3,其实还有其他几个target对应0-6运行级别,如下表所示:

sysvinit运行级别 systemd目标名称 作用
0 runlevel0.target,poweroff.target 关机
1 runlevel1.target,rescue.target 单用户模式
2 runlevel2.target,multi-user.target 等同于级别3
3 runlevel3.target,multi-user.target 多用户的文本界面
4 runlevel4.target,multi-user.target 等同于级别3
5 runlevel5.target,graphical.target 多用户的图形界面
6 runlevel6.target,reboot.target 重启
emergency emergency.target 紧急Shell

3.5. service、unit与target之间的联系

  1. 一个service属于一种unit
  2. 多个unit一起组成了一个target
  3. 一个target里面包含了多个service

可以查看文件/usr/lib/systemd/system/sshd.service里面[install]部分的内容,它就定义了该service属于哪一个target。


4. 6和7版本对比

在RHEL6之前的系统管理系统服务的命令是service、chkconfig等,而在RHEL7系统中则是systemctl命令。其用法如以下表所示:

systemctl管理服务的启动、重启、停止、重载、查看状态的命令:

Sysvinit命令(红帽RHEL6系统) Systemctl命令(红帽RHEL7系统) 作用
service foo start systemctl start too.service 启动服务
service foo restart systemctl restart foo.service 重启服务
service foo stop systemctl stop foo.service 停止服务
service foo reload systemctl reload foo.service 重新加载配置文件(不终止服务)
service foo status systemctl status foo.service 查看服务状态

systemctl设置服务的开机启动、不启动、查看各级别下服务启动状态的命令:

Sysvinit命令(红帽RHEL6系统) Systemctl命令(红帽RHEL7系统) 作用
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 查看各个级别下服务的启动与禁用情况

OK

0%