-
Systemd:
POST –> Boot Sequence –> Bootloader –> kernel + initramfs(initrd) –> rootfs –>
/sbin/init
init:
-
CentOS 5: SysV init
-
CentOS 6: Upstart
-
CentOS 7: Systemd
-
Systemd新特性:
-
系统引导时实现服务并行启动;
-
按需激活进程;
-
系统状态快照;
-
基于依赖关系定义服务控制逻辑;
-
核心概念: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的激活机制:dbus
-
基于device的激活机制:比如usb
-
基于path的激活机制:监控某路径下的文件,一旦有变化启动服务
-
系统快照:保存各unit的当前状态信息于持久存储设备中;
-
向后兼容sysv init脚本;但是systemd 的target有十几个之多。system更精确。
-
不兼容:
-
systemctl命令固定不变(毕竟没有写脚本灵活)
-
非由systemd启动的服务,systemctl无法与之通信
-
sysv切换模式时init3 切换 到init 2/init.d/rc2.d/下所有k开头的都要stop,所有s开头都要start. 而start.systemd只有处于运行状态的程序才会stop。
-
系统服务不会读取,标准输入的数据流。不会跟任何标准输入进行交互
-
不会从用户哪里继承任何环境变量信息。使用绝对路径。
-
任何服务unit,5分钟未执行,都会超时退出。
-
管理系统服务:
CentOS 7: service unit
注意:能兼容早期的服务脚本
-
命令:
systemctl COMMAND name.service
-
启动:
service name start ==> systemctl start name.service
[root@centos72 samba]# systemctl start httpd
-
停止:
service name stop ==> systemctl stop name.service
[root@centos72 samba]# systemctl stop httpd
-
重启:
service name restart ==> systemctl restart name.service
[root@centos72 samba]# systemctl restart httpd
-
状态:
service name status ==> systemctl status name.service
[root@centos72 samba]# systemctl status httpd
-
条件式重启:
service name condrestart ==> systemctl try-restart name.service
[root@centos72 samba]# systemctl try-restart httpd
-
重载或重启服务:
systemctl reload-or-restart name.service
[root@centos72 samba]# systemctl reload-or-restart httpd
-
重载或条件式重启服务:
systemctl reload-or-try-restart name.service
[root@centos72 samba]# systemctl reload-or-try-restart httpd
-
禁止设定为开机自启:
systemctl mask name.service
[root@centos72 samba]# systemctl mask httpd
Created symlink from /etc/systemd/system/httpd.service to /dev/null.
-
取消禁止设定为开机自启:
systemctl unmask name.service
[root@centos72 samba]# systemctl unmask httpd
Removed symlink /etc/systemd/system/httpd.service.
-
查看某服务当前激活与否的状态:
systemctl is-active name.service
[root@centos72 samba]# systemctl is-active httpd
active
-
查看所有已经激活的服务:
systemctl list-units –type service
[root@centos72 samba]# systemctl list-units –type service
-
查看所有服务:
systemctl list-units –type service –all
[root@centos72 samba]# systemctl list-units –type service –all
-
chkconfig命令的对应关系:
-
设定某服务开机自启:
chkconfig name on ==> systemctl enable name.service
-
[root@centos72 samba]# systemctl enable httpd
-
禁止:
chkconfig name off ==> systemctl disable name.service
-
查看所有服务的开机自启状态:
chkconfig –list ==> systemctl list-unit-files –type service
[root@centos72 samba]# systemctl list-unit-files –type service
-
查看服务是否开机自启:
systemctl is-enabled name.service
[root@centos72 samba]# systemctl is-enabled httpd
disabled
-
查看服务的依赖关系:
systemctl list-dependencies name.service
[root@centos72 samba]# systemctl list-dependencies httpd
-
target units:
-
unit配置文件:.target
-
-
运行级别:
0 ==> runlevel0.target, poweroff.target
1 ==> runlevel1.target, rescue.target
2 ==> runlevel2.target, multi-user.target
3 ==> runlevel3.target, multi-user.target
4 ==> runlevel4.target, multi-user.target
5 ==> runlevel5.target, graphical.target
6 ==> runlevel6.target, reboot.target
-
级别切换:
init N ==> systemctl isolate name.target
-
查看级别:
-
列出系统的target:
runlevel ==> systemctl list-units –type target
[root@centos72 samba]# systemctl list-units –type target
-
获取默认运行级别:
/etc/inittab ==> systemctl get-default
[root@centos72 samba]# systemctl get-default
multi-user.target
-
修改默认级别:
/etc/inittab ==> systemctl set-default name.target
[root@centos72 samba]# systemctl get-default
multi-user.target
[root@centos72 samba]# systemctl set-default slices.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/slices.target.
[root@centos72 samba]# systemctl get-default
slices.target
-
切换至紧急救援模式:
systemctl rescue 大多数服务没启动,驱动会加载
-
切换至emergency模式:
systemctl emergency 不加载驱动、服务不启动、系统不做初始化,
centos6 e进入传递emgergency
-
其它常用命令:
-
关机:systemctl halt、systemctl poweroff
-
重启:systemctl reboot
-
挂起:systemctl suspend
-
快照:systemctl hibernate
快照并挂起:systemctl hybrid-sleep