Linux

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > Linux > Linux-Redhat系统启动读取文件

Linux-Redhat系统启动读取文件方式

作者:feng68_

文章详细描述了RHEL系统从启动到登录的整个过程,包括读取关键配置文件、加载内核、挂载文件系统、启动初始化系统以及进入用户界面的步骤

文件引导启动系统

所要读取的文件:

之后读取/etc/fstab

之后读取/etc/sysconfig/selinux

之后读取/etc/systemd/system/multi-user.target.wants/下的软链接文件

1. /boot/grub2/grub.cfg

首先读的是/boot/grub2/grub.cfg这个文件

开机后会有两个内核标题

这两个标题看的是/boot/grub2/grub.cfg这个文件,这个文件中指定了下一步读取哪一个文件、timeout等于几秒等等配置

[root@Client ~]# vim /boot/grub2/grub.cfg
# timeout(就是开机启动时有5秒给你选择操控图形界面,不做操作的话就默认进入系统正常内核文件)指定为多少秒,默认是五秒,但是一般不修改这个文件,直接修改这个文件很危险,可以修改/etc/default/grub文件,当grub.cfg文件丢失后,系统无法完成自动启动
 terminal_output console
 if [ x$feature_timeout_style = xy ] ; then
   set timeout_style=menu
   set timeout=5
 # Fallback normal timeout code in case the timeout_style feature is
 # unavailable.
 else
   set timeout=5
 fi
 ​
 [root@Server ~]# vim /etc/default/grub  # 这个文件的内容是生成grub.cfg时使用的默认参数
 GRUB_TIMEOUT=5      # 修改这里参数是表示修改:grub启动时的等待时间
 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
 GRUB_DEFAULT=saved
 GRUB_DISABLE_SUBMENU=true
 GRUB_TERMINAL_OUTPUT="console"
 GRUB_CMDLINE_LINUX="crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=UUID=bfdecbb4-7f0a-43ca-b071-da64fa17f29b rhgb quiet net.ifnames=0 selinux=0"
 GRUB_DISABLE_RECOVERY="true"
 GRUB_ENABLE_BLSCFG=true
 [root@Server ~]# grub2-mkconfig > /boot/grub2/grub.cfg  # 生成新的grub.cfg并覆盖掉旧的grub.cfg

2. /boot/loader/entries/

之后读取/boot/loader/entries/下面的内核文件

 [root@Client ~]# ll /boot/loader/entries/
 total 8
 -rw-r--r--. 1 root root 507 Oct 26 22:18 3890aef9b6a64543b93a31bfc8c60d0d-0-rescue.conf     # 挽救内核文件
 -rw-r--r--. 1 root root 469 Oct 26 22:18 3890aef9b6a64543b93a31bfc8c60d0d-5.14.0-427.13.1.el9_4.x86_64.conf     # 正常内核文件
 ​
 [root@Client ~]# vim /boot/loader/entries/3890aef9b6a64543b93a31bfc8c60d0d-5.14.0-427.13.1.el9_4.x86_64.conf
 title Red Hat Enterprise Linux (5.14.0-427.13.1.el9_4.x86_64) 9.4 (Plow)
 version 5.14.0-427.13.1.el9_4.x86_64    # 标题
 linux /vmlinuz-5.14.0-427.13.1.el9_4.x86_64     # 引导内核,读取内核镜像
 initrd /initramfs-5.14.0-427.13.1.el9_4.x86_64.img $tuned_initrd    # 读取(加载)系统初始化镜像
 options root=UUID=a591379a-75cc-41c4-85d3-45e085821016 ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=UUID=bfdecbb4-7f0a-43ca-b071-da64fa17f29b rhgb quiet net.ifnames=0 selinux=0  # 启动参数  
 grub_users $grub_users
 grub_arg --unrestricted
 grub_class rhel
 ​
 # 之前使用grub命令就是改变这个文件的“启动参数”的位置
 grubby --update-kernel ALL --args net.ifnames=0
 grubby --update-kernel ALL --args selinux=0

虚拟机按e后会发现内容和上面查看的文件的内容一样

