一、systemd简介

systemd是CentOS 7和RHEL 7的init程序, 拥有如下新特性:

  • 系统引导时实现服务并行启动
  • 按需激活进程
  • 系统状态快照
  • 基于依赖关系定义服务控制逻辑

二、systemd核心概念: unit

systemd将各种系统启动和运行的相关对象表示为各种不同类型的unit, 并提供了处理不同unit之间依赖关系的能力. 使用配置文件进行标识和配置, 文件中主要包含系统服务、监听socket、保存的系统快照以及其他的init相关的信息, 配置文件路径如下:

  • /usr/lib/systemd/system/
  • /run/systemd/systemd/system/
  • /etc/systemd/system/

2.1 unit的类型

unit主要有以下类型:

unit类型 文件扩展名 作用
Service unit .service 用于封装一个后台服务进程
Target unit .target 用于模拟实现“运行级别”
Device unit .device 用于定义内核识别的设备
Mount unit .mount 用于定义文件系统挂载点
Socket unit .socket 用于标识进程间通信用的socket文件
Snapshot unit .snapshot 管理系统快照
Swap unit .swap 用于标识swap设备
Automount .automount 用于标识文件系统的自动挂载点
Path unit .path 用于定义文件系统中的一个文件或目录

2.2 关键特性

systemd关键特性如下:

  • 基于socket的激活机制: socket与服务程序分离
  • 基于bus的激活机制
  • 基于device的激活机制
  • 基于path的激活机制
  • 系统快照: 保存各unit的当前状态信息于持久存储设备中
  • 向后兼容sysV init脚本

Note: init的命令和systemd的命令不完全兼容, systemctl命令固定不变, 非由systemd启动的服务, systemctl无法与之通信

三、systemd管理系统服务命令: systemctl

systemctl - 管理系统服务, 能兼容早期的服务脚本

# 用法: systemctl COMMAND name[.service]

# 启动服务: service name start ==> systemctl start name[.service]
[root@zabbix system]# systemctl start httpd

# 停止服务: service name stop ==> systemctl stop name[.service]
[root@zabbix system]# systemctl stop httpd

# 重启服务: service name restart ==> systemctl restart name[.service]
[root@zabbix system]# systemctl restart httpd

# 状态: service name status ==> systemctl status name[.service]
[root@zabbix system]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2018-02-26 10:30:40 CST; 23min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 3870 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─3870 /usr/sbin/httpd -DFOREGROUND
           ├─3927 /usr/sbin/httpd -DFOREGROUND
           ├─3928 /usr/sbin/httpd -DFOREGROUND
           ├─3929 /usr/sbin/httpd -DFOREGROUND
           ├─3930 /usr/sbin/httpd -DFOREGROUND
           └─3931 /usr/sbin/httpd -DFOREGROUND

Feb 26 10:30:40 zabbix.leistudy.com systemd[1]: Starting The Apache HTTP Server...
Feb 26 10:30:40 zabbix.leistudy.com systemd[1]: Started The Apache HTTP Server.
Feb 26 10:31:40 zabbix.leistudy.com systemd[1]: Reloaded The Apache HTTP Server.
Feb 26 10:32:20 zabbix.leistudy.com systemd[1]: Reloaded The Apache HTTP Server.
Feb 26 10:39:18 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.
Feb 26 10:39:28 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.
Feb 26 10:39:38 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.

# 条件式重启: service name condrestart ==> systemctl try-restart name[.service]
[root@zabbix system]# systemctl try-restart httpd

# 重载或重启服务: systemctl reload-or-restart name[.service]
[root@zabbix system]# systemctl reload-or-restart httpd

# 重载或条件式重启: systemctl reload-or-try-restart name[.service]
[root@zabbix system]# systemctl reload-or-try-restart httpd

# 禁止设定为开机启动: systemctl mask name[.service]
[root@zabbix system]# systemctl mask httpd
Created symlink from /etc/systemd/system/httpd.service to /dev/null.

# 取消禁止设定为开机自启: systemctl unmask name[.service]
[root@zabbix system]# systemctl unmask httpd
Removed symlink /etc/systemd/system/httpd.service.

# 查看某服务当前激活状态: systemctl is-active name[.service]
[root@zabbix system]# systemctl is-active httpd
active

