Centos6系统启动流程
POST --> Boot Sequence(BIOS) --> Boot Loader --> Kernel(ramdisk) --> rootfs --> switchroot --> /sbin/init -->(/etc/inittab, /etc/init/*.conf) --> 设定默认运行级别 --> 系统初始化脚本rc.sysinit (/etc/rc.d/rc.sysinit)–> 关闭或启动对应级别的服务(/etc/rc.d/rc*.d/) --> 启动终端
一 硬件启动流程
1.1 BIOS
1.1.1POST 初始化硬件设备,检查系统外围主要设备(CPU、内存、IO)
1.1.2确定启动设备
根据BIOS设置的启动顺序,检查驱动器(硬盘、光盘、U盘、网络)
如果硬盘是启动项,读取硬盘第一个扇区(MBR、512bit)到内存
控制区转给MBR中的bootloader
1.2 MBR
MBR由512字节组成,前446字节为bootloader,中间64字节为分区表,最后2字节55aa为分区标记
执行MBR中的bootloader
MBR由主引导程序、硬盘分区表、有效标记组成
MBR里面安装有/boot/grub/的一部分,在安装时写入
根据grub能够加载内核及文件驱动
[root@centos6 ~]#cat /boot/grub/grub.conf
default=0 #当有多个title时可以选择默认进入内核default=1
timeout=5 #开机间隔5秒载入内核
splashimage=(hd0,0)/grub/splash.xpm.gz #背景 640*480 (hd0,0)表示第一个硬盘第一个分区
hiddenmenu
title CentOS 6 (2.6.32-754.el6.x86_64) #命名,启动菜单显示,可以有多个
root (hd0,0)
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=32b998eb-6eb0-41e3-86a3-d3ceaefdae2d rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=128M KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
#Kernel 内核参数配置 rugb 背景 quiet 不输出明细
initrd /initramfs-2.6.32-754.el6.x86_64.img
#vmlinuz initramfs在/boot中
实验:64字节分区表丢失(备份修复)
[root@localhost ~]#dd if=/dev/zero of=/dev/sda bs=1 count=64 seek=446
[root@localhost ~]#fdisk -l /dev/sda
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ba3f3
Device Boot Start End Blocks Id System
[root@localhost ~]#dd if=/data/part.txt of=/dev/sda bs=1 count=64 skip=446 seek=446
实验:分区标记55aa丢失(备份修复)
[root@localhost ~]#dd if=/dev/sda of=/data/part.txt bs=1 count=512
[root@localhost ~]#dd if=/dev/zero of=/dev/sda bs=1 count=2 seek=510
[root@localhost ~]#fdisk -l /dev/sda
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@localhost ~]#dd if=/data/part.txt of=/dev/sda bs=1 count=2 skip=510 seek=510
2+0 records in
2+0 records out
2 bytes (2 B) copied, 0.000120493 s, 16.6 kB/s
[root@localhost ~]#fdisk -l /dev/sda
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ba3f3
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 211814399 104857600 83 Linux
/dev/sda3 211814400 216008703 2097152 82 Linux swap / Solaris
/dev/sda4 216008704 419430399 101710848 5 Extended
/dev/sda5 216010752 320868351 52428800 83 Linux
实验:前446字节bootloader丢失(备份修复)
[root@localhost ~]#dd if=/dev/zero of=/dev/sda bs=1 count=446
[root@localhost ~]#dd if=/data/part.txt of=/dev/sda bs=1 count=512
以上实验可以通过光盘进入拯救系统拷贝文件修复
二 GRUB启动引导阶段
2.1 Stage1阶段
stage1是安装时写入MBR中的
因为MBR空间有限,因此,MBR中安装bootloader的最小程序
主要作用是系统启动装在stage2
实验:stage1阶段bootloader破坏
[root@localhost ~]#dd if=/dev/zero of=/dev/sda bs=1 count=446
[root@localhost ~]#reboot
开机进入光盘启动(硬盘启动不能识别)
一、/boot/grub/存在
二、/boot/grub/删除
chroot /mnt/sysimage
grub-install /dev/sda
由于/boot/grub/grub.conf没有,不能进入系统,开机后手工输入(临时)以下内容,进入系统后修改文件
编写/boot/grub/grub.conf
[root@CentOS6 ~]# cat /boot/grub/grub.conf
default=0
timeout=5
title test linux
kernel /vmlinuz-2.6.32-754.el6.x86_64 root=UUID=54cfd3b3-28e0-43ce-813a-2957c26d429d
initrd /initramfs-2.6.32-754.el6.x86_64.img
2.2 Stage1.5阶段
stage1.5是在MBR后面的分区,
stage1.5能够识别文件系统,
stage1.5是stage1和2的桥梁;
grub访问/boot/grub/stage2,将stage2载入内存并执行
实验:stage1.5阶段MBR后面部分破坏
[root@CentOS6 ~]# dd if=/dev/zero of=/dev/sda bs=1 count=10240 seek=512
/boot文件并没有破坏,只需写入
grub-install /dev/sda
2.3 Stage2阶段
解析grub的配置文件/boot/grub/grub.conf,
显示操作系统启动菜单,
加载内核镜像到内存,
通过/boot/initramfs-2.6.32-754.el6.x86_64.img加载虚拟文件系统,
转交给内核
stage2阶段/boot/grub/grub.conf破坏
实验:破解用户口令
在启动阶段按ESC进入以下界面
a:内核参数
e: 编辑模式,用于编辑菜单
c: 命令模式,交互式接口
输入a后输入1(s,S,single)进入单用户模式(维护模式),不要密码直接进入系统,可以修改root口令
实验:增加密码进入单用户模式
由于单用户模式可以直接进入系统,因此系统变得不安全,在此之前添加口令进入维护模式,可以增加系统安全
[root@CentOS6 ~]# cat /boot/grub/grub.conf
default=0
timeout=5
password mage #增加明码
title test linux
kernel /vmlinuz-2.6.32-754.el6.x86_64 root=UUID=54cfd3b3-28e0-43ce-813a-2957c26d429d
initrd /initramfs-2.6.32-754.el6.x86_64.img
[root@CentOS6 ~]# openssl passwd -1
Password:
Verifying - Password:
$1$/ryP.BCO$XthwAFs.FHkpd6ZCdrJK30
[root@CentOS6 ~]# cat /boot/grub/grub.conf
default=0
timeout=5
password --md5 $1$/ryP.BCO$XthwAFs.FHkpd6ZCdrJK30 #增加md5加密密码mage
title test linux
kernel /vmlinuz-2.6.32-754.el6.x86_64 root=UUID=54cfd3b3-28e0-43ce-813a-2957c26d429d
initrd /initramfs-2.6.32-754.el6.x86_64.img
[root@CentOS6 ~]# grub-crypt
Password:
Retype password:
$6$6RbG9Sqp1IAoVXV7$xmnOHpZ43Es7Wf4Jjvml8fnste.8QO1ps0.nDgWPRSAwed3yvckQzAVIrPpDaiy.x0ijUiK9P2y2JApr6qTNq.
[root@CentOS6 ~]# cat /boot/grub/grub.conf
default=0
timeout=5
password --encrypted $6$6RbG9Sqp1IAoVXV7$xmnOHpZ43Es7Wf4Jjvml8fnste.8QO1ps0.nDgWPRSAwed3yvckQzAVIrPpDaiy.x0ijUiK9P2y2JApr6qTNq. #增加sha512加密密码mage
title test linux
kernel /vmlinuz-2.6.32-754.el6.x86_64 root=UUID=54cfd3b3-28e0-43ce-813a-2957c26d429d
initrd /initramfs-2.6.32-754.el6.x86_64.img
这些针对光盘救援没用,光盘救援可以破解一切
三 内核引导阶段
调用虚拟根文件系统中的init
探测可识别到的所有硬件设备
加载硬件驱动程序(借助于ramdisk加载驱动)
以只读方式挂载根文件系统
加载内核/boot/vmlinuz-2.6.32-754.el6.x86_64
运行用户空间的第一个应用程序:/sbin/init
##### mkinitrd
##### grub
实验:/boot/initramfs-2.6.32-754.el6.x86_64.img丢失
什么也没有,光标闪烁
进入拯救模式 rescue installed system
chroot /mnt/sysimage
pwd
mkinitrd /boot/initramfs-`uname -r`.img `uname -r`
ls /boot/
exit
reboot
实验:/boot/vmlinuz-2.6.32-754.el6.x86_64丢失
/boot/vmlinuz-2.6.32-754.el6.x86_64是系统启动依赖内核
进入拯救模式 rescue installed system
mkdir /mnt/sr0
mount /dev/sr0 /mnt/sr0
cp /mnt/sr0/isolinux/vmliuz /mnt/sysimage/boot/vmliuz-`uname -r`
ls /mnt/sysimage/boot/
reboot
实验:基于分区,删除/etc/fstab、/boot 修复之
[root@CentOS6 ~]# rm /etc/fstab
[root@CentOS6 ~]# rm -rf /boot/*
###进入救援模式
bash-4.1# mkdir /mnt/tmp
bash-4.1# blkid
bash-4.1# mount /dev/sda2 /mnt/tmp
bash-4.1# ls /mnt/tmp
bash-4.1# lsblk
bash-4.1# vi /mnt/tmp/etc/fstab
/dev/sda2 / xfs defaults 0 0
/dev/sda1 /boot xfs defaults 0 0
/dev/sda3 /data xfs defaults 0 0
/dev/sda5 swap swap defaults 0 0
bash-4.1#reboot
######重启
bash-4.1# chroot /mnt/sysimage
sh-4.1# mount /dev/sr0 /mnt
sh-4.1# rpm -ivh /mnt/Packages/kernel-2.6.32-754.e16.x86_64.rpm --force ###强制安装内核
###另一种方法见上面两个实验
sh-4.1# sync
sh-4.1# grub-install /dev/sda
sh-4.1# vim /boot/grub/grub.conf ##另一种为开机进入grub临时写入,进入系统修改
default=0
timeout=5
title test linux
kernel /vmlinuz-2.6.32-754.el6.x86_64 root=/dev/sda2
initrd /initramfs-2.6.32-754.el6.x86_64.img
实验:基于逻辑卷,删除/etc/fstab、/boot 修复之
bash-4.1# blkid #查看硬盘UUID
bash-4.1# lvs #查看逻辑卷
bash-4.1# vgdisplay #显示卷组
bash-4.1# vgchange -ay VolGroup #激活卷组
bash-4.1# blkid
bash-4.1# mkdir /mnt/tmp
bash-4.1# mount /dev/mapper/VolGroup-lv_root /mnt/tmp
bash-4.1# cat >/mnt/tmp/etc/fstab
/dev/sda1 /boot ext4 defaults 0 0
/dev/mapper/VolGroup-lv_root / ext4 defaults 0 0
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
bash-4.1# reboot
###重启进入救援系统
bash-4.1#chroot /mnt/sysimage
sh-4.1# mount /dev/sr0 /mnt
sh-4.1# rpm -ivh /mnt/Packages/kernel-2.6.32-754.e16.x86_64.rpm --force ###强制安装内核
###另一种方法见上面两个实验
sh-4.1# sync
sh-4.1# grub-install /dev/sda
sh-4.1# vim /boot/grub/grub.conf ##另一种为开机进入grub临时写入,进入系统修改
default=0
timeout=5
title test linux
kernel /vmlinuz-2.6.32-754.el6.x86_64 root=/dev/VolGroup/lv_root
initrd /initramfs-2.6.32-754.el6.x86_64.img
核心文件:/boot/vmlinuz-VERSION-release
ramdisk:辅助的伪根系统
CentOS 5 /boot/initrd-VERSION-release.img
CentOS 6,7 /boot/initramfs-VERSION-release.img
四 系统初始化阶段
4.1 /etc/inittab获取用户级别
初始运行级别(RUN LEVEL)
系统初始化脚本
对应运行级别的脚本目录
捕获某个关键字顺序
定义UPS电源终端/恢复脚本
在虚拟控制台生成getty
在运行级别5初始化X
4.1.1 SysV: init,CentOS 5之前
配置文件:/etc/inittab
[root@centos6 ~]#cat /etc/inittab
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode #单用户模式(维护模式)
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking) #无网络
# 3 - Full multiuser mode #多用户模式
# 4 - unused #保留
# 5 - X11 #图形
# 6 - reboot (Do NOT set initdefault to this)
id:5:initdefault: #默认启动模式
si::sysinit:/etc/rc.d/rc.sysinit #初始化脚本配置
l0:0:wait:/etc/rc.d/rc 0 #根据模式启动相应文件夹服务
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
ca::ctrlaltdel:/sbin/shutdown -t3 -r now #重启
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down” #断电后2小时关机
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled”
1:2345:respawn:/sbin/mingetty tty1 #启动相应模式的终端
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
x:5:respawn:/etc/X11/prefdm -nodaemon #5模式为图形界面,可以修改为其他数字如x:3:respawn:/etc/X11/prefdm -nodaemo
4.1.2 Upstart: init,CentOS 6
配置文件:/etc/inittab, /etc/init/*.conf
[root@centos6 ~]#cat /etc/inittab
id:3:initdefault:
4.1.3 Systemd:systemd, CentOS 7
配置文件:/usr/lib/systemd/system/
/etc/systemd/system/
实验:/etc/inittab更改默认级别为0或6,实现开机及关或一直重启
在此界面按ESC键进入menu
输入a后输入5进入系统后修改/etc/init参数
[root@CentOS6 ~]# sed -ri '/^id/s#id:0#id:5#' /etc/inittab
[root@CentOS6 ~]# cat /etc/inittab
id:5:initdefault:
4.2 /etc/rc.d/rc.sysinit: 系统初始化脚本
获取网络环境与主机类型
打印文本欢迎信息
决定是否启动selinux
测试与载入内存设备/proc及USB设备/sys
接口设备的检查与即插即用PnP参数的测试
挂载所有在/etc/fstab定义的文件系统
加载核心的相关设置,按/etc/sysctl.conf设定内核参数的值
设置系统时间
设置终端控制台的字形
设置RAID与LVM等硬盘功能
以fack检查磁盘文件系统
进行磁盘配额quota的转换
重新以可读取模式载入系统磁盘、
启动quota功能
启动系统随机数设备
清除启动过程中的临时文件
将启动相关信息加载到/var/log/dmesg中
4.3 加载系统服务
4.3.1 /etc/rc.d/rc*.d
根据运行级别(/etc/inittab默认级别)按顺序启动相应目录下的服务(*为1-6),S开头为启动K开始为停止
ntsysv
centos5设置服务在所有级别的启动或关闭(图形界面)
chkconfig
查看服务在所有级别的启动或关闭设定情形
chkconfig [–level levels] name <on|off|reset>
–level LLLL: 指定要设置的级别;省略时表示2345
xinetd
超级守护进程,监听服务,有些进程平时使用少,开机启动占资源,不启动需要用一两下,xinetd能监听到使用请求时打开相应服务,没有请求关闭服务
[root@CentOS6 ~]# yum install telnet-server
[root@CentOS6 ~]# chkconfig #查看服务
xinetd based services:
chargen-dgram: off #为off时,xinetd不能控制该服务,需改为on,两种方法
chargen-stream: off
daytime-dgram: off
daytime-stream: off
discard-dgram: off
discard-stream: off
echo-dgram: off
echo-stream: off
rsync: off
tcpmux-server: off
telnet: off
time-dgram: off
time-stream: off
[root@CentOS6 ~]# chkconfig --list telnet
telnet off
[root@CentOS6 ~]# vim /etc/xinetd.d/telnet
disable = yes #一、改为on
[root@CentOS6 ~]# chkconfig telnet on #二、修改
[root@CentOS6 ~]# chkconfig --list telnet
telnet on
[root@CentOS6 ~]# service xinetd status
xinetd is stopped
[root@CentOS6 ~]# service xinetd start
Starting xinetd: [ OK ]
[root@CentOS6 ~]# lsof -i :23
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
xinetd 3294 root 5u IPv6 20483 0t0 TCP *:telnet (LISTEN) #xinetd监听端口
centos7由systemd监听
实验:编写服务
[root@CentOS6 ~]# cat /etc/init.d/testrv
#! /bin/bash
# chkconfig: 345 98 03 #345为启动模式 -时为禁止启动 98为启动编号,3为禁止编号
# description:test service
. /etc/init.d/functions
start (){
touch /var/lock/subsys/testrv
action "starting `basename $0`"
}
stop (){
rm -f /var/lock/subsys/testrv
action "stoping `basename $0`"
}
status (){
[ -e /var/lock/subsys/testrv ] && echo `basename $0` is running || echo `basename $0` is stoped
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac
[root@CentOS6 ~]# chkconfig --list #列出所有服务
NetworkManager 0:off 1:off 2:on 3:on 4:on 5:on 6:off
abrt-ccpp 0:off 1:off 2:off 3:on 4:off 5:on 6:off
[root@CentOS6 ~]# chkconfig --del testrv #删除服务
[root@CentOS6 ~]# chkconfig --add testrv #增加服务
[root@CentOS6 ~]# chkconfig --list testrv #列出指定服务
testrv 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[root@CentOS6 ~]# chkconfig --level 345 testrv off #345模式关闭
[root@CentOS6 ~]# chkconfig testrv on #2345模式开启
[root@CentOS6 ~]# ls /etc/rc.d/rc*.d/*testrv -l
lrwxrwxrwx. 1 root root 16 May 20 13:15 /etc/rc.d/rc0.d/K03testrv -> ../init.d/testrv
lrwxrwxrwx. 1 root root 16 May 20 13:15 /etc/rc.d/rc1.d/K03testrv -> ../init.d/testrv
lrwxrwxrwx. 1 root root 16 May 20 13:16 /etc/rc.d/rc2.d/S98testrv -> ../init.d/testrv
lrwxrwxrwx. 1 root root 16 May 20 13:16 /etc/rc.d/rc3.d/S98testrv -> ../init.d/testrv
lrwxrwxrwx. 1 root root 16 May 20 13:16 /etc/rc.d/rc4.d/S98testrv -> ../init.d/testrv
lrwxrwxrwx. 1 root root 16 May 20 13:16 /etc/rc.d/rc5.d/S98testrv -> ../init.d/testrv
lrwxrwxrwx. 1 root root 16 May 20 13:15 /etc/rc.d/rc6.d/K03testrv -> ../init.d/testrv
[root@CentOS6 ~]# /etc/init.d/testrv start #开启服务方法一
starting testrv [ OK ]
[root@CentOS6 ~]# service testrv restart #开启服务方法二
stoping testrv [ OK ]
starting testrv [ OK ]
实验:服务出问题,单用户禁止启动,不能进入系统
[root@CentOS6 ~]# chkconfig --list testrv
testrv 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[root@CentOS6 ~]# cat /etc/init.d/testrv
#! /bin/bash
# chkconfig: 345 98 03 #345为启动模式 -时为禁止启动 98为启动编号,3为禁止编号
# description:test service
. /etc/init.d/functions
start (){
touch /var/lock/subsys/testrv
action "starting `basename $0`"
sleep 40000 #模拟出问题
}
在开机菜单输入a 1 进入单用户模式,临时进入系统关闭服务(一般服务单用户模式下不启动)
实验:服务出问题,单用户开机启动出问题,不能进入系统
[root@CentOS6 ~]# chkconfig --list testrv
testrv 0:off 1:on 2:off 3:on 4:on 5:on 6:off
一种方法:
在选择菜单输入a 输入/bin/bash
系统的第一个进程/sbin/init 会读取各种服务,更改init为/bin/bash(系统的第一进程为bash,绕过服务),
4.3.2 /etc/rc.local
正常级别下,最后启动一个服务S99local没有链接至/etc/rc.d/init.d一个服务脚本,而是指向了/etc/rc.d/rc.local脚本
不便或不需写为服务脚本放置于/etc/rc.d/init.d/目录,且又想开机时自动运行的命令,可直接放置于/etc/rc.d/rc.local文件中
/etc/rc.d/rc.local在指定运行级别脚本后运行
可以根据情况,进行自定义修改
五 启动终端
5.1 默认情况下执行/sbin/mingetty打开6个纯文本终端
5.1.1 验证登录
/etc/nologin (可能没有)用户在该文件中不允许登录 #可以创建文件添加用户实现禁止用户登录
/etc/usertty 对文件作出附近访问限制,不存在没有限制
/etc/securetty 登记的终端才允许使用,没有文件任何终端都可以登录
/etc/passwd /etc/shadow 验证密码、设置账户的主目录‘默认shell’
5.1.2 登录成功
5.1.2.1 屏幕输出
/var/log/lastlog 输出最近一次登录的信息
/var/spool/mail 检查用户是否有新邮件
5.1.2.2 加载环境变量
全局配置 | 个人配置 |
---|---|
/etc/profile | ~/.bash_profile |
/etc/profile.d/*.sh | ~/.bashrc |
/etc/bashrc |
交互式登录
直接通过终端输入账号密码登录
使用“su - UserName” 切换的用户
/etc/profile
/etc/profile.d/*.sh #常使用
~/.bash_profile
~/.bashrc #常使用
/etc/bashrc
非交互式登录
su UserName
图形界面下打开的终端
执行脚本
任何其它的bash实例
/etc/profile.d/*.sh
/etc/bashrc
~/.bashrc
5.2 如果级别为5,打开X-window
Centos7系统启动流程
UEFi或BIOS初始化,运行POST开机自检
选择启动设备
引导装载程序, centos7是grub2
加载装载程序的配置文件:
/etc/grub.d/
/etc/default/grub
/boot/grub2/grub.cfg
加载initramfs驱动模块
加载内核选项
内核初始化,centos7使用systemd代替init
执行initrd.target所有单元,包括挂载/etc/fstab
从initramfs根文件系统切换到磁盘根目录
systemd执行默认target配置,配置文件/etc/systemd/system/default.target
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需要的服务
systemd
Systemd:系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其它进程
Systemd新特性
系统引导时实现服务并行启动
centos 5 串行启动
centos6 依赖关系 串行启动 无依赖关系 并行启动
centos7 并行启动
按需启动守护进程
自动化的服务依赖关系管理
同时采用socket式与D-Bus总线式激活服务
系统状态快照
systemd服务配置文件
/usr/lib/systemd/system:每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/,
/run/systemd/system:系统执行过程中所产生的服务脚本,比上面目录优先运行
/etc/systemd/system:管理员建立的执行脚本,类似于/etc/rcN.d/Sxx的功能,比上面目录优先运行
[root@localhost ~]#systemctl -t help #查看unit类型
Available unit types:
service #用于定义系统服务
socket
busname
target #用于模拟实现运行级别
snapshot
device #用于定义内核识别的识别
mount
automount #文件系统的自动挂载点,相当于autofs
swap
timer
path
slice
scope
systemctl命令固定不变,不可扩展,service命令自定义,可扩展
systemctl可同时启动多个服务,service只能启动一个服务
非由systemd启动的服务,systemctl无法与之通信和控制
proc
内核把自己内部状态信息及统计信息,以及可配置参数通过proc伪文件系统加以输出
sysctl
将/proc的值写入/etc/sysctl.conf中永久生效
sysctl -w 临时生效
[root@localhost ~]#sysctl -a |head -n3
abi.vsyscall32 = 1
crypto.fips_enabled = 0
debug.exception-trace = 1
[root@localhost ~]#sysctl -a |grep ip_for
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_use_pmtu = 0
[root@localhost ~]#cat /proc/sys/net/ipv4/ip_forward
1 #表示禁用
[root@localhost ~]#cat /proc/sys/net/ipv4/icmp_echo_ignore_all
0 #表示启用
[root@localhost ~]#sysctl -w net.ipv4.icmp_echo_ignore_all=1
net.ipv4.icmp_echo_ignore_all = 1
##注意. /区别表示方法从/proc/sys/后面用.表示/
[root@localhost ~]#cat /proc/sys/net/ipv4/icmp_echo_ignore_all
1
[root@localhost ~]#sysctl -p /proc/sys/net/ipv4/icmp_echo_ignore_all #重新读取
###centos7 net.ipv4.icmp_echo_ignore_all=1直接写入/etc/sysctl.conf
###centos6 直接修改即可
[root@CentOS6 ~]# cat /etc/sysctl.conf
# Controls IP packet forwarding
net.ipv4.ip_forward = 0
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
sys
为用户使用的伪文件系统,输出内核识别出的各硬件设备的相关属性信息,也有内核对硬件特性的设定信息;有些参数是可以修改的,用于调整硬件工作特性
systemctl
[root@localhost ~]#yum install httpd #安装
[root@localhost ~]#systemctl status httpd #查看状态
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) #disabled 开机状态 vendor····安装时状态
Active: inactive (dead) #未激活
Docs: man:httpd(8)
man:apachectl(8)
[root@localhost ~]#systemctl start httpd #开启
[root@localhost ~]#systemctl stop httpd #关闭
[root@localhost ~]#systemctl restart httpd #重启
[root@localhost ~]#systemctl enable httpd #开机启动
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. #创建软连接
[root@localhost ~]#systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
[root@localhost ~]#systemctl disable httpd #禁止开机启动
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service. #删除软连接
[root@localhost ~]#systemctl mask httpd #禁止启动
Created symlink from /etc/systemd/system/httpd.service to /dev/null. #创建软连接指向空
[root@localhost ~]#systemctl start httpd
Failed to start httpd.service: Unit is masked.
[root@localhost ~]#systemctl unmask httpd #关闭禁止启动
Removed symlink /etc/systemd/system/httpd.service. #删除软连接
[root@localhost ~]#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
[root@localhost ~]#systemctl list-units --type service -a #查看所有服务
[root@localhost ~]#systemctl is-active httpd 查看服务当前激活与否的状态
unknown
[root@localhost ~]#systemctl start httpd
[root@localhost ~]#systemctl is-active httpd
active
service unit文件格式
/etc/systemd/system:系统管理员和用户使用/usr/lib/systemd/system:发行版打包者使用
以 “#” 开头的行后面的内容会被认为是注释
相关布尔值,1、yes、on、true 都是开启,0、no、off、false 都是关闭
时间单位默认是秒,所以要用毫秒(ms)分钟(m)等须显式说明
Unit
Unit段的常用选项:
Description:描述信息
After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反
Requires:依赖到的其它units,强依赖,被依赖的units无法激活时,当前unit也无法激活
Wants:依赖到的其它units,弱依赖
Conflicts:定义units间的冲突关系
Service
EnvironmentFile:环境配置文件
ExecStart:指明启动unit要运行命令或脚本的绝对路径
ExecStartPre: ExecStart前运行
ExecStartPost: ExecStart后运行
ExecStop:指明停止unit要运行的命令或脚本
Restart:当设定Restart=1 时,则当次daemon服务意外终止后,会再次自动启动此服务
Install
Alias:别名,可使用systemctl command Alias.service
RequiredBy:被哪些units所依赖,强依赖
WantedBy:被哪些units所依赖,弱依赖
Also:安装本服务的时候还要安装别的相关服务
实验:编辑服务
[root@localhost /data]#cat bak.sh #编写脚本 备份etc
cp -av /etc /data/
tar cf etc.tar etc
rm -rf /data/etc
[root@localhost /data]#cat /etc/systemd/system/bak.service
[Unit]
Description=backup /etc
Requires=atd.service
[Service]
Type=simple
ExecStart=/bin/bash -c "echo /data/bak.sh|at now"
[Install]
WantedBy=multi-user.target
You have new mail in /var/spool/mail/root
[root@localhost /data]#systemctl daemon-reload #systemd重载此配置文件
[root@localhost /data]#systemctl start bak #开启服务
[root@localhost /data]#ls #备份etc
bak.sh etc.tar
[root@localhost /data]#systemctl status bak #状态
● bak.service - backup /etc
Loaded: loaded (/etc/systemd/system/bak.service; disabled; vendor preset: disabled)
Active: inactive (dead) #由于服务为复制etc,开启后复制文件,可以查看
May 23 21:03:39 localhost.localdomain systemd[1]: Started backup /etc.
May 23 21:03:39 localhost.localdomain bash[2966]: job 1 at Sat May 23 21:03:00 2020
May 23 21:15:20 localhost.localdomain systemd[1]: Started backup /etc.
May 23 21:15:20 localhost.localdomain bash[3612]: job 2 at Sat May 23 21:15:00 2020
运行级别
unit配置文件:.target
ls /usr/lib/systemd/system/*.target
systemctl list-unit-files --type target --all
运行级别:
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
实验:设置默认级别为6后修复
[root@localhost ~]#systemctl get-default
graphical.target
[root@localhost ~]#systemctl set-default reboot.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/reboot.target.
启动时,在linux16行后添加systemd.unit=rescue.target
Ctrl+x 进入系统后输入root口令
[root@localhost ~]#systemctl set-default multi-user.target
[root@localhost ~]#reboot
[root@localhost ~]#who -r #一、查看运行级别
run-level 3 2020-05-24 09:35
[root@localhost ~]#runlevel #二、查看运行级别
N 3
[root@localhost ~]#systemctl isolate graphical.target #一、切换运行级别
[root@localhost ~]#runlevel
3 5
[root@localhost ~]#init 3 #二、切换运行级别
[root@localhost ~]#runlevel
5 3
关机相关命令
传统命令init,poweroff,halt,reboot都成为
systemctl的软链接
关机:systemctl halt、systemctl poweroff
重启:systemctl reboot
挂起:systemctl suspend
休眠:systemctl hibernate
休眠并挂起:systemctl hybrid-sleep
实验:破解root口令
方法一
启动时任意键暂停启动
按e键进入编辑模式
将光标移动linux16开始的行,后面添加内核参数rd.break
按ctrl-x启动
mount –o remount,rw /sysroot
chroot /sysroot
passwd root
touch /.autorelabel #逻辑卷才使用
exit
reboot
方法二
启动时任意键暂停启动
按e键进入编辑模式
将光标移动linux16开始的行,后面添加rw init=/sysroot/bin/sh
按ctrl-x启动
chroot /sysroot
passwd root
touch /.autorelabel #逻辑卷才使用
exit
reboot
实验:加grub口令
[root@localhost ~]#grub2-setpassword
Enter password:
Confirm password:
[root@localhost ~]#ls /boot/grub2 #新增口令后增加user.cfg
device.map fonts grub.cfg grubenv i386-pc locale user.cfg
##将user.cfg移除grub口令失效
实验:删除/boot修复
[root@localhost ~]#rm -rf /boot/*
###进入光盘救援模式
sh-4.2# chroot /mnt/sysimage
bash-4.2# mount /dev/sr0 /mnt
bash-4.2# rpm -ivh /mnt/Packages/kernel-3.10.0-1062.el7.x86_64.rpm --force #强制安装内核相关文件
bash-4.2# grub2-install /dev/sda #安装grub
bash-4.2# grub2-mkconfig -o /boot/grub2/grub.cfg #根据/etc/default/grub模板生成grub.cfg
bash-4.2# exit
sh-4.2# reboot