Getting Started with Linux system boot process ---

Getting Started with Linux system boot process ---

One,centos6

Linux started when, in fact, this open process, and not everyone thought so complicated, the process probably divided into five stages

1. kernel boot

2. Run the init

3. The system initialization

4. The establishment of the terminal

The user login system

init程序的类型:

SysV:       init, CentOS 5之前, 配置文件: /etc/inittab。
Upstart:    init,CentOS 6, 配置文件: /etc/inittab, /etc/init/*.conf。
Systemd:    systemd, CentOS 7,配置文件: /usr/lib/systemd/system、 /etc/systemd/system。

1. kernel boot

When the computer is powered on, first boot BIOS POST, the BIOS boot device in accordance with the setting, is generally initiated by a hard disk.
Can also be U disk, CD-ROM, etc. ... in the absence of the system, you need to install the system, you can be adjusted.

BIOS self-test, the general need to check the hardware aspects, such as: memory, CPU, disk and other equipment ...

Previously trained, 'listen to the sound identified bit' ... it is to hear a voice, beep, judge them, what hardware is bad, each hardware damage, different startup sound.

After the operating system to take over the hardware, the kernel first reads the files in the / boot directory.

2. Run the init

init in CentOS, the process is the starting point for all systems, if you want to start the process, it must have init, init if not, any process in the system will not start, it is equivalent to the system can not open ... so init Dad is.

Then the init program first is the need to read the configuration file /etc/inittab

In the init, it involves seven operational level (runlevel)

Many programs require start-up. They are called "service" (service) in Windows, in Linux is called "daemon" (daemon).

A major task of the init process, is to run these programs in the boot.

However, different situations need to launch different programs, such as when used as a server, they need to start nginx, use as a desktop is not required.

Linux allows for different occasions, assign different boot program, which is called "operational level" (runlevel). That is, the boot according to "run level" to determine which programs you want to run.

Runlevel (runlevel) classification:

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

Check the level of the currently running

[root@zls ~]# runlevel
N 3

Temporary switching the operating level

[root@zls ~]# init 5
[root@zls ~]# runlevel
3 5

Permanent switching the operating level

[root@zls ~]# vim /etc/inittab
id:3:initdefault

Check boot from Kai Services

[root@zls ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole      0:关 1:关 2:关 3:关 4:关 5:关 6:关
network         0:关 1:关 2:开 3:开 4:开 5:开 6:关
sysstat         0:关 1:关 2:开 3:开 4:关 5:开 6:关

Practice Case 1: centos6 system into single-user level

1.重启服务器,一直按“E”键,进入grub引导菜单的内核界面

2.在内核界面选择使用上下箭头移动并选择内核

3.内核选定之后再次按下“E”键进行编辑

4.在最后一行后面加上数字1(是单用户模式的运行级别)或者single然后回车

5.然后根据提示按下boot进行重新引导

** ** 3. The system initialization

Such a line in the init configuration file: si :: sysinit: /etc/rc.d/rc.sysinit it calls performed /etc/rc.d/rc.sysinit, and rc.sysinit is a bash shell script it is the completion of a number of system initialization work, rc.sysinit is important to run a script every level should be run first.

It is mainly the work: activate the swap partition, check the disk, load the hardware module and a number of other priority tasks.

l5:5:wait:/etc/rc.d/rc 5

This line expressed as a parameter to run /etc/rc.d/rc,/etc/rc.d/rc 5 is a Shell script that takes 5 as a parameter to perform /etc/rc.d/rc5.d/ directory All rc script at startup, / etc / rc.d / rc5.d / directory of the startup scripts are actually some of the connection file, rather than true rc startup script, real rc startup scripts are in fact /etc/rc.d/init.d/ placed in the directory.

These rc startup scripts have similar usage, they are generally able to accept start, stop, restart, status and other parameters.

The /etc/rc.d/rc5.d/ rc startup script is usually the beginning of the S K or connection file for the script to start, will start at the beginning of S parameters to run.

If the script is found there is a corresponding connection starts with K, and has been in the operating mode (file / var / lock / subsys / under a flag), then the first stop to stop these parameters has started guardian process, and then re-run.

This is done to ensure that when init to change run levels, all the daemons will be restarted.

As for what daemon running in a run each level, users can chkconfig or setup in the "System Services" from the line set.

4. The establishment of the terminal

Rc has finished, return to init. Then the basic system environment has been set up, all kinds of daemons have been started.

init will open next six terminals, so that the user login system. The following 6 line is defined in inittab six terminals:

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

As can be seen from the above run level are 2,3,4,5 mingetty program will run respawn, mingetty program can open the terminal, setting mode.

At the same time it will display a text login screen, the interface is what we often see the login screen, in the login screen will prompt the user to enter a user name, and the user will be entered by the user as a parameter to the login program to verify the user's identity.

The user login system

In general, users log in three ways:

(1) command line login
(2) ssh login
(3) Graphical login

For graphical user run level 5, their log in through a graphical login screen. After a successful login can go directly to KDE, Gnome and other window manager.

The paper stresses the situation or text login: When we see mingetty login screen, we can enter a user name and password to log in the system.

Linux account verification process is login, login will receive a user name mingetty came as the username parameter.

Then login user name will be analyzed: If the username is not root, and there is / etc / nologin file, login nologin will output the contents of the file, and then exit.

This prevents non-root user login time is usually used for system maintenance. The registered / securetty only / etc terminal only allow the root user login, if the file does not exist, the root user can log on any terminal.

/ Etc / usertty file is used to make additional access restrictions on users, if the file does not exist, there are no other restrictions.

Graphics and text mode mode switching manner

Linux预设提供了六个命令窗口终端机让我们来登录。

默认我们登录的就是第一个窗口,也就是tty1,这个六个窗口分别为tty1,tty2 … tty6,你可以按下Ctrl + Alt + F1 ~ F6 来切换它们。

如果你安装了图形界面,默认情况下是进入图形界面的,此时你就可以按Ctrl + Alt + F1 ~ F6来进入其中一个命令窗口界面。

当你进入命令窗口界面后再返回图形界面只要按下Ctrl + Alt + F7 就回来了。

如果你用的vmware 虚拟机,命令窗口切换的快捷键为 Alt + Space + F1~F6. 如果你在图形界面下请按Alt + Shift + Ctrl + F1~F6 切换至命令窗口。

Linux 关机

在linux领域内大多用在服务器上,很少遇到关机的操作。毕竟服务器上跑一个服务是永无止境的,除非特殊情况下,不得已才会关机。

正确的关机流程为:sync > shutdown > reboot > halt

关机指令为:shutdown ,你可以man shutdown 来看一下帮助文档。

关机重启指令:

sync 将数据由内存同步到硬盘中。

shutdown 关机指令,你可以man shutdown 来看一下帮助文档。例如你可以运行如下命令关机:

shutdown –h 10 ‘This server will shutdown after 10 mins’ 这个命令告诉大家,计算机将在10分钟后关机,并且会显示在登陆用户的当前屏幕中。

shutdown –h now 立马关机

shutdown –h 20:25 系统会在今天20:25关机

shutdown –h +10 十分钟后关机

shutdown –r now 系统立马重启

shutdown –r +10 系统十分钟后重启

reboot 就是重启,等同于 shutdown –r now

halt 关闭系统,等同于shutdown –h now 和 poweroff

总结:

不管是重启系统还是关闭系统,首先要运行 sync 命令,把内存中的数据写到磁盘中。

关机的命令有 shutdown –h now halt poweroff 和 init 0 , 重启系统的命令有 shutdown –r now reboot init 6。

centos6 启动过程

centos6 和centos7开机对比

img

### centos6 开启过程


1.BIOS(开机自检)
2.MBR ( Master Boot Record 主引导记录)
3.GRUB2 Bootloader(引导菜单)
4.Kernel(内核引导)
5.Systemd (不再使用init,改成了systemd)
6.Runlevel-Target (运行级别)

1.BIOS

基本输入/输出系统是一个固件接口,它不仅控制启动过程,还提供对连接的外围设备的低级接口的所有控制。打开系统电源后,它将读取所有设备设置并执行开机自检(开机自检)过程,以识别硬件设备,以测试和初始化系统硬件组件。成功后处理后,它将加载MBR(主引导记录)以进行下一个引导处理。

2.MBR引导

主引导记录放在Linux引导硬盘的第一个扇区中,此信息通过BIOS预加载到ROM(只读内存)中。

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

3.GRUB2(引导加载程序版本2)引导加载程序

言简意赅: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内核引导

Linux内核是操作系统的核心,它是系统上第一个加载的程序。当系统启动时,内核从initrd.img加载所有必要的内核模块和驱动器,以加载Linux7中的系统第一个进程systemd,在6里面,init是爸爸,在7里面换了个爸爸,那就是systemd

下面的命令将帮助您找到SystemD进程ID(PID)

[root@zls ~]# ps -ef | grep 折叠ystemd
root          1      0  0 7月14 ?       00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root        347      1  0 7月14 ?       00:00:00 /usr/lib/systemd/systemd-journald
root        379      1  0 7月14 ?       00:00:00 /usr/lib/systemd/systemd-udevd
root        522      1  0 7月14 ?       00:00:00 /usr/lib/systemd/systemd-logind
dbus        525      1  0 7月14 ?       00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation

二、systemd管理进程

systemd使用“targets”而不是runlevels。默认情况下,有两个主要级别:

1.查看级别

multi-user(多用户).target: analogous to runlevel 3

#查看运行级别
[root@zls ~]# systemctl get-default
multi-user.target

2.设置默认级别

graphical(图形化).target: analogous to runlevel 5

#设置默认运行级别
[root@zls ~]# systemctl set-default graphical.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.

#查看默认界面
[root@zls ~]# systemctl get-default
graphical.target
[root@zls ~]# vim /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
#

4.CentOS7中的运行级别:

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,否则不能正常启动

三、centos7实践案例:

1.案例1:centos7系统,单用户修改root密码

方法1:

1.重启Linux系统主机并出现引导界面时,按下键盘上的e键,进入内核编辑界面

2.在Linux16这行后面添加enforcing=0 init=/bin/bash,然后按下Ctrl+X组合键来运行修改过的内核程序

3.然后进入单用户模式界面。

#重新挂载/目录
bash-4.2# mount -o rw,remount /   
#非交互设置root密码
bash-4.2# echo 1 |passwd --stdin root
#重启系统
bash-4.2# exec /sbin/init

方法2: 不需要重新挂载的方式 (直接修改ro 为rw)

案例2: 如果修改级别

添加 rd.break 进入。

如果centos7系统运行级别被设置成重启的级别...


案例3:救援模式

1.挂载光盘镜像

2.从BIOS启动

3.进入BIOS设置从光盘启动,选择CD-ROM Drive ,按shift+ ,把CD-ROM Drive置顶到第一行

4.按F10保存并退出

5.进入救援模式

6.选择救援模式

7.按 1 选择继续,进入救援模式

救援模式生产场景1:

当系统坏了,无法登陆系统,但是需要把里面的数据拷贝出来,怎么办?

bash-4.2# mkfs.xfs -f /dev/sdc
bash-4.2# mkdir /data
bash-4.2# mount /dev/sdc /data
bash-4.2# cp -a /raid0 /data
bash-4.2# umount /dev/sdb

救援模式生产场景2:

修复MBR,主要出现在安装双系统的时候,后安装的系统把原来的MBR删除了,需要修复

1.比如破坏硬盘的前446字节,模拟MBR引导损坏,会发现重启无法启动系统

[root@zls ~]# dd < /dev/zero >/dev/sda bs=1 count=446
记录了446+0 的读入
记录了446+0 的写出
446字节(446 B)已复制,0.0011061 秒,403 kB/秒

2.重启,查看系统状态

3.重启进入救援模式

4.进入系统,获取root权限

5.修复MBR引导,然后重启连接服务器

#修复grub
bash-4.2# grub2-install /dev/sda

#退出
bash-4.2# exit

#重启进入系统
bash-4.2# reboot

救援模式生产场景3:

CentOS7 误删除grub文件,进行修复

1.虚拟机先删除grub文件,模拟故障

#删除grub2
[root@zls ~]# rm -fr /boot/grub2/
#重启
[root@zls ~]# reboot

2.查看系统状态

3.打开进入救援模式

4.重新安MBR,加载grub2中的grub.cfg文件

bash-4.2# chroot /mnt/sysimage
bash-4.2# grub2-install /dev/sda
bash-4.2# grub2-mkconfig -o /boot/grub2/grub.cfg

5.查看加载出来的文件

bash-4.2# ls /boot/grub2

6.退出

bash-4.2# exit

7.再打开vmware,进入BIOS设置从光盘启动,改回默认Hard Drive ,按shift+ ,把它置顶到第一行

继续登陆...

满血复活...

四、修改CentOS7网卡名称为eth0和eth1

1.环境准备

1.重装虚拟机

2.添加网卡,添加lan区段

3.设置时间,关闭KDUMP,并进行磁盘分区

4磁盘分区,选择标准分区,略

显示2块网卡

把所有的安装完成后。

修改网卡名称,如果已经安装过的系统,那我们还是从BIOS界面到光盘启动,进入系统安装界面。

选择 Install CentOS 7然后按下Tab键,

末行输入  net.ifnames=0  biosdevname=0   (默认规则)

如果不是dell物理机环境:

net.ifnames=0

进入系统后:开始配置网卡

2.用命令配置网卡

方法1:

开机,用命令配置网卡

配置第一块外网网卡eth0

配置内网网卡

重启网卡,显示IP

ifdown eth0可关闭网卡,ifup eth0可开启网卡,通过同一个LAN区段可相互通信)

方法2:

命令行设置网卡名称规则
[root@ server ~]# cd /etc/sysconfig/network-scripts/ #修改网卡配置文件

[root@ server network-scripts]# mv ifcfg-eno16777728 ifcfg-eth0

[root@ server network-scripts]# sed -i "s#ens33#eth0#g" ifcfg-eth0

[root@server~]# vim /etc/sysconfig/grub #GRUB添加kernel参数

GRUB_CMDLINE_LINUX="...net.ifnames=0 biosdevname=0 quiet"

[root@server~]# grub2-mkconfig -o /boot/grub2/grub.cfg

[root@server~]# reboot #重启系统生效

3.系统基本环境优化

1.下载安装包

下载镜像源、软件包

\#安装Base源

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 

\#安装软件包

yum install -y wget tree gcc gcc-c++ glibc vim net-tools telnet

\#删除其他用不上的源

find /etc/yum.repos.d/ -type f ! -name 'CentOS-Base.repo' |xargs rm -f 

\#下载repl源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/reo/epel-7.repo

2.编辑脚本,作为模板机

vim host_ip.sh

\#!/bin/bash

. /etc/init.d/functions

read -p '请输入要修改的IP主机位: ' IP
read -p '请输入要修改的主机名: ' H

sed -i "s#120#$IP#g" /etc/sysconfig/network-scripts/ifcfg-eth{0,1}

systemctl restart network
if [ $? -eq 0 ];then
​   action "network" /bin/true
else
​   action "network" /bin/false
fi

hostnamectl set-hostname $H
new_name=`cat /etc/hostname`
if [ $new_name == $H ];then
​   action "hostname" /bin/true
else
​   action "hostname" /bin/false
fi

 
修改host_ip.sh权限chmod +x ./host_ip.sh
关闭防火墙
关闭selinux即可
快照,克隆即可开启新的虚拟机,执行脚本
克隆模板机:然后执行sh ./host_ip.sh 
​                   设置ip
​                   设置主机名

五、Systemd 进程管理

Linux一直以来都采用init进程作为爸爸,但是init有两个缺点:

1.启动时间长,init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
2.启动脚本复杂,初始化完成后,系统会加载很多脚本,脚本都会处理各自的情况,这回让脚本多而复杂。

CentOS5 是启动速度最慢的,串行启动过程,无论进程相互之间有无依赖关系。
CentOS6 相对启动速度有所改进,有依赖的进程之间依次启动而其他没有依赖关系的则并行启动。
CentOS7 所有进程无论有无依赖关系,都并行启动,当然有很多时候进程并没有真正的启动而是只有一个信号或者说是标记,在真正利用的时候,才会真正启动。

这就类似咱们使用的windows,最开始win95,win98,win7启动速度极其的慢,从win8,开始win8.1,win10启动速度,快的...噌一下子就起来了。

systemd即为system daemon守护进程,systemd主要解决上文的问题而诞生,systemd的目标是,为系统的启动和管理提供一套完整的解决方案。

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


/usr/lib/systemd/system #类似C6系统的启动脚本目录/etc/init.d/
/etc/systemd/system/ #类似C6系统的/etc/rc.d/rcN.d/
/etc/systemd/system/multi-user.target.wants/

由于之前长期使用RHEL/CentOS 6系统, 已经习惯使用service chkconfig等命令来管理系统服务,但在RHEL/CentOS 7系统中是使用systemctl命令来管理服务的。

如下是RHEL/CentOS 6系统中System V init命令与RHEL/CentOS 7系统中systemctl命令的对比,后续课程中会经常用到它们。

1.systemctl管理服务的启动、重启、停止、重载、查看状态等常用命令

System V init(6系统) systemctl命令(7系统) 作用
service crond start systemctl start crond.service *** 启动服务
service crond stop systemctl stop crond.service *** 停止服务
service crond restart systemctl restart crond.service *** 重启服务
service crond reload systemctl reload crond.service *** 平滑重启(不终止服务)
service crond status systemctl status crond.servre *** 查看服务运行状态
systemctl is-active sshd.service 查看服务是否在运行中
systemctl mask crond.servre 禁止服务运行
systemctl unmask crond.servre 取消禁止服务运行

2.systemctl设置服务开机启动、不启动、查看各级别下服务启动状态等常用命令

System V init(6系统) systemctl命令(7系统) 作用
chkconfig crond on systemctl enable crond.service *** 开机自动启动
chkconfig crond off systemctl disable crond.service *** 机关闭启动
chkconfig --list systemctl list-unit-files 查看各个级别下服务的启动与禁用
chkconfig --list crond systemctl is-enabled crond.service 查看特定服务是否为开机自启动
chkconfig--add crond systemctl daemon-reload 创建新服务文件或者变更设置

3.systemctl服务状态说明

服务状态 状态说明
loaded *** 服务单元的配置文件已经关闭
active(running) *** 服务的一个或多个进程在运行中
active(exited) 一次性运行的服务成功被执行并退出(服务运行后完成任务,相关进程会自动退出)
active(waiting) 服务已经运行但在等待某个事件
inactive 服务没有在运行
enable 服务设定为开机运行
disabled 服务设定为开机不运行
static 服务不能被设定开机启动,但可以由其他服务启动该服务

Guess you like

Origin www.cnblogs.com/gongjingyun123--/p/11247756.html