Linux Sysmted介绍
Linux 启动流程
POST-->Boot Sequeue(BIOS)-->Bootloader(MBR)-->Kernel(ramdisk)-->rootfs -->/sbin/init
init
CentOS 5 # SysV init
CentOS 6 # Upstart
CentOS 7 # Systemd
Systemd的新特性
系统引导时实现服务并行启动
按需激活进程
系统状态快照
基于依赖关系定义服务控制逻辑
核心概念:unit
unit 由其相关配置文件进行标识、识别和配置;文件中主要包含了系统服务、监听的socket、保存的快照以及其它与init相关的信息; 这些配置文件主要保存在:
/usr/lib/systemd/system
/run/systemd/system
/etc/systemd/system
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, 用于定义文件系统中的一文件或目录
关键特性
基于socket的激活机制:socket与程序分离
基于bus的激活机制
基于device的激活机制
基于Path的激活机制
系统快照:保存各unit的当前状态信息于持久存储设备中
向后兼容sysv init脚本( /etc/init.d/ )
不兼容说明
- systemctl的命令是固定不变的
- 非由systemd启动的服务,systemctl无法与之通信
管理系统服务
系统服务在 CentOS 7 中是 service 类型的 unit 文件。
可使用 systemctl 命令进行系统服务管理。
服务状态
# systemctl list-unit-files --type service --all # 显示状态
# loaded # Unit配置文件已处理
# active(running) # 一次或多次持续处理的运行
# active(exited) # 成功完成一次性的配置
# active(waiting) # 运行中,等待一个事件
# inactive # 不运行
# enabled # 开机启动
# disabled # 开机不启动
# static # 开机不启动,但可被另一个启用的服务激活
示例:
# systemctl list-unit-files --type service --all | grep sshd
sshd-keygen.service static
sshd.service enabled
sshd@.service static
systemctl 命令
syscemctl - Control the systemd system and service manager
语法格式:
systemctl [OPTIONS...] COMMAND [NAME...]
管理系统常用命令
功能 CentOS 5,6 命令 CentOS 7 命令
启动:
service NAME start ==> systemctl start NAME.service
停止:
service NAME stop ==> systemctl stop NAME.service
重启:
service NAME restart ==> systemctl restart NAME.service
状态:
service NAME status ==> systemctl status NAME.service
条件式重启:
service NAME condrestart ==> systemctl try-restart NAME.service
重载或重启服务:
systemctl reload-or-restart NAME.servcie
重载或条件式重启服务:
systemctl reload-or-try-restart NAME.service
查看某服务当前激活与否的状态:
systemctl is-active NAME.service
查看所有已激活的服务:
systemctl list-units --type service
查看所有服务(已激活及未激活):
chkconfig --lsit ==> systemctl list-units -t service --all
查看所有服务:
systemctl list-units --type service --all|-a
查看所有已经激活的服务:
systemctl list-units --type|-t service
设置服务开机自启:
chkconfig NAME on &#