本文共 4881 字,大约阅读时间需要 16 分钟。
systemd是Linux的系统服务管理器,也是默认的初始化系统,并且向后兼容SysV初始化系统的init脚本,能够在启动Linux系统的时候并发启动系统服务。systemd的配置文件/etc/systemd/system.conf。
systemd提出了units的概念,一个unit代表着一种systemd管理的系统资源。每个unit都是通过unit配置文件定义的。一个unit的类型是由其unit配置文件的后缀名表示的。如,对于最常见的service units,表示作为服务的资源。每个service unit都对应着一个systemd的service unit配置文件,以.service为扩展名。
RHEL/CentOS 6使用SysV init和Upstart构成初始化系统,通过预定义的runlevels区分执行模式,以执行/etc/rc.d/init.d/目录下对应的脚本文件,从而定制系统启动的过程。系统启动后,可以通过service命令或chkconfig命令进行操作系统服务。
在RHEL/CentOS 7中,systemd已经替代了Upstart,成为默认的初始化系统。/etc/rc.d/init.d/目录下的脚本文件虽然仍然存在,但只能是被各种service units文件所引用。service units的作用类似于/etc/rc.d/init.d/目录下的脚本文件,给出了当前系统的一个服务。
根据系统资源的类型不同, systemd units的类型也可能不同。当前支持的systemd units类型如下:
每个systemd unit都拥有一个配置文件,给出其配置属性。RHEL/CentOS 7中,systemd units的配置文件可能位于如下路径:
系统中可用的所有systemd units的配置文件
当前运行时中,启动的systemd units的配置文件
通过systemctl enable命令,创建的systemd units的配置文件。事实上,这里的systemd units配置文件,是对/usr/lib/systemd/system/中的配置文件的链接。
一个systemd unit的类型,是通过其配置文件的扩展名体现的,如对于一个service unit,其对应一个同名的以.service为扩展名的配置文件,位于/usr/lib/systemd/system/路径下。而一个target unit,其对应一个同名的以.target为扩展名的配置文件,如常用的multi-user.target。 为了保持向后兼容,在当前的基于systemd的Linux系统中,init脚本仍然有效,只是对于一个脚本文件/etc/rc.d/init.d/my,该脚本文件已经在系统初始化时被悄悄映射为一个服务文件/usr/lib/systemd/system/my.service。
例如,对于/etc/rc.d/rc.local脚本,对应/usr/lib/systemd/system/rc-local.service服务文件,该配置文件的默认内容如下:# This unit gets pulled automatically into multi-user.target by# systemd-rc-local-generator if /etc/rc.d/rc.local is executable.[Unit]Description=/etc/rc.d/rc.local CompatibilityConditionFileIsExecutable=/etc/rc.d/rc.localAfter=network.target[Service]Type=forkingExecStart=/etc/rc.d/rc.local startTimeoutSec=0RemainAfterExit=yes
一个systemd unit的配置文件,其常见结构如下:
[Unit],与unit类型无关的通用配置 After= Before= Requires= Wants= Conflicts= [Unit_type],如[Service]或[Mount]等,与该unit类型相关的专有配置 [Install],该unit的目标安装位置,通常是一个target 需要特别指出的是Service类型的systemd unit,其配置文件中的[Service]部分需要给出服务的启动类型Type。这与服务的运行形式密切相关,如下所示: forking,ExecStart会spawn一个子进程以提供服务,并保持该子进程,父进程退出 simple,默认值,ExecStart启动一个进程提供服务后,保持服务进程 oneshot,ExecStart启动一个进程提供服务后退出,即一次性运行,而无需长期运行 dbus,ExecStart先取得BusName,然后才能提供服务 idle,ExecStart在所有服务都正常执行完毕后,再启动进程提供服务。往往作为最后一个服务进程。 notify,ExecStart在收到sd_notify()消息后被唤醒,继续执行systemctl命令行工具
操作service units时,可以不带.service扩展名 systemctl stop nfs-server.service 等价于 systemctl stop nfs-server 设置service开机启动或禁止开机启动 systemctl enable service_name systemctl disable service_name 设置service禁止手工启动或允许手工启动 systemctl mask service_name #禁止手工启动 systemctl unmask service_name #允许手工启动 查看services列表 systemctl list-units --type service #启动的services systemctl list-units --type service --all --state=inactive #未启动的services systemctl list-units --type service --all #已加载(未必启动)的services systemctl list-unit-files --type service #已安装(未必加载)的services 查看services状态 systemctl status name.service systemctl is-enabled name.service systemctl is-active name.service systemctl is-failed name.service 查看unit配置文件 systemctl cat atd.service 以键值属性对的方式查看unit配置文件 systemctl show atd.service systemctl show atd.service -p Conflicts 查看atd.service配置文件中的Conflicts属性的值 查看units的依赖关系 systemctl list-dependencies sshd.service sshd对其他units的依赖关系 systemctl list-dependencies sshd.service --all sshd对其他units的依赖关系,递归显示所有依赖关系 systemctl list-dependencies sshd.service --reverse 依赖sshd的其他units 系统操作 systemctl reboot systemctl halt systemctl poweroff #即shutdown+power off systemctl hibernate systemctl suspend systemctl hybrid-sleep #即hibernate+suspend示例如下:
查看当前所有运行着的systemd service文件
systemctl 或 systemctl list-units --type service 查看当前已加载的systemd service文件 systemctl -a 或 systemctl list-units --type service --all 查看rc-local.service服务的启动情况 systemctl | grep rc-local.service UNIT LOAD ACTIVE SUB DESCRIPTION rc-local.service loaded active exited /etc/rc.d/rc.local Compatibility查看net相关的服务的启动情况
systemctl list-units --type service --all | grep net UNIT LOAD ACTIVE SUB DESCRIPTION ebtables.service loaded inactive dead Ethernet Bridge Filtering tables network.service loaded active exited LSB: Bring up/down networking rhel-domainname.service loaded active exited Read and set NIS domainname from /etc/sysconfig/network rhel-import-state.service loaded active exited Import network configuration from initramfs查看当前所有安装的systemd unit文件
systemctl list-unit-files查看rc-local.service的systemd unit文件
systemctl list-unit-files | grep rc-local.service UNIT FILE STATE rc-local.service static
参考链接:
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html-single/system_administrators_guide/index#chap-Managing_Services_with_systemd
http://0pointer.de/blog/projects/systemd-for-admins-3.html http://www.freedesktop.org/wiki/Software/systemd/Incompatibilities转载地址:http://bhlai.baihongyu.com/