管理systemd(一)

一、systemd基本概念

(一)systemd简要介绍

  • 定义
    系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其它进程
  • 新特性
    • 系统引导时实现服务并行启动
    • 按需启动守护进程
    • 自动化的服务依赖关系管理
    • 同时采用socket式与D-Bus总线式激活服务
    • 系统状态快照
  • systemd核心概念unit
    unit表示不同类型的systemd对象,通过配置文件进行标识和配置;文件中主要包含了系统服务、监听socket、保存的系统快照以及其它与init相关的信息
  • 配置文件
    • /usr/lib/systemd/system:每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/
    • /run/systemd/system:系统执行过程中所产生的服务脚本,比上面目录优先运行
    • /etc/systemd/system:管理员建立的执行脚本,类似于/etc/rc.d/rcN.d/Sxx类的功能,比上面目录优先运行

(二)systemd的unit类型

  • systemctl -t help:查看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 unit:文件扩展名为.automount,文件系统的自动挂载点
  • path unit:文件扩展名为.path,用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激活服务,如:spool 目录

(三)systemd的特性

  • 关键特性
    • 基于socket的激活机制:socket与服务程序分离
    • 向后兼容sysvinit脚本
  • 不兼容:
    • systemctl命令固定不变,不可扩展
    • 非由systemd启动的服务,systemctl无法与之通信和控制

二、systemd管理服务

  • 语法:systemctl COMMAND name.service

(一)管理服务

  • 启动:systemctl start name.service,相当于CentOS6的service name start
  • 停止:systemctl stop name.service,相当于CentOS6的service name stop
  • 重启:systemctl restart name.service,相当于CentOS6的service name restart
  • 状态:systemctl status name.service,相当于CentOS6的service name status

  • 条件式重启:已启动才重启,否则不做操作
    systemctl try-restart name.service,相当于CentOS6的service name condrestart
    如下图:之前sshd服务未启动,使用条件式重启时发现服务未开启,未做任何操作,故查询状态仍旧是inactive
  • 重载或重启服务:先加载,再启动
    systemctl reload-or-restart name.service
  • 重载或条件式重启服务:
    systemctl reload-or-try-restart name.service
  • 禁止自动和手动启动:
    systemctl mask name.service
  • 取消禁止自动和手动启动:
    systemctl unmask name.service
    如图:可以看出mask和umask命令实际为建立/删除指向/dev/null的软链接
  • 实验:修改unit
    修改/usr/lib/systemd/system/sshd.service文件,将Description改为OpenSSH new server daemon,此时用systemctl status sshd.service查看服务状态,发现服务正常运行,但名称没有改变

根据状态信息的提示,执行systemctl daemon-reload,再查看状态发现名称改变了

当服务正在运行时,对服务的unit文件的改动必须进行重载才能更新

(二)查看服务

  • 查看某服务当前激活与否的状态:
    systemctl is-active name.service
  • 查看所有已经激活的服务:
    systemctl list-units –type|-t service
  • 查看所有服务:
    systemctl list-units –type|-t service –all|-a

(三)设置服务开机自启状态

  • 设定某服务开机自启:
    systemctl enable name.service,相当于CentOS6的chkconfig name on
  • 设定某服务开机禁止启动:
    systemctl disable name.service,相当于CentOS6的chkconfig name off
  • 查看所有服务的开机自启状态:
    systemctl list-unit-files –type|-t service,相当于CentOS6的chkconfig –list
  • 用来列出该服务在哪些运行级别下启用和禁用:
    ls /etc/systemd/system/*.wants/sshd.service,相当于CentOS6的chkconfig sshd –list
  • 实验:设置服务自启和禁止自启(1)ntpdate.service服务原先禁止自启[红框1]。
    (2)执行systemctl enabel ntpdate.service命令,设置ntpdate.service服务为自启,提示建立了软链接文件[红框2]。
    (3)此时删除软链接文件后,发现ntpdate.service服务仍旧为禁止自启状态[红框3]。
    (4)按照之前的提示信息,手动建立软链接文件
    ln -s /usr/lib/systemd/system/ntpdate.service /etc/systemd/system/multi-user.target.wants/ntpdate.service
    (5)再查询发现ntpdate.service服务的状态变为自启[红框4]。
    (6)执行systemctl disable ntpdate.service命令,提示删除了软链接文件。
    (7)使用ls命令查询提示信息路径,确实文件已经消失。
    (8)执行systemctl is-enabled ntpdate.service命令,确认当前ntpdate服务处于禁止自启状态。
    总结:将某个服务设为自启或者禁止自启,实际上就是在相应的文件夹建立软链接文件的过程,systemd通过查询软链接文件的存在与否判断启用/禁止自启的状态。

(四)其他命令

  • 查看服务是否开机自启:
    systemctl is-enabled name.service
  • 查看服务的依赖关系:
    systemctl list-dependencies name.service
  • 杀掉进程:
    systemctl kill unitname

(五)服务状态

  • 显示状态:systemctl list-unit-files –type service –all
    • loaded:unit配置文件已处理
    • active(running):一次或多次持续处理的运行
    • active(exited):成功完成一次性的配置
    • active(waiting):运行中,等待一个事件
    • inactive:不运行
    • enabled:开机启动
    • disabled:开机不启动
    • static:开机不启动,但可被另一个启用的服务激活

三、systemd管理运行级别

(一)target的基本概念

  • target:systemd的运行级别管理unit
  • 路径:/usr/lib/systemd/system/*.target
  • 显示状态:systemd list-unit-files –type target –all
  • systemd的运行级别与传统运行级别的对应关系
    • runlevel0.target, poweroff.target ==> 0
    • runlevel1.target, rescue.target ==> 1
    • runlevel2.target, multi-user.target ==> 2
    • runlevel3.target, multi-user.target ==> 3
    • runlevel4.target, multi-user.target ==> 4
    • runlevel5.target, graphical.target ==> 5
    • runlevel6.target, reboot.target ==> 6
  • 查看target的依赖关系:
    systemctl list-dependencies name.target

(二)级别切换

    • 级别切换:systemctl isolate name.target
      注:只有/lib/systemd/system/*.target文件中AllowIsolate=yes 才能切换(修改文件需执行systemctl daemon-reload才能生效)
    • 显示状态:systemctl list-unit-files –type target -all
    • 获取默认运行级别:
      systemctl get-default
    • 修改默认级别:
      systemctl set-default name.target
    • 实验:将默认级别由multi-user改为graphical

四、systemd其他命令

    • 切换至紧急救援模式:
      systemctl rescue
    • 切换至emergency模式:
      systemctl emergency
    • 其它常用命令:
      • 传统命令init,poweroff,halt,reboot都成为systemctl的软链接
      • 关机:systemctl halt、systemctl poweroff
      • 重启:systemctl reboot
      • 挂起:systemctl suspend
      • 休眠:systemctl hibernate
      • 休眠并挂起:systemctl hybrid-slee
上一篇
下一篇