Linux boot process and service control - Hands! (Seven)

Content structure

  • Boot process

  • Start fault exclusion

  • Service Control and switching the operating level

  • Optimization of the startup process



(A) Boot Process

1, the boot process overview

Screenshot micro-channel _20190823153629.png

  • POST after boot server host, the initial detection of the CPU (Central Processing Unit, central processing unit), memory, graphics card, such as a keyboard apparatus according to the motherboard BIOS settings after successful detection of the handover control system according to a preset startup sequence right, most of the time will be handed over to the local hard disk. 

  • MBR boot when the boot from the local hard drive system, first, according to the first sector of the hard disk MBR (Master Boot Record, the master boot record) is provided, the system passes control to the operating system partition contains boot files; or directly the guidance information boot menu call (e.g. GRUB) MBR record. 

  • GRUB menu For Linux operating systems, GRUB (GRand Unified Bootloader, unified boot loader) is the most widely used multi-system boot program. After the transfer system control to GRUB, the boot menu will be displayed to the user selected, and depending on the option (or default value) to load the Linux kernel files, then the system control to the kernel. Note that, CentOS 7 uses GRUB2 boot loader. 

  • Load Linux kernel Linux kernel is a special pre-compiled binary files between various hardware resources and system programs, responsible for resource allocation and scheduling. After the kernel took control of the system, we will have complete control over the entire process of running the Linux operating system. In CentOS system, the default kernel file is located in "/boot/vmlinuz-3.10.0-514.el7.x86_64". 

  • init 进程初始化 为了完成进一步的系统引导过程,Linux 内核首先将系统中的“/sbin/init”程序加载到内存中运行(运行中的程序称为进程),init 进程负责完成一系列的系统初始化过程, 最后等待用户进行登录。

2,系统初始化进程及文件

init进程

  • 由Linux内核加载运行/sbin/init程序

  • 是系统中第一个进程

  • PID(进程标记)号永远为1

Systemd单元类型

Screenshot micro-channel _20190823153629.png

运行级别所对应的systemd目标

Screenshot micro-channel _20190823153629.png

(二)排除启动类故障(实验操作!)

1,修复MBR扇区故障

故障原因

  • 病毒等造成的破坏

  • 不正确的分区操作,磁盘读写误操作

故障现象

  • 找不到引导程序,启动中断

  • 无法加载操作系统,开机后黑屏

解决思路

  • 应提前作好备份文件

  • 以安装光盘引导进入急救模式

  • 从备份文件中恢复

1)备份mbr引导扇区到其他磁盘,先创建一个磁盘分区/dev/sdb1-格式化-挂载到/data(详细看前文章)

[root@localhost ~]# dd if=/dev/sda of=/data/sda.mbr.bak bs=512 count=1  备份
记录了1+0 的读入
记录了1+0 的写出
512字节(512 B)已复制,0.000254623 秒,2.0 MB/秒
[root@localhost ~]# ls /data    查看备份是否成功
sda.mbr.bak

Screenshot micro-channel _20190823162033.png

2)模拟破坏sda中第一个扇区mbr引导分区,重启

[root@localhost ~]# dd if=/dev/zero of=/dev/sda bs=512 count=1  模拟破坏
记录了1+0 的读入
记录了1+0 的写出
512字节(512 B)已复制,0.00028556 秒,1.8 MB/秒
[root@localhost ~]# init 6  重启

Screenshot micro-channel _20190823162230.png

Screenshot micro-channel _20190823163536.png

3)连接centos7镜像光盘,重启系统后迅速按ESC进入启动菜单,选择cdrom-Troubleshooting-rescue centos system

Screenshot micro-channel _20190823163536.png

Screenshot micro-channel _20190823162033.png

Screenshot micro-channel _20190823162230.png

4)按1进入shell,创建挂载点/bak-将/dev/sdb1挂载到/bak-将备份的mbr复制到/dev/sda中,重启完成修复

微信截图_20190823163536.png

微信截图_20190823171347.png

微信截图_20190823173043.png

微信截图_20190823173242.png

2,恢复GRUB引导故障

故障原因

  • MBR中的GRUB引导程序遭到破坏

  • grub.conf文件丢失,引导配置有误

故障现象

  • 系统引导停滞,显示“grub>”提示符

解决思路

  • 尝试手动输入引导命令

  • 进入急救模式,重写或者备份中恢复grub.conf

  • 向MBR扇区中重建grub程序

1)模拟破坏GRUB引导配置文件,删除grub配置文件

微信截图_20190823162033.png

微信截图_20190823162230.png

微信截图_20190823163536.png

2)连接centos7镜像光盘,重启系统后迅速按ESC进入启动菜单,选择cdrom-Troubleshooting-rescue centos system-按1进入shell

微信截图_20190823163536.png

微信截图_20190823162033.png

微信截图_20190823162230.png

微信截图_20190823163536.png

微信截图_20190823171347.png

3)切换bash环境-建立加载sda分区-构建grub菜单配置文件-退出bash环境-重启

微信截图_20190823171347.png微信截图_20190823173242.png

3,遗忘root用户密码

故障原因

  • 遗忘root用户的密码

故障现象

  • 无法进行需要root权限管理操作

  • 若没有其他可用账号,将无法登录系统

解决思路

  • 进入急救模式,重设密码

1)重启你的Linux系统

微信截图_20190823193646.png

2,进入急救模式,切换bash环境-修改密码-退出bash环境-重启即可

微信截图_20190823194204.png

(三)服务控制及切换运行级别

系统服务类型

常用方式

  • systemctl 控制类型 服务名称

控制类型

  • start:启动

  • stop:停止

  • restart:重新启动(停止加启动,现实慎用)

  • reload:重新加载(刷新配置文件)

  • status:查看服务状态

微信截图_20190823233018.png

Linux系统的运行级别

查看运行级别

  • runlevel命令

  • systemctl工具

微信截图_20190823222017.png

临时切换运行级别

  • init命令

  • systemctl工具


命令含义 命令行界面 图形界面 关机 重启
init命令 init3 init5 init0 init6
systemctl命令 systemctl isolate multi-usr.target systemctl isolate graphical.target systemctl isolate poweroff.target systemctl isolate reboot.target
[Root @ localhost ~] # init 3 is switched to the command line interface
[root@localhost ~]# runlevel 
5 3
or
[Root @ localhost ~] # systemctl isolate multi-user.target switch to the command line interface

Change the default operating mode level

[Root @ localhost ~] # ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target modified by a soft connection
[root@localhost ~]# systemctl get-default
graphical.target

微信截图_20190823224605.png

(Iv) optimize the boot process

System Service Management Tools

netstat -ntap view an ongoing service and port

  • n: digital display

  • t: tcp agreement

  • a: All

  • p: Agreement

微信截图_20190823230106.png

ntsysv tool

  • Providing an interactive visualization window

  • You can run on an ASCII terminal.

  • Facilitating the centralized management of multiple services

[Root @ localhost ~] # ntsysv exchange Visualization Service Management

微信截图_20190823230435.png

systemctl Tools

  • It does not provide an interactive visualization window

  • Single service management more efficient

View the status of system services start

systemctl is-enabled service name

微信截图_20190823231234.png

Start setting the system state services

systemctl enable / disable service name

微信截图_20190823232200.png



thanks for reading! ! !

Guess you like

Origin blog.51cto.com/14080162/2432161