0
点赞
收藏
分享

微信扫一扫

开机启动流程

一.CentOS6启动流程

1.内核引导

  • BIOS加电自检,检测主板上的硬件设备,根据BIOS设置启动系统
  • 读取/boot分区下,所有内核文件

2.运行init

#init:
sysV:CentOS5之前使用的pid为1的进程
Upstart:CentOS6使用的init进程
Systemd:CentOS7

## 加载init,涉及到Linux系统中7个运行级别
## 查看Linux系统当前的运行级别
[root@Quanyi <sub>]# runlevel
N 3

## 7个运行级别
0:关机运行级别
1:单用户运行级别
2:多用户运行级别,没有文件系统
3:多用户运行级别,有文件系统(命令行模式)
4:CentOS6和CentOS7 保留,未使用
5:多用户,图形化运行级别
6:重启运行级别

## 运行init,会读取 /etc/inittab配置文件
## 读取配置文件中的默认运行级别
## 获取当前系统的运行级别
CentOS6
id:3:initdefault:

CentOS7
multi-user.target
[root@Quanyi </sub>]# cat /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target

启动init进程

3.系统初始化

## 根据运行级别中设置了开机自启动服务来启动对应服务
CentOS6
[root@localhost <sub>]# chkconfig --list

CentOS7
systemctl list-unit-files
[root@Quanyi </sub>]# ll /etc/systemd/system/multi-user.target.wants/
total 0
lrwxrwxrwx. 1 root root 38 Mar 15 11:41 auditd.service -> /usr/lib/systemd/system/auditd.service
lrwxrwxrwx. 1 root root 37 Mar 15 11:41 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx. 1 root root 41 Mar 15 11:41 firewalld.service -> /usr/lib/systemd/system/firewalld.service
lrwxrwxrwx. 1 root root 42 Mar 15 11:41 irqbalance.service -> /usr/lib/systemd/system/irqbalance.service
lrwxrwxrwx. 1 root root 46 Mar 15 11:41 NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx. 1 root root 39 Mar 15 11:41 postfix.service -> /usr/lib/systemd/system/postfix.service
lrwxrwxrwx. 1 root root 40 Mar 15 11:40 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target
lrwxrwxrwx. 1 root root 46 Mar 15 11:41 rhel-configure.service -> /usr/lib/systemd/system/rhel-configure.service
lrwxrwxrwx. 1 root root 39 Mar 15 11:41 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service
lrwxrwxrwx. 1 root root 36 Mar 15 11:41 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx. 1 root root 39 May 6 14:49 sysstat.service -> /usr/lib/systemd/system/sysstat.service
lrwxrwxrwx. 1 root root 37 Mar 15 11:41 tuned.service -> /usr/lib/systemd/system/tuned.service
lrwxrwxrwx. 1 root root 40 Mar 15 11:41 vmtoolsd.service -> /usr/lib/systemd/system/vmtoolsd.service

开机启动流程_加载

4.建立终端

rc执行完毕后,返回init。这时基本系统环境已经设置好了,各种守护进程也已经启动了。
init接下来会打开6个终端,以便用户登录系统。在inittab中的以下6行就是定义了6个终端

5.用户登录系统

一般来说,用户的登录方式有三种:
(1)命令行登录
(2)ssh登录
(3)图形界面登录
开机启动流程_运行级别_02

二.CentOS7开机流程

1.BIOS(开机自检)

  • BIOS加电自检,检测主板上的硬件设备,根据BIOS设置启动系统
  • 读取/boot分区下,所有内核文件

