首页 > 代码库 > 操作系统---Systemd
操作系统---Systemd
Systemd
概述:
CentOS 6和之前版本采用SysVinit的系统启动进程管理体系,一般用户都可通过在/etc/inittab文件的配置,来个性化自己的系统启动序列。但也经常会由于特殊环境的硬件等关系问题,造成其串行的启动进程控制流,因为可能任务的阻塞而影响启动过程。
CentOS 7开始使用SystemD,所以我们必须要了解SystemD.本章将从CentOS 7 的启动流程、Unit、服务管理,启动排错,破解口令以及修复grub2 等方面来介绍Systemd的相关内容。
1.Systemd介绍:
1)启动流程
POST --> Boot Sequence --> Bootloader --> kernel + initramfs(initrd) --> rootfs(根切换)--> /sbin/init
init:
init:CentOS 5: SysVinit;
CentOS 6: Upstart;
CentOS 7: Systemd;
2)Systemd:
系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其它进程;
3)Systemd新特性
系统引导时实现服务并行启动;
按需启动守护进程;
自动化的服务依赖关系管理;
同时采用socket式与D-Bus总线式激活服务;
系统状态快照。
2.核心概念:Unit
unit表示不同类型的systemd对象,通过配置文件进行标识和配置;
文件中主要包含了系统服务、监听socket、保存的系统快照以及其它与init相关的信息;
3.配置文件
/usr/lib/systemd/system:每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/
/run/systemd/system:系统执行过程中所产生的服务脚本,比上面目录优先运行
/etc/systemd/system:管理员建立的执行脚本,类似于/etc/rc.d/rcN.d/Sxx类的功能,比上面目录优先运行
[root@centos7 ~]# cd /usr/lib/systemd/system [root@centos7 system]# ls abrt-ccpp.service gdm.service ntpdate.service sys-fs-fuse-connections.mount abrtd.service geoclue.service oddjobd.service sysinit.target abrt-oops.service getty@.service paths.target sysinit.target.wants abrt-pstoreoops.service getty.target plymouth-halt.service sys-kernel-config.mount abrt-vmcore.service graphical.target plymouth-kexec.service sys-kernel-debug.mount abrt-xorg.service graphical.target.wants plymouth-poweroff.service syslog.socket accounts-daemon.service gssproxy.service plymouth-quit.service syslog.target.wants alsa-restore.service halt-local.service plymouth-quit-wait.service sysstat.service alsa-state.service halt.target plymouth-read-write.service systemd-ask-password-console.path alsa-store.service halt.target.wants plymouth-reboot.service systemd-ask-password-console.service anaconda-direct.service hibernate.target plymouth-start.service systemd-ask-password-plymouth.path anaconda-nm-config.service htcacheclean.service plymouth-switch-root.service systemd-ask-password-plymouth.service anaconda-noshell.service httpd.service polkit.service systemd-ask-password-wall.path anaconda.service hybrid-sleep.target postfix.service systemd-ask-password-wall.service anaconda-shell@.service initial-setup-graphical.service poweroff.target systemd-backlight@.service anaconda-sshd.service initial-setup-text.service poweroff.target.wants s
4.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 目录
文件如下:
[root@centos7 ~]# systemctl -t help Available unit types: service socket busname target snapshot device mount automount swap timer path slice scope
5.特性
1)关键特性:
基于socket的激活机制:socket与服务程序分离
基于d-bus的激活机制:
基于device的激活机制:
基于path的激活机制:
系统快照:保存各unit的当前状态信息于持久存储设备中
向后兼容sysvinit脚本
2)不兼容
systemctl命令固定不变,不可扩展
非由systemd启动的服务,systemctl无法与之通信和控制
6.管理服务
1)管理系统服务
CentOS 7: service unit
注意:能兼容早期的服务脚本
命令:systemctl COMMAND name.service
启动: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.service
重载或条件式重启服务:
systemctl reload-or-try-restart name.service
禁止某服务设定为自动和手动启动:
systemctl mask name.service
取消禁止:
systemctl unmask name.service
示例:
[root@CentOS6 ~]# service httpd status # CentOS 6 显示的状态信息 httpd is stopped [root@centos7 ~]# systemctl status httpd.service # CentOS 7 显示的状态信息 ● 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)
2)服务查看
查看某服务当前激活与否的状态
systemctl is-active name.service
查看所有已经激活的服务:
systemctl list-units --type|-t service
查看所有服务(已激活及未激活):
systemctl list-units --type|-t service –all|-a
示例:
[root@centos7 ~]# systemctl is-active httpd.service # 查看某服务当前激活与否的状态 active [root@centos7 ~]# systemctl stop httpd.service [root@centos7 ~]# systemctl is-active httpd.service unknown [root@centos7 ~]# systemctl list-units -t 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 alsa-state.service loaded active running Manage Sound Card State (restore and store) atd.service loaded active running Job spooling tools auditd.service loaded active running Security Auditing Service autofs.service loaded active running Automounts filesystems on demand blk-availability.service loaded active exited Availability of block devices chronyd.service loaded active running NTP client/server crond.service loaded active running Command Scheduler cups.service loaded active running CUPS Printing Service dbus.service loaded active running D-Bus System Message Bus getty@tty1.service loaded active running Getty on tty1 [root@centos7 ~]# systemctl list-units -t service --all # 查看所有服务状态 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 abrt-xorg.service loaded active running ABRT Xorg log watcher abrtd.service loaded active running ABRT Automated Bug Reporting Tool accounts-daemon.service loaded inactive dead Accounts Service alsa-restore.service loaded inactive dead Restore Sound Card State
3)服务状态:
systemctl list-units --type service --all显示状态
loaded:Unit配置文件已处理
active(running):一次或多次持续处理的运行
active(exited):成功完成一次性的配置
active(waiting):运行中,等待一个事件
inactive:不运行
enabled:开机启动
disabled:开机不启动
static:开机不启动,但可被另一个启用的服务激活
命令的对应关系
设定某服务开机自启:
chkconfig name on ==> systemctl enable name.service
设定某服务开机禁止启动:
chkconfig name off ==> systemctl disable name.service
查看所有服务的开机自启状态:
chkconfig --list ==> systemctl list-unit-files --type service
用来列出该服务在哪些运行级别下启用和禁用
chkconfig sshd –list==> ls /etc/systemd/system/*.wants/sshd.service
查看服务能否开机自启:
chkconfig --list name ==> systemctl is-enabled name.service
其他命令
查看服务的依赖关系:
systemctll ist-dependencies name.service
杀掉进程:
systemctl kill 进程名
示例:
[root@centos7 ~]# systemctl is-enabled httpd disabled [root@centos7 ~]# systemctl is-enabled sshd # 查看某服务能否开机自启 enabled
systemctl示例:
·显示所有单元状态
systemctl 或systemctl list-units
·只显示服务单元的状态
systemctl--type=service
·显示sshd服务单元
systemctl status sshd.service–l
·验证sshd服务当前是否活动
systemctlis-active sshd
·启动,停止和重启sshd服务
systemctl start sshd.service
systemctl stop sshd.service
systemctl restart sshd.service
·重新加载配置
systemctlreload sshd.service
·列出活动状态的所有服务单元
systemctllist-units --type=service
·列出所有服务单元
systemctllist-units --type=service --all
·查看服务单元的启用和禁用状态。
systemctllist-unit-files --type=service
·列出失败的服务
systemctl--failed --type=service
·列出依赖的单元
systemctllist-dependencies sshd
·验证sshd服务是否开机启动
systemctlis-enabled sshd
·禁用network,使之不能自动启动,但手动可以
systemcltdisable network
·启用network
systemctlenable network
·禁用network,使之不能手动或自动启动
systemcltmask network
·启用network
systemctlumasknetwork
运行级别(管理target unit)
★ 级别切换:init N ==> systemctlisolate name.target
systemctl isolate multi-user.target
注:只有/lib/systemd/system/*.target文件中AllowIsolate=yes 才能切换(修改文件需执行systemctldaemon-reload才能生效)
★ 查看target:
runlevelwho -r
systemctllist-units --type target
★ 获取默认运行级别:
/etc/inittab==> systemctlget-default
★ 修改默认级别:
/etc/inittab==> systemctlset-default name.target
systemctl set-default multi-user.target
ls –l /etc/systemd/system/default.target
其他命令
★ 切换至紧急救援模式:
·systemctl rescue
★ 切换至emergency模式:
·systemctlemergency
★ 其它常用命令:
传统命令init,poweroff,halt,reboot都成为systemctl的软链接
·关机:systemctlhalt、systemctlpoweroff
·重启:systemctlreboot
·挂起:systemctlsuspend
·休眠:systemctlhibernate
·休眠并挂起:systemctlhybrid-sleep
操作系统---Systemd