linux——系统排错之引导恢复shell

一、grub 磁盘引导阶段

MBR

mbr 的作用是为了记录 /boot 目录所在分区位置,磁盘的 0 磁道 1 扇区的前 446 字节

[root@localhost Desktop]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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 Desktop]# dd if=/dev/zero of=/dev/vda bs=446 count=1
1+0 records in
1+0 records out
446 bytes (446 B) copied, 0.000137605 s, 3.2 MB/s
[root@localhost Desktop]# reboot

重启后的操作如下图:

400

扫描二维码关注公众号,回复: 910241 查看本文章

上图过后,还有一个选择,也是选择OK,就会进入下图界面



二、grub 文件引导阶段

当你用命令 rm -fr 误删除了/boot/grub2/grub.cfg文件时,介绍两种恢复方法。

1. 当发现误删除该文件后,如果你还没有重启系统的话,那么恭喜你,这很好恢复,如下操作:

[root@localhost Desktop]# rm -fr /boot/grub2/grub.cfg
[root@localhost Desktop]# grub2-mkconfig > /boot/grub2/grub.cfg    ##因为文件grub2-mkconfig里的内容与删掉的文件内容一样
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-123.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-123.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
Found initrd image: /boot/initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
done
[root@localhost Desktop]#    ##恢复文件成功
2. 当发现误删除该文件后,如果你已经重启系统的话,那就好好的跟着下图的操作做吧
操作截图:


注意:进入系统后,一定要执行下面的命令!!!不然下次还需要重新引导

[root@localhost Desktop]# grub2-mkconfig > /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-123.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-123.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
Found initrd image: /boot/initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
done
[root@localhost Desktop]#

三、内核引导阶段(一)

错误执行 rm -fr vmlinuz-3.10.0-123.el7.x86_64 该条命令后

[root@localhost Desktop]# cd /boot/
[root@localhost boot]# ls
config-3.10.0-123.el7.x86_64
grub
grub2
initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
initramfs-3.10.0-123.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-123.el7.x86_64.gz
System.map-3.10.0-123.el7.x86_64
vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
vmlinuz-3.10.0-123.el7.x86_64
[root@localhost boot]# rm -fr vmlinuz-3.10.0-123.el7.x86_64
[root@localhost boot]# ls
config-3.10.0-123.el7.x86_64
grub
grub2
initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
initramfs-3.10.0-123.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-123.el7.x86_64.gz
System.map-3.10.0-123.el7.x86_64
vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817

1. 若还没有重启系统,可做如下操作恢复

[root@localhost boot]# yum whatprovides /boot/vmlinuz-3.10.0-123.el7.x86_64
Loaded plugins: langpacks
rhel_dvd/filelists_db                                        | 3.0 MB  00:00:00
kernel-3.10.0-123.el7.x86_64 : The Linux kernel
Repo        : rhel_dvd
Matched from:
Filename    : /boot/vmlinuz-3.10.0-123.el7.x86_64



kernel-3.10.0-123.el7.x86_64 : The Linux kernel
Repo        : installed
Matched from:
Filename    : /boot/vmlinuz-3.10.0-123.el7.x86_64