2.MBR ( Master Boot Record 主引导记录

主引导记录放在Linux引导硬盘的第一个扇区中,此信息通过BIOS预加载到ROM(只读内存)中。
MBR的大小只有512个字节,它包含用于引导操作系统的机器代码指令,称为引导加载程序,以及分区表。一旦BIOS找到引导加载程序(GRUB2)并将其加载到(ROM)内存或硬盘驱动器中,它就会控制引导过程。只是MBR(主引导记录)加载并执行GRUB2引导加载程序。

3.GRUB2 Bootloader(引导菜单)

加载grub2菜单,选择要进入的系统
言简意赅:grub2菜单,如果装了双系统会让你进行选择,Linux?还是windows?还是MacOS?
GRUB2是所有最新版本(如Red Hat/CentOS 7)中的默认引导加载程序,也是9.10版中的Ubuntu。它已经被GRUBbootloader(也称为GRUBLegacy)所取代。
grub2配置文件位于/boot/grub2/grub.cfg中,由grub2 mkconfig使用/etc/grub.d中的模板和/etc/default/grub中的设置自动生成。不建议编辑GRUB2配置文件。
引导加载程序(rhel 7的grub2)启动rhel7内核和初始RAM磁盘(initrd)。GRUB 2安装在服务器硬盘的引导扇区中,配置为加载Linux内核和initramfs,initrd是一个初始根文件系统,将在Linux系统上的实际根文件系统之前安装。

4.Kernel(内核引导)

读取/boot目录下所有内核文件
Linux内核是操作系统的核心,它是系统上第一个加载的程序。当系统启动时,内核从initrd.img加载所有必要的内核模块和驱动器,以加载Linux7中的系统第一个进程systemd

5.Systemd (不再使用init,改成了systemd)

runlevel0.target -> poweroff.target
runlevel1.target -> rescue.target
runlevel2.target -> multi-user.target
runlevel3.target -> multi-user.target
runlevel4.target -> multi-user.target
runlevel5.target -> graphical.target
runlevel6.target -> reboot.target


运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆
运行级别2:多用户运行级别(没有文件系统)
运行级别3:完全的多用户状态(有文件系统),登陆后进入控制台命令行模式
运行级别4:多用户运行级别
运行级别5:X11控制台,登陆后进入图形GUI模式
运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动

6.Runlevel-Target (运行级别)

开机启动流程_加载_03

三.CentOS7进入单用户模式-忘记root密码

1.关机

halt
poweroff
shutdown -h now
init 0

2.开机

开机启动流程_运行级别_04

3.进入gurb2菜单

##两种方法
selinux
vim /etc/sysconfig/selinux
selinux=enforcing  ==> selinux=disabled

在linux16行末,写入:
enforcing=0 init=/bin/bash

按Ctrl + x

开机启动流程_加载_05

4.修改密码

开机启动流程_运行级别_06

## 重新挂载/
mount -o rw,remount /

## 修改密码(两种方法)
passwd root
echo 123 |passwd --stdin root

## 重新加载init
exec /sbin/init

开机启动流程_加载_07

不需要重新挂载 / 的方法

开机启动流程_运行级别_08

# 将Linux16中的ro 改成rw

四.CentOS7进入单用户模式-修改默认运行级别

1.关机2.进入grub2菜单3.按e编辑grub2菜单的内核参数

开机启动流程_加载_09

Linux16行中 ro改成rw
行末写:rd.break
修改后,按Ctrl + x

开机启动流程_加载_10

开机启动流程_运行级别_11

## 1.重新挂载root权限目录
mount -o rw,remount /sysroot

## 2.获取root权限
chroot /sysroot

## 3.修改默认运行级别
systemctl set-default multi-user.target

## 4.退出
exit

## 5.重启
reboot

五.救援模式

需要有系统盘,才可以进入救援模式
要进入安装系统的界面,才能进入救援模式

## 1.关机
init 0
halt
poweroff
shutdown -h now

## 2.将系统盘放入光驱中

开机启动流程_centos_12

## 3.进入BIOS
将系统启动的顺序,改为从光盘启动
方法一:
开机时,不断按f2
方法二:
在电源里面点击,进入固件

举例方法二

开机启动流程_centos_13

进入BIOS界面

开机启动流程_运行级别_14

选择boot

开机启动流程_centos_15

把CD-ROM启动顺序排到第一个

开机启动流程_centos_16

按f10保存并退出 ->选择yes

开机启动流程_centos_17

进入安装系统的界面

开机启动流程_运行级别_18

开机启动流程_运行级别_19

进入救援模式

开机启动流程_centos_20

开机启动流程_加载_21

开机启动流程_centos_22

开机启动流程_加载_23

# 1.先输入回车
# 2.获取root的环境(root权限)
chroot /mnt/sysimage

案例1-损坏系统扇区(前512字节)

dd </dev/zero >/dev/sda bs=1 count=452

开机启动流程_加载_24

扇区损坏,需要进入救援模式修复扇区

开机启动流程_加载_25

# 1.修复扇区
grub2-install /dev/sda
# 2.退出root环境
exit
# 3.重启
reboot

修复grub菜单

开机启动流程_运行级别_26

# 1.修复扇区
grub2-install /dev/sda
# 1.5 重置grub.cfg配置文件
grub2-mkconfig -o /boot/grub2/grub.cfg
# 2.退出root环境
exit
# 3.重启
reboot

六.systemd的优势

开机启动流程_加载_27

systemd的优势

1.最新系统都采用systemd管理(RedHat7,CentOS7,Ubuntu15...)
2.CentOS7 支持开机并行启动服务,显著提高开机启动效率
3.CentOS7关机只关闭正在运行的服务,而CentOS6,全部都关闭一次。
4.CentOS7服务的启动与停止不在使用脚本进行管理,也就是/etc/init.d下不在有脚本。
5.CentOS7使用systemd解决原有模式缺陷,比如原有service不会关闭程序产生的子进程。

systemd相关配置文件

## 系统中所有服务启动的脚本存放路径
CentOS7:
/usr/lib/systemd/system/
nginx.service

CentOS6:
/etc/init.d/

## 系统运行级别相关目录
#CentOS7:
[root@localhost <sub>]# ll /etc/systemd/system
total 4
drwxr-xr-x. 2 root root 57 Mar 15 18:34 basic.target.wants
lrwxrwxrwx. 1 root root 41 Mar 15 18:34 dbus-org.fedoraproject.FirewallD1.service -> /usr/lib/systemd/system/firewalld.service
lrwxrwxrwx. 1 root root 46 Mar 15 18:34 dbus-org.freedesktop.NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx. 1 root root 57 Mar 15 18:34 dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
lrwxrwxrwx. 1 root root 37 Mar 15 18:38 default.target -> /lib/systemd/system/multi-user.target
drwxr-xr-x. 2 root root 87 Mar 15 18:34 default.target.wants
drwxr-xr-x. 2 root root 32 Mar 15 18:34 getty.target.wants
drwxr-xr-x. 2 root root 35 Mar 15 18:34 local-fs.target.wants
drwxr-xr-x. 2 root root 4096 Mar 15 18:38 multi-user.target.wants
drwxr-xr-x. 2 root root 48 Mar 15 18:34 network-online.target.wants
drwxr-xr-x. 2 root root 134 Mar 15 18:34 sysinit.target.wants
drwxr-xr-x. 2 root root 44 Mar 15 18:34 system-update.target.wants
drwxr-xr-x. 2 root root 29 Mar 15 18:34 vmtoolsd.service.requires

#CentOS6:
[root@localhost </sub>]# ll /etc/rc*.d -d
lrwxrwxrwx.  1 root root   10 May  7 10:46 /etc/rc0.d -> rc.d/rc0.d
lrwxrwxrwx.  1 root root   10 May  7 10:46 /etc/rc1.d -> rc.d/rc1.d
lrwxrwxrwx.  1 root root   10 May  7 10:46 /etc/rc2.d -> rc.d/rc2.d
lrwxrwxrwx.  1 root root   10 May  7 10:46 /etc/rc3.d -> rc.d/rc3.d
lrwxrwxrwx.  1 root root   10 May  7 10:46 /etc/rc4.d -> rc.d/rc4.d
lrwxrwxrwx.  1 root root   10 May  7 10:46 /etc/rc5.d -> rc.d/rc5.d
lrwxrwxrwx.  1 root root   10 May  7 10:46 /etc/rc6.d -> rc.d/rc6.d

## 默认运行级别需要开机自启的服务存放目录
#CentOS7:
[root@localhost ~]# ll /etc/systemd/system/multi-user.target.wants/
total 0
lrwxrwxrwx. 1 root root 38 Mar 15 18:34 auditd.service -> /usr/lib/systemd/system/auditd.service
lrwxrwxrwx. 1 root root 37 Mar 15 18:34 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx. 1 root root 41 Mar 15 18:34 firewalld.service -> /usr/lib/systemd/system/firewalld.service
lrwxrwxrwx. 1 root root 42 Mar 15 18:34 irqbalance.service -> /usr/lib/systemd/system/irqbalance.service
lrwxrwxrwx. 1 root root 46 Mar 15 18:34 NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx. 1 root root 39 Mar 15 18:34 postfix.service -> /usr/lib/systemd/system/postfix.service
lrwxrwxrwx. 1 root root 40 Mar 15 18:34 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target
lrwxrwxrwx. 1 root root 46 Mar 15 18:34 rhel-configure.service -> /usr/lib/systemd/system/rhel-configure.service
lrwxrwxrwx. 1 root root 39 Mar 15 18:34 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service
lrwxrwxrwx. 1 root root 36 Mar 15 18:34 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx. 1 root root 37 Mar 15 18:34 tuned.service -> /usr/lib/systemd/system/tuned.service
lrwxrwxrwx. 1 root root 40 Mar 15 18:34 vmtoolsd.service -> /usr/lib/systemd/system/vmtoolsd.service

#CentOS6:
/etc/rc3.d/
K01smartd -> ../init.d/smartd
K10psacct -> ../init.d/psacct
K10saslauthd -> ../init.d/saslauthd
K15svnserve -> ../init.d/svnserve
K50netconsole -> ../init.d/netconsole
K74ntpd -> ../init.d/ntpd
K75ntpdate -> ../init.d/ntpdate
K75quota_nld -> ../init.d/quota_nld
K87restorecond -> ../init.d/restorecond
K89rdisc -> ../init.d/rdisc
K99rngd -> ../init.d/rngd
S01sysstat -> ../init.d/sysstat
S02lvm2-monitor -> ../init.d/lvm2-monitor
S08ip6tables -> ../init.d/ip6tables
S08iptables -> ../init.d/iptables
S10network -> ../init.d/network
S11auditd -> ../init.d/auditd
S12rsyslog -> ../init.d/rsyslog
S13cpuspeed -> ../init.d/cpuspeed
S13irqbalance -> ../init.d/irqbalance
S15mdmonitor -> ../init.d/mdmonitor
S20kdump -> ../init.d/kdump
S22messagebus -> ../init.d/messagebus
S25blk-availability -> ../init.d/blk-availability
S25netfs -> ../init.d/netfs
S26acpid -> ../init.d/acpid
S26haldaemon -> ../init.d/haldaemon
S26udev-post -> ../init.d/udev-post S55sshd -> ../init.d/sshd S80postfix -> ../init.d/postfix S82abrt-ccpp -> ../init.d/abrt-ccpp S82abrtd -> ../init.d/abrtd S90crond -> ../init.d/crond S95atd -> ../init.d/atd S99local -> ../rc.local

七.systemd管理服务的命令

## 1.启动服务 
#CentOS6:
/etc/init.d/服务名 start
service 服务名 start

#CentOS7:
systemctl start 服务名
service 服务名 start

## 2.停止服务
#CentOS6:
/etc/init.d/服务名 stop
service 服务名 stop

#CentOS7:

systemctl stop 服务名
service 服务名 stop

## 3.服务重启
#CentOS6:

/etc/init.d/服务名 restart
service 服务名 restart

#CentOS7:
systemctl restart 服务名
service 服务名 restart

## 4.服务重新加载
#CentOS6:
/etc/init.d/服务名 reload
service 服务名 reload

#CentOS7:
systemctl reload 服务名
service 服务名 reload

## 5.检查服务的启动状态
#CentOS6:
[root@localhost <sub>]# /etc/init.d/sshd status
openssh-daemon (pid 1508) is running... S

#CentOS7:
[root@localhost </sub>]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-04-25 20:43:16 CST; 1 weeks 6 days ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 6859 (sshd)
CGroup: /system.slice/sshd.service
└─6859 /usr/sbin/sshd -D

Apr 25 20:43:16 localhost.localdomain systemd[1]: Starting OpenSSH server daemon...
Apr 25 20:43:16 localhost.localdomain sshd[6859]: Server listening on 0.0.0.0 port 22.
Apr 25 20:43:16 localhost.localdomain sshd[6859]: Server listening on :: port 22.
Apr 25 20:43:16 localhost.localdomain systemd[1]: Started OpenSSH server daemon.
May 09 16:35:01 localhost.localdomain sshd[7600]: Accepted password for root from 10.0.0.1 port 58260 ssh2

## 6.判断服务是否在运行
[root@localhost <sub>]# systemctl is-active sshd
active
[root@localhost </sub>]# systemctl stop sshd
[root@localhost <sub>]# systemctl is-active sshd
inactive

## 7.禁用某个服务
[root@localhost </sub>]# systemctl mask crond

## 8.取消禁用某个服务
[root@localhost ~]# systemctl unmask crond

八.开机自启服务管理

## 1.查看开机自启的服务有哪些 
#CentOS6:
[root@localhost <sub>]# chkconfig

#CentOS7:
[root@localhost </sub>]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
brandbot.path disabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-1.scope static
session-3.scope static
auditd.service enabled
autovt@.service enabled
blk-availability.service disabled
brandbot.service static
console-getty.service disabled
console-shell.service disabled
container-getty@.service static
cpupower.service disabled
crond.service enabled
dbus-org.fedoraproject.FirewallD1.service enabled
dbus-org.freedesktop.hostname1.service static

## 2.设置开机自启动
#CentOS6:
[root@localhost <sub>]# chkconfig sshd on (默认 2 3 4 5 运行级别都自启)
[root@localhost </sub>]# chkconfig |grep sshd
sshd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@localhost <sub>]# chkconfig sshd on
[root@localhost </sub>]# chkconfig |grep sshd
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost <sub>]# chkconfig sshd off
[root@localhost </sub>]# chkconfig |grep sshd
sshd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@localhost <sub>]# chkconfig sshd --level 3 on
[root@localhost </sub>]# chkconfig |grep sshd
sshd 0:off 1:off 2:off 3:on 4:off 5:off 6:off

#CentOS7:
[root@localhost <sub>]# systemctl enable sshd

## 3.关闭开机自启
#CentOS6:
[root@localhost </sub>]# chkconfig sshd off

#CentOS7:
[root@localhost <sub>]# systemctl disable sshd

## 4.查看指定服务是否开机自启
#CentOS6:
[root@localhost </sub>]# chkconfig --list sshd
sshd 0:off 1:off 2:off 3:on 4:off 5:off 6:off

#CentOS7:
[root@localhost ~]# systemctl is-enabled nginx
enabled:允许开机自启
disabled:不允许开机自启

## 7.如果启动脚本被修改,重新加载启动脚本
#CentOS7:
systemctl daemon-reload


举报

相关推荐

0 条评论