1.内核空间系统启动流程
1)post:进行加电自检
2)Boot Sequence(bios):选择具有引导程序的设备
3)Bootloader(MBR):提供菜单,将选择启动的内核加载至内存中
4)kernel(ramdisk):为内核提供可加载根文件系统的驱动,以及其他所需内核模块的临时根
5)rootfs:进行根切换
6)/sbin/init:启动用户空间的第一个程序init
2.各版本init程序
1)centos5:SysV init、
2)centos6:Upstart
3)centos7:Systemd
1.新特性
1.系统引导时实现服务并行启动(针对没有依赖关系的服务,加快其启动速度)
2.按需激活进程(只有在第一次使用时才会启动该进程)
3.系统状态快照(支持回滚至系统过去的某个状态)
4.基于依赖关系定义服务控制逻辑
1.unit:由其相关配置文件进行标识,识别和配置;配置文件中主要包含了系统服务,监听的socket,保存的快照以及其他与init相关的信息,其保存位置为:
1./usr/lib/systemd/system
2./etc/systemd/system(实现开机自动启动的程序,实际为/usr/lib/systemd/system下的符号链接)
3./run/systemd/system
ps:systemd所有管理功能通过unit来实现
2.unit的常见类型:
1.Service unit:文件扩展名为.service,用于定义系统服务
2.Target unit:文件扩展名为.target,用于模拟实现“运行级别”
3.Device unit:文件扩展名为.device,用于定义内核识别的设备
4.Mount unit:文件扩展名为.mount,定义文件系统挂载点
5.Socket unit:文件扩展名为.socket,用于标识进程间通信用到的socket文件
6.Snapshot unit:文件内扩展名为.snapshot,管理系统快照
7.Swap unit:文件扩展名为.swap,用于标识swap设备
8.Automount unit:文件扩展名为.automount,文件系统自动挂载点设置
9.Path unit:文件扩展名为.path,用于定义文件系统中的一个文件或目录
3.关键特性:
1.基于socket的激活机制,socket与程序分离(先给对应程序socket文件,但是仅在使用时激活)
2.基于bus的激活机制(总线上有存在对于某一个服务的访问,基于总线激活)
3.基于device的激活机制(某个设备插入时,自动激活Mount unit,Automount unit,可以实现自动创建设备文件,自动挂载)
4.基于path的激活机制(监控文件或目录是否在,根据其存在与否,可自动进行相对应的另一个进程)
5.系统快照:保存各个unit的当前状态信息于持久存储设备中()
ps:支持先向后兼容sysv init脚本,即/etc/init.d/下的脚本
4.不兼容:systemctl的命令是固定不变的,并且非由systemd启动的服务,systemctl无法与之进行通信
1.systemctl命令:Control the systemd system and service manager;控制systemd系统和服务管理器
2.systemctl语法:systemctl [OPTIONS...] COMMAND [NAME...]
1.启动:service NAME start(centos 6) => systemctl start NAME.service(centos7)
2.停止:service NAME stop => systemctl stop NAME.service
3.重启:service NAME restart => systemctl restart NAME.service
4.查看状态:service NAME status => systemctl status NAME.service
5.条件式重启:service NAME condrestart => systemctl try-restart NAME.service(若服务已启动,则将其停止后启动,若服务未启动,则不启动该服务)
6.重载或重启服务:systemctl reload-or-restart NAME.service
7.重载或条件式重启服务:systemctl reload-or-try-restart NAME.service
8.查看某服务当前激活状态的与否:systemctl is-active NAME.service
9.查看所有已激活服务:systemctl list-units --type service
10.查看所有服务(激活与未激活):chkconfig --list => systemctl list-units -t service -all
11.设置服务开机自动启动:chkconfig NAME on => systemctl enable NAME.service
12.禁止服务开机自动启动:chkconfig NAME off => systemctl disable NAME.service
13.查看某服务是否能开机自动启动:chkconfig --list NAME => systemctl is-enabled NAME.service
14.禁止某服务设定为开机自动启动:systemctl mask NAME.service
15.取消此禁止项:systemctl unmask NAME.service
16.查看服务间的依赖关系:systemctl list-dependencies NAME.service
3.相关练习
1)查看
[root@yuki ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
loaded:已装载,即已在对应目录下创建unit文件;disabled:开机不会自动启动;inactive:未激活
2)启动
[root@yuki ~]# systemctl start httpd
[root@yuki ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 五 2019-05-31 17:49:34 CST; 2s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 1915 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─1915 /usr/sbin/httpd -DFOREGROUND
├─1917 /usr/sbin/httpd -DFOREGROUND
├─1918 /usr/sbin/httpd -DFOREGROUND
├─1919 /usr/sbin/httpd -DFOREGROUND
├─1920 /usr/sbin/httpd -DFOREGROUND
└─1921 /usr/sbin/httpd -DFOREGROUND
5月 31 17:49:33 yuki systemd[1]: Starting The Apache HTTP Server...
5月 31 17:49:34 yuki httpd[1915]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 1... message
5月 31 17:49:34 yuki systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
ps:cgroup:控制组
3)停止
[root@yuki ~]# systemctl stop httpd.service
[root@yuki ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
4)重启
Hint: Some lines were ellipsized, use -l to show in full.
[root@yuki ~]# systemctl restart httpd.service
[root@yuki ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 五 2019-05-31 17:52:47 CST; 2s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 2019 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─2019 /usr/sbin/httpd -DFOREGROUND
├─2022 /usr/sbin/httpd -DFOREGROUND
├─2023 /usr/sbin/httpd -DFOREGROUND
├─2024 /usr/sbin/httpd -DFOREGROUND
├─2025 /usr/sbin/httpd -DFOREGROUND
└─2026 /usr/sbin/httpd -DFOREGROUND
5)条件式重启
此时httpd服务已经启动
[root@yuki ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 五 2019-05-31 17:52:47 CST; 54s ago
Docs: man:httpd(8)
man:apachectl(8)
进行条件式重启,可以发现其启动时间已经变更
[root@yuki ~]# systemctl try-restart httpd
[root@yuki ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 五 2019-05-31 17:54:12 CST; 1s ago
Docs: man:httpd(8)
man:apachectl(8)
停止httpd服务,进行条件式重启,发现其1服务并未重启
[root@yuki ~]# systemctl stop httpd
[root@yuki ~]# systemctl try-restart httpd
[root@yuki ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
6)查看某服务当前激活与否的状态
[root@yuki ~]# systemctl is-active httpd
unknown
[root@yuki ~]# systemctl start httpd
[root@yuki ~]# systemctl is-active httpd
active
7)查看所有已激活的服务
[root@yuki ~]# systemctl list-units --type service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-xorg.service loaded active running ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
accounts-daemon.service loaded active running Accounts Service
8)查看所有服务(已激活及未激活)
[root@yuki ~]# systemctl list-units -t service -a
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-vmcore.service loaded inactive dead Harvest vmcores for ABRT
9)设置服务开机自启
此时为disabled
[root@yuki ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 五 2019-05-31 17:58:04 CST; 5min ago
此时为enabled
[root@yuki ~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@yuki ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2019-05-31 17:58:04 CST; 6min ago
10)查看某服务是否能开机自启
[root@yuki ~]# systemctl is-enabled httpd.service
enabled
11)禁止某服务设定为开机自启:
[root@kasumi ~]# systemctl mask httpd.service
Created symlink from /etc/systemd/system/httpd.service to /dev/null.
12)取消此禁止
[root@kasumi ~]# systemctl unmask httpd.service
Removed symlink /etc/systemd/system/httpd.service.
13)查看服务的依赖关系
[root@yuki ~]# systemctl list-dependencies httpd.service
httpd.service
● ├─-.mount
● ├─system.slice
● └─basic.target
● ├─alsa-restore.service
.
.
.
1.centos6与centos7运行级别对比
运行级别:centos 6 ==> centos 7
0 ==> runlevel0.target power off.target
1 ==> runlevel1.target rescue.target
2 ==> runlevel2.target multi-user.target
3 ==> runlevel4.target multi-user.target
4 ==> runlevel4.target multi-user.target
5 ==> runlevel5.target graphical.target
6 ==> runlevel6.target reboot.target
2.级别切换
1.centos 6:init runlevel
2.centos 7:systemctl isolate NAME.target
3.查看运行级别
1.centos 6:runlevel
2.centos 7:systemctl list-units -t target
3.查看所有运行级别:systemctl list-units -t target -a
4.获取默认运行级别:systemctl get-default
5.修改默认运行级别:systemctl set-default
6.切换至紧急救援模式:systemctl rescue(会启动开机自动开启的相关服务)
7.切换至emergency模式:systemctl emergency(不会启动自动开启的相关服务)
4.其他常用命令:
1.关机:systemctl halt;systemctl power off
2.重启:systemctl reboot
3.挂起:systemctl suspend
4.快照:systemctl hibernate
5.快照并挂起:systemctl hybrid-sleep
5.相关练习
1)查看级别
[root@yuki ~]# systemctl list-units -t target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
bluetooth.target loaded active active Bluetooth
cryptsetup.target loaded active active Encrypted Volumes
getty.target loaded active active Login Prompts
graphical.target loaded active active Graphical Interface
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
nfs-client.target loaded active active NFS client services
nss-user-lookup.target loaded active active User and Group Name Lookups
paths.target loaded active active Paths
remote-fs-pre.target loaded active active Remote File Systems (Pre)
remote-fs.target loaded active active Remote File Systems
slices.target loaded active active Slices
sockets.target loaded active active Sockets
sound.target loaded active active Sound Card
swap.target loaded active active Swap
sysinit.target loaded active active System Initialization
timers.target loaded active active Timers
2)修改默认运行级别
[root@yuki ~]# systemctl get-default
graphical.target
更改运行级别
[root@yuki ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
[root@yuki ~]# systemctl get-default
multi-user.target
可以看到实际为将/etc/systemd/system/default.target进行移除,使用/usr/lib/systemd/system/multi-user.target重新创建/etc/systemd/system/default.target符号连接
3)挂起操作
[root@kasumi ~]# systemctl suspend
4)快照操作
[root@kasumi ~]# systemctl hibernate
5)切换至紧急救援模式
[root@kasumi ~]# systemctl rescue
此时需要键入root密码以此进入紧急救援模式
6)切换至emergency模式
[root@kasumi ~]# systemctl emergency
此时需要键入root密码以此进入emergency模式
1.service unit file组成部分
1.[Unit]:定义与unit类型无关的通用选项;用于提供unit的描述信息,unit行为及依赖关系等
2.[Service]:与特定类型相关的专用选项,此处为service类型
3.[Install]:定义由“systemctl enable”以及“systemctl disable”命令在实现服务启用或禁用时用到的
一些选项
2.unit段的常用选项
1.Description:描述信息;意义性描述
2.After:定义unit的启动次序,表示当前unit应晚于那些unit启动;其功能与Before相反
3.Requies:依赖到的其他units;强依赖,被依赖的units无法激活时,当前unit无法激活
4.Wants:依赖到的其他units;弱依赖,被依赖的units无法激活时,当前unit可以激活
5.Conflicts:定义units间的冲突关系
3.Service段的常用选项
1.Type:用于定义影响ExecStart及相关参数的功能的unit进程启动类型
1)simple:由exec start启动的进程为服务的主进程
2)forKing:由exec start启动的进程生成的一个子进程成为子进程,并且启动完后此父进程会退出
3)oneshot:一次性的,在启动后续的units之前主进程会退出
4)dbus:后续的units仅在主进程取得dbus名称后,才能启动
5)notify:类似于simple,表示后续的units仅在通过notify函数通知后,然后才能运行命令
6)idle:类似于simple
常见的:notify,forKing,simple
2.EnvironmentFile:环境配置文件
3.ExecStart::指明启动unit要运行的命令或脚本;ExecStartPre, ExecStartPost分别为启动unit前与后运行的命令或脚本
4.ExecStop:指明要停止unit要运行的命令或脚本
5.Restart:表示启动此项后,会导致进程退出后自动重启相关服务
4.install段的常用选项:
1.Alias:当前unit的别名
2.RequiredBy:强依赖,被哪些units所依赖,若此unit未启动,则其他units无法启动
3.WantedBy:弱依赖,被哪些units所依赖,若此unit未启动,则其他units可以启动
5.对于新创建的unit文件或修改了的unit文件,要通知systemd重载此配置文件:
1) systemctl daemon-reload