[root@localhost ~]# cd /run/media/root/RHEL-7.0\ Server.x86_64/
[root@localhost RHEL-7.0 Server.x86_64]# ls
addons  images      Packages                 RPM-GPG-KEY-redhat-release
EFI     isolinux    release-notes            TRANS.TBL
EULA    LiveOS      repodata
GPL     media.repo  RPM-GPG-KEY-redhat-beta
[root@localhost RHEL-7.0 Server.x86_64]# cd Packages/
[root@localhost Packages]# pwd
/run/media/root/RHEL-7.0 Server.x86_64/Packages
[root@localhost Packages]# cp kernel-3.10.0-123.el7.x86_64.rpm /root/Desktop/
[root@localhost boot]# cd /root/Desktop/
[root@localhost Desktop]# ls
kernel-3.10.0-123.el7.x86_64.rpm
[root@localhost Desktop]# file kernel-3.10.0-123.el7.x86_64.rpm
kernel-3.10.0-123.el7.x86_64.rpm: RPM v3.0 bin i386/x86_64 kernel-3.10.0-123.el7
[root@localhost Desktop]# rpm2cpio kernel-3.10.0-123.el7.x86_64.rpm | cpio -id
219282 blocks
[root@localhost Desktop]# ls
boot  etc  kernel-3.10.0-123.el7.x86_64.rpm  lib
[root@localhost Desktop]# cp boot/vmlinuz-3.10.0-123.el7.x86_64 /boot/
[root@localhost Desktop]#     ##恢复成功
2. 若已经重启了系统,那么接下来的操作如下图:


四、内核引导阶段(二)

错误执行 rm -fr /boot/initramfs-3.10.0-123.el7.x86_64.img 该条命令后

[root@localhost Desktop]# cd /boot/
[root@localhost boot]# ls
config-3.10.0-123.el7.x86_64
grub
grub2
initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
initramfs-3.10.0-123.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-123.el7.x86_64.gz
System.map-3.10.0-123.el7.x86_64
vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
vmlinuz-3.10.0-123.el7.x86_64
[root@localhost boot]# rm -fr /boot/initramfs-3.10.0-123.el7.x86_64.img
[root@localhost boot]# ls
config-3.10.0-123.el7.x86_64
grub
grub2
initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
initrd-plymouth.img
symvers-3.10.0-123.el7.x86_64.gz
System.map-3.10.0-123.el7.x86_64
vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
vmlinuz-3.10.0-123.el7.x86_64
1. 若还没有重启系统,可做如下操作恢复
[root@localhost boot]# rm -fr /boot/initramfs-3.10.0-123.el7.x86_64.img
[root@localhost boot]# mkinitrd /boot/initramfs-3.10.0-123.el7.x86_64.img 3.10.0-123.el7.x86_64
[root@localhost boot]# ls
config-3.10.0-123.el7.x86_64
grub
grub2
initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
initramfs-3.10.0-123.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-123.el7.x86_64.gz
System.map-3.10.0-123.el7.x86_64
vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
vmlinuz-3.10.0-123.el7.x86_64
[root@localhost boot]#              ##恢复成功
2. 若已经重启了系统,那么接下来的操作如下图:



五、启动级别被改变后所引起的系统问题

启动级别设置

systemctl set-default + 启动级别

[root@localhost Desktop]# systemctl set-default multi-user.target   
rm '/etc/systemd/system/default.target'
ln -s '/usr/lib/systemd/system/multi-user.target' '/etc/systemd/system/default.target'
[root@localhost Desktop]# systemctl set-default shutdown.target
rm '/etc/systemd/system/default.target'
ln -s '/usr/lib/systemd/system/shutdown.target' '/etc/systemd/system/default.target'
[root@localhost Desktop]# reboot
执行该操作后,在重启时会遇到这样的问题


此时的操作如下图所示:


注意:进入系统后,一定要执行下面的命令!!!不然下次还需要重新设置

[root@localhost Desktop]# systemctl set-default graphical.target
rm '/etc/systemd/system/default.target'
ln -s '/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'
[root@localhost Desktop]#

系统启动级别介绍

runlever0.target -> poweroff.target
runlever1.target -> rescue.target
runlever2.target -> multi-user.target
runlever3.target -> multi-user.target
runlever4.target -> multi-user.target
runlever5.target -> grapical.target
runlever6.target -> reboot.target
运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆
运行级别2:多用户状态(没有NFS)
运行级别3:完全的多用户状态(有NFS),登陆后进入控制台命令行模式
运行级别4:系统未使用,保留
运行级别5:X11控制台,登陆后进入图形GUI模式
运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动

猜你喜欢

转载自blog.csdn.net/hh5820/article/details/80344657