3. 内核文件、系统初始化镜像文件

 [root@Client ~]# ll /boot
 total 214032
 -rw-r--r--. 1 root root    223205 Apr 10  2024 config-5.14.0-427.13.1.el9_4.x86_64
 drwxr-xr-x. 3 root root        17 Oct 25 13:14 efi
 drwx------. 5 root root        97 Oct 29 16:51 grub2
 -rw-------. 1 root root 117325156 Oct 25 13:16 initramfs-0-rescue-3890aef9b6a64543b93a31bfc8c60d0d.img
 -rw-------. 1 root root  34653703 Oct 25 13:18 initramfs-5.14.0-427.13.1.el9_4.x86_64.img   # 系统初始化镜像文件
 -rw-------. 1 root root  31115776 Oct 25 13:22 initramfs-5.14.0-427.13.1.el9_4.x86_64kdump.img
 drwxr-xr-x. 3 root root        21 Oct 25 13:15 loader
 lrwxrwxrwx. 1 root root        52 Oct 25 13:15 symvers-5.14.0-427.13.1.el9_4.x86_64.gz -> /lib/modules/5.14.0-427.13.1.el9_4.x86_64/symvers.gz
 -rw-------. 1 root root   8629622 Apr 10  2024 System.map-5.14.0-427.13.1.el9_4.x86_64
 -rwxr-xr-x. 1 root root  13604984 Oct 25 13:16 vmlinuz-0-rescue-3890aef9b6a64543b93a31bfc8c60d0d
 -rwxr-xr-x. 1 root root  13604984 Apr 10  2024 vmlinuz-5.14.0-427.13.1.el9_4.x86_64     # 系统内核文件

读完这两个文件后,没问题就开始启动内核(vmlinuz-5.14.0-427.13.1.el9_4.x86_64),它的主要作用是只读挂载根分区(/)

4. 开始启动内核

4.1 /etc/fstab

目的是确定系统中还有哪些磁盘需要挂载

[root@Client ~]# vim /etc/fstab
​
 #
 # /etc/fstab
 # Created by anaconda on Sat Oct 25 05:13:56 2025
 #
 # Accessible filesystems, by reference, are maintained under '/dev/disk/'.
 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
 #
 # After editing this file, run 'systemctl daemon-reload' to update systemd
 # units generated from this file.
 #
 UUID=a591379a-75cc-41c4-85d3-45e085821016 /                       xfs     defaults        0 0
 UUID=3d02312d-1efc-4336-9505-125191f59fa3 /boot                   xfs     defaults        0 0
 UUID=bfdecbb4-7f0a-43ca-b071-da64fa17f29b none                    swap    defaults        0 0
 ​

4.2 /etc/sysconfig/selinux

目的是通过此文件里面的参数来确认是否激活selinux(我记得哪个网卡默认命名(grub……net.ifnames=0命令那个)那个文件也读)

[root@Client ~]# vim /etc/sysconfig/selinux
 # This file controls the state of SELinux on the system.
 # SELINUX= can take one of these three values:
 #     enforcing - SELinux security policy is enforced.
 #     permissive - SELinux prints warnings instead of enforcing.
 #     disabled - No SELinux policy is loaded.
 # See also:
 # https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/using_selinux/changing-selinux-states-and-modes_using-selinux#changing-selinux-modes-at-boot-time_changing-selinux-states-and-modes
 #
 # NOTE: Up to RHEL 8 release included, SELINUX=disabled would also
 # fully disable SELinux during boot. If you need a system with SELinux
 # fully disabled instead of SELinux running with no policy loaded, you
 # need to pass selinux=0 to the kernel command line. You can use grubby
 # to persistently set the bootloader to boot with selinux=0:
 #
 #    grubby --update-kernel ALL --args selinux=0
 #
 # To revert back to SELinux enabled:
 #
 #    grubby --update-kernel ALL --remove-args selinux
 #
 SELINUX=enforcing
 # SELINUXTYPE= can take one of these three values:
 #     targeted - Targeted processes are protected,
 #     minimum - Modification of targeted policy. Only selected processes are protected.
 #     mls - Multi Level Security protection.
 SELINUXTYPE=targeted

然后修改系统的时钟

然后对硬件开始初始化

到此内核启动结束了

5. 开始系统初始化

内核启动结束后开始读取“系统初始化镜像文件”(initramfs-5.14.0-427.13.1.el9_4.x86_64.img)

主要作用是加载初始化系统,开始进行系统初始化

5.1 /etc/systemd/system/multi-user.target.wants/下的软链接文件

首先把根(/)读写挂载,进入/etc/systemd/system/multi-user.target.wants/中读取链接文件,它们都是链接到做开机自启动服务的服务脚本

[root@Client ~]# ll /etc/systemd/system/multi-user.target.wants/

6. /etc/rc.d/rc.local

读取完这些链接后,最后会读取开机自运行脚本(/etc/rc.d/rc.local)

[root@Client ~]# ll /etc/rc.d/rc.local
 -rwxr-xr-x. 1 root root 497 Oct 26 22:15 /etc/rc.d/rc.local

7.tty

读取完开机自运行脚本后开始读取tty

❓tty是什么

tty启动完后启动图形(如果开图形的话就显示图形登录界面)如果没开就显示那个黑黑的登录界面

至此,rhel系统启动完成

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文