# 查看所有已经激活的服务: systemctl list-units --type service
[root@zabbix system]# systemctl list-units --type service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
auditd.service                     loaded active running Security Auditing Service
chronyd.service                    loaded active running NTP client/server
crond.service                      loaded active running Command Scheduler
dbus.service                       loaded active running D-Bus System Message Bus
...

# 查看所有服务: systemctl list-unit --type service --all
[root@zabbix system]# systemctl list-units --type service --all
  UNIT                                                  LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service                                        loaded    active   running Security Auditing Service
  brandbot.service                                      loaded    inactive dead    Flexible Branding Service
  chronyd.service                                       loaded    active   running NTP client/server
  cpupower.service                                      loaded    inactive dead    Configure CPU power related settings
  crond.service                                         loaded    active   running Command Scheduler
  dbus.service                                          loaded    active   running D-Bus System Message Bus
● display-manager.service                               not-found inactive dead    display-manager.service
...

# 设定某服务开机自启动: chkconfig name on ==> systemctl enable name[.service] 
[root@zabbix system]# systemctl enable httpd   
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

# 设定某服务禁止开机自启动: chkconfig name off ==> systemctl disable name[.service]
[root@zabbix system]# systemctl disable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.

# 查看所有服务的开机自启状态: chkconfig --list ==> systemctl list-unit-files --type service
[root@zabbix system]# systemctl list-unit-files --type service
UNIT FILE                                     STATE   
arp-ethers.service                            disabled
auditd.service                                enabled 
[email protected]                               enabled 
blk-availability.service                      disabled
...

# 查看服务是否开机自启: systemctl is-enabled name[.service]
[root@zabbix system]# systemctl is-enabled httpd
enabled

# 查看服务的依赖关系: systemctl list-dependencies name[.service]
[root@zabbix system]# systemctl list-dependencies httpd
httpd.service
● ├─-.mount
● ├─system.slice
● └─basic.target
●   ├─microcode.service
●   ├─rhel-autorelabel-mark.service
●   ├─rhel-autorelabel.service
●   ├─rhel-configure.service
...
  • target unit
# 运行级别:
    # level 0 ==> runlevel0.target, poweroff.target
    # level 1 ==> runlevel1.target, rescue.target
    # level 2 ==> runlevel2.target, multi-user.target
    # level 3 ==> runlevel3.target, multi-user.target
    # level 4 ==> runlevel4.target, multi-user.target
    # level 5 ==> runlevel5.target, graphical.target
    # level 6 ==> runlevel6.target, reboot.target

# 级别切换: init N ==> systemctl isolate name.service
[root@zabbix ~]# systemctl isolate multi-user.target

# 查看级别: runlevel ==> systemctl list-units --type target
[root@zabbix ~]# systemctl list-units --type target
UNIT                  LOAD   ACTIVE SUB    DESCRIPTION
basic.target          loaded active active Basic System
cryptsetup.target     loaded active active Encrypted Volumes
getty.target          loaded active active Login Prompts
local-fs-pre.target   loaded active active Local File Systems (Pre)
local-fs.target       loaded active active Local File Systems
multi-user.target     loaded active active Multi-User System
network-online.target loaded active active Network is Online
network.target        loaded active active Network
paths.target          loaded active active Paths
remote-fs.target      loaded active active Remote File Systems
slices.target         loaded active active Slices
sockets.target        loaded active active Sockets
swap.target           loaded active active Swap
sysinit.target        loaded active active System Initialization
timers.target         loaded active active Timers

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.

15 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

# 获取默认运行级别: /etc/inittab ==> systemctl get-default
[root@zabbix ~]# systemctl get-default
multi-user.target

# 修改默认级别: /etc/inittab ==> systemctl set-default name[.target]
[root@zabbix ~]# systemctl set-default rescue.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/rescue.target.

# 切换至紧急救援模式: systemctl resuce

# 切换至emergency模式: systemctl emergency
  • 其他常用命令
# 关机: systemctl halt, systemctl poweroff

# 重启: systemctl reboot

# 挂起: systemctl suspend

# 快照
    # 保存系统快照: systemctl hibernate
    # 快照并挂起: systemctl hibrid-sleep