CentOS7下Systemctl详解

2018-05-15  本文已影响0人  风吹我已散博客

一、Systemd简介

Systemd是由红帽公司的一名叫做Lennart Poettering的员工开发,systemd是Linux系统中最新的初始化系统(init),它主要的设计目的是克服Sys V 固有的缺点,提高系统的启动速度,systemd和upstart是竞争对手,ubantu上使用的是upstart的启动方式,centos7上使用systemd替换了Sys V,Systemd目录是要取代Unix时代依赖一直在使用的init系统,兼容SysV和LSB的启动脚本,而且能够在进程启动中更有效地引导加载服务。
system:系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其他进程,根据管理,字母d是守护进程(daemon)的缩写,systemd这个名字的含义就是它要守护整个系统。

二、Systemd新特性

三、Systemd核心概念

四、Unit类型

五、关键特性

六、不兼容

七、Systemd基本工具

监视和控制systemd的主要命令是systemctl。该命令可用于查看系统状态和管理系统及服务。

命令: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

设定某服务开机禁止启动
chkconfig name off –>systemctl disable name.service

查看所有服务的开机自启状态
chkconfig –list–>systemctl list-unit-files -t service
[图片上传失败...(image-fa8cbe-1519920006718)]

chkconfig sshd –list –>ls /etc/system/system/*.wants/sshd.service
[root@www ~]# ls /etc/systemd/system/*.wants/sshd.service
/etc/systemd/system/multi-user.target.wants/sshd.service

CentOS7引导顺序

使用systemctl控制单元时,通常需要使用单元文件的全名,包括扩展名,但是有些单元可以在systemctl中使用简写方式

如果无扩展名,systemctl默认把扩展名当做.service。例如netcfg和netcfg.service是等同的

挂载点会自动转化为相应的.mount单元,例如/home等价于home.mount

设备会自动转化为相应的.device单元,所以/dev/sd2等价于dev-sda2.device
加载initramfs驱动模块

加载内核选项

内核初始化,centos7使用systemd代替init

执行initrd.target所有单元,包括挂载/etc/fstab

从initramfs根文件系统切换到磁盘根目录

systemd执行默认target配置,配置文件/etc/systemd/default.target /etc/systemd/system/

systemd执行sysinit.target初始化系统及basic.target准备操作系统

systemd启动multi-user.target下的本机与服务器服务

systemd执行multi-user.target下的/etc/rc.d/rc.local

systemd执行multi-user.target下的getty.target及登入服务

systemd执行graphical需要的服务(此为图形界面所有)

unit文件格式

以#开头的行后面的内容会被认为是注释
相关布尔值,1、yes、on、ture都是开启,0、no、off、false都是关闭
时间单位默认是秒

Unit文件组成

unit段常用选项

Service段常用选项

Install段常用选项

案例

vim /etc/systemd/system/bak.service

[Unit]
Description=backup my etc
Requires=atd.service

[Service]
Type=simple
ExecStart=/bin/bash -c "echo /testdir/bak.sh|at now"

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start bak

设置内核参数

在系统启动时,进入内核参数修改,修改只影响当次启动,在启动时在linux16行后添加systemd.unit=desired.target
systemd.unit=emergency.target 进入救援模式
system.unit=recure.target 进入紧急救援模式(功能比emergency多)
修改完成后使用ctrl+x启动进入相应的模式

启动排错

破解root口令

启动时任意键暂停启动
按e键进入编辑模式
将光标移动linux16开始的行,添加内核参数rd.break
按ctrl-x启动

mount -o remount,rw /sysroot    
chroot /sysroot    
passwd root    
touch /.autorelabel    
exit   
reboot   

等待系统重新打标签后即可进入系统,这时候root的密码已经更改。


9.png

修复grub2

GRUB“the Grand Unified Bootloader”
引导提示时可以使用命令行界面,可从文件系统引导
主要配置文件/boot/grub2/grub.cfg
修复配置文件
grub2-mkconfig > /boot/grub2/grub.cfg
修复grub

grub2-install /dev/sda          #BIOS环境     
grub2-install                   #UEFI环境   
grub2-mkconfig -o grub.cfg  

重启

博文参考

http://www.178linux.com/48343
http://www.178linux.com/48674
http://www.178linux.com/48563
http://www.jinbuguo.com/systemd/index.html
上一篇下一篇

猜你喜欢

热点阅读