Linux GRUB 2及修改默认启动项

时间:2023-03-08 18:30:01

The GRUB 2 boot loader makes sure that you can boot Linux. GRUB 2 is installed in the boot sector of your server’s hard drive and is configured to load a Linux kernel and the initramfs:
■ The kernel is the heart of the operating system, allowing users to interact with the hardware that is installed in the server.
■ The initramfs contains drivers that are needed to start your server. It contains a mini file system that is mounted during boot. In it are kernel modules that are needed during the rest of the boot process (for example, the LVM modules and SCSI modules for accessing disks that are not supported by default).

Normally, GRUB 2 works just fine and does not need much maintenance. In some cases, though, you might have to change its configuration. To apply changes to the GRUB 2 configuration, the starting point is the /etc/default/grub file. In this file, you’ll find options that tell GRUB what to do and how to do it.

[root@rhel7 ~]# cat /etc/default/grub
GRUB_TIMEOUT=
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
[root@rhel7 ~]#

The most important part that it configures is the GRUB-CMDLINE_LINUX option. This line contains boot arguments for the kernel on your server.

Modifying Default GRUB 2 Boot Options  

To apply modifications to the GRUB 2 boot loader, the file /etc/default/grub is your entry point; do not change the contents of the /boot/grub2/grub.cfg(不要直接修改这个配置文件) configuration file directly. The most important line in this file is GRUB_CMDLINE_ LINUX, which defines how the Linux kernel should be started. In this line, you can apply permanent fixes to the GRUB 2 configuration. Some likely candidates for removal are the options  rhgb  and  quiet(删除这两个参数后,启动时显示详细信息) . These options tell the kernel to hide all output while booting. That is nice to hide confusing messages for end users, but if you are a server administrator, you probably just want  to remove these options.

Another interesting parameter is GRUB_TIMEOUT. This defines the amount of time your server waits for you to access the GRUB 2 boot menu before it continues booting automatically.

 Applying Modifications to GRUB2  

In this exercise you’ll apply some changes to the GRUB2 boot configuration and write them to the /boot/grub2/grub.cfg configuration file.

  • 1.   Open the file /etc/default/grub with an editor and remove the  rhgb  and  quiet  options from the GRUB_CMDLINE_LINUX line.
  • 2.   From the same file, set the GRUB_TIMEOUT parameter to 10 seconds. Save changes to the file and close the editor.
[root@rhel7 ~]# cat /etc/default/grub
GRUB_TIMEOUT=10
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap"
GRUB_DISABLE_RECOVERY="true"
[root@rhel7 ~]#
  • 3.   From the command line, type  grub2-mkconfig > /boot/grub2/grub.cfg  to write the changes to GRUB 2. (Note that instead of using the  redirector >  to write changes to the grub.cfg file, you could use the  -o  option. Both methods have the same result.)
[root@rhel7 ~]# grub2-mkconfig > /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.-.el7.x86_64
Found initrd image: /boot/initramfs-3.10.-.el7.x86_64.img
Found linux image: /boot/vmlinuz--rescue-c9e1b1f7c9514da7a2e9448646555486
Found initrd image: /boot/initramfs--rescue-c9e1b1f7c9514da7a2e9448646555486.img
done
[root@rhel7 ~]#
  • 4.   Reboot and verify that while booting you see boot messages scrolling by.