且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

linux命令:grub 文件详解及grub修复,系统常见故障修复

更新时间:2022-08-15 09:29:41

 首先模拟grub损坏,dd if=/dev/zero of=/dev/sda count=1 bs=400

  grub引导破坏,但是此时并未重启,可以直接重新安装grub,从而达到恢复grub:

  第一种方法:

  1、首先输入grub 进入grub命令行

[root@xuelinux networking]# grub

Probing devices to guess BIOS drives. This may take a long time.

    GNU GRUB  version 0.97  (640K lower / 3072K upper memory)

 [ Minimal BASH-like line editing is supported.  For the first word, TAB

   lists possible command completions.  Anywhere else TAB lists the possible

   completions of a device/filename.]

 grub>root (hd0,0)   制定内核所在分区 

 root (hd0,0)

 Filesystem type is ext2fs, partition type 0x83 

 grub> setup (hd0) 直接使用setup安装,setup (hd0)直接安装在hd0第一引导的硬盘上

 setup (hd0)

 Checking if "/boot/grub/stage1" exists... no

 Checking if "/grub/stage1" exists... yes

 Checking if "/grub/stage2" exists... yes

 Checking if "/grub/e2fs_stage1_5" exists... yes

 Running "embed /grub/e2fs_stage1_5 (hd0)"...  27 sectors are embedded.

 succeeded

 Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded

Done.  

 grub> quit  然后退出即可

 quit

 第二种方法:

 #grub-install --root-directory=/PATH/TO/boot父目录 /dev/sda  指定/boot的父目录及当前硬                                             盘设备



  1、cat /etc/grub.conf 该配置文件时/boot/grub的链接文件:

# grub.conf generated by anaconda

#

# Note that you do not have to rerun grub after making changes to this file

# NOTICE:  You have a /boot partition.  This means that

#          all kernel and initrd paths are relative to /boot/, eg.

#          root (hd0,0)

#          kernel /vmlinuz-version ro root=/dev/sda2

#          initrd /initrd-[generic-]version.img

#boot=/dev/sda

default=0 # 设定默认启动的title编号,从0开始,从上往下排序。

timeout=5  # 等待用户选择的超时时长,单位是秒

splashimage=(hd0,0)/grub/linux.xpm.gz # grub的背景图片

hiddenmenu  # 隐藏菜单

password --md5 $1$7yZf4/$Bo.1Rg8dZNcimFoMud6Ed0 # 使得进入维护模式需要使用密码

title CentOS (2.6.32-431.el6.i686) # 内核标题或者操作系统名称,字符串,可***修改

root (hd0,0) # 内核文件所在的设备,对grub而言,所有类型硬盘一律为hd;

          格式为(hd#,N);hd#,#表示第几个磁盘,最后的0表示对应磁盘的分区

kernel /vmlinuz-2.6.32-431.el6.i686 ro root=UUID=47b74772-4295-493a-8aa6-8fc85ae0807d rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet # 内核文件路径/boot目录,及传递给内核的参数

initrd /initramfs-2.6.32-431.el6.i686.img # ramdisk文件路径/boot目录中


对grub加密:grub-md5-crypt 生成加密字符串


[root@xuelinux ~]# grub-md5-crypt 

Password: 

Retype password: 

$1$5vIg4/$xqmsVS.9l2m99sZYaCfEb/  复制该加密字符串

然后修改grub.conf文件,vim /boot/grub/grub.conf

 # grub.conf generated by anaconda

 #

 # Note that you do not have to rerun grub after making changes to this file

 # NOTICE:  You have a /boot partition.  This means that

 #          all kernel and initrd paths are relative to /boot/, eg.

 #          root (hd0,0)

 #          kernel /vmlinuz-version ro root=/dev/sda2

 #          initrd /initrd-[generic-]version.img

 #boot=/dev/sda

 default=0

 timeout=6

 splashimage=(hd0,0)/grub/splash.xpm.gz

 hiddenmenu

 password --md5 $1$5vIg4/$xqmsVS.9l2m99sZYaCfEb/  添加加密密码

 title My CentOS6.5 (2.6.32-431.el6.i686)

          root (hd0,0)

          kernel /vmlinuz-2.6.32-431.el6.i686 ro root=UUID=47b74772-4295-493a-8aa6-8fc85ae0807d rd_NO    _LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rh    gb quiet

          initrd /initramfs-2.6.32-431.el6.i686.img


2、纯字符界面的分辨率调整:

grep"CONFIG_FRAMEBUFFER_CONSOLE"/boot/config-2.6.32-279.e16.i686

 查询内核是否支持分辨率修改

修改grub.conf配置文件,从而达到调整分辨率

vim /boot/grub/grub.conf

 default=0

   ....

 kernel /..........     quiet  vga=791 此字符串为修改分辨率的命令


3、系统修复模式

  1、开机按e或者p键进入选择内核再按e quiet 1 再按b键,即可进入单用户维护模式 

  2、单用户模式常见的的错误修复

    1)、root密码的重置

    2)、修改系统默认的运行级别 vim /etc/inittab


4、光盘修复模式

 先从光盘引导,选择rescue installed system 系统修复,进入修复界面:

 bash-4.1#  重要系统文件丢失,导致系统无法启动时,光盘修复步骤如下:

  假设系统文件丢失了/etc/inittab文件

 bash-4.1# chroot /mnt/sysimage  临时把光盘根目录设置为系统根目录

       # cd /root

       # rpm -qf /etc/inittab  查询下/etc/inittab文件属于那个安装包

       # mkdir /mnt/cdrom  建立挂载点

       # mount /dev/sr0 /mnt/cdrom  挂载光盘

 bash-4.1#rpm2cpio /mnt/cdrom/Packages/initscripts-8.45.3-l.i386.rpm | 

 cpio-idv ./etc/inittab   提取inittab文件到当前目录.

 bash-4.1#cp ./etc/inittab /etc/inittab  复制inittab文件到指定位置


5、linux安全性

 用户密码-->grub密码-->Bios加密-->锁起来

针对各种安全有不同的解锁方式:

 用户密码丢失,通过单用户模式重置,grub密码可以通过光盘修复模式重置

 Bios密码可以通过拨除主板电池清除,锁起来直接暴力把锁弄掉


 当开机后进入grub界面但没有菜单,只剩下grub>提示符的解决方法:

  grub>cat /boot/grub/grub.conf 查看grub.conf配置文件的参数

   grub>root (hd0,0)

   grub>kernl (hd0,0) /vmlinuz-2.6.32-431.el6.i686

    grub>initrd (hd0,0) /initramfs-2.6.32-431.el6.i686.img

    grub>boot 重启











本文转自wang650108151CTO博客,原文链接: http://blog.51cto.com/woyaoxuelinux/1880342,如需转载请自行联系原作者