Grubby: Modifying Grub Configurations on Linux

Now, before I start, let me just say this: if you don’t know what Grub is or why it matters, then you probably shouldn’t be messing with its configuration in the first place. But hey, we all have to learn somehow!

So, what exactly is Grub? Well, for those of us who are too lazy to Google it (which is most of us), Grub stands for Grand Unified Bootloader. Its a boot manager that loads your operating system when you turn on your computer. And if you’re like me and have multiple operating systems installed on your machine, then Grub is the magic sauce that lets you choose which one to use at startup.

Now, let’s say you want to add a new operating system to your list of boot options. Or maybe you want to change the order in which they appear. Well, my friend, youre going to need to edit Grub’s configuration file and that’s where things get exciting!

First off, let me warn you: messing with Grub can be dangerous if you don’t know what you’re doing. So, make sure you have a backup of your current configuration before proceeding. And by “backup,” I mean copy the file to another location or take a screenshot because trust me, you won’t remember all those lines of code after you mess them up!

Alright, now that weve got that out of the way, let’s get started. To edit Grub’s configuration file, open your terminal and type:

#!/bin/bash
# This is a bash script used to edit Grub's configuration file.
# It opens the file using the nano text editor with root privileges.

sudo nano /etc/grub.d/40_custom

# The "sudo" command allows the user to run a command with root privileges.
# "nano" is a text editor that allows for easy editing of files in the terminal.
# "/etc/grub.d/40_custom" is the path to the Grub configuration file that we want to edit.

This will open a text editor called “nano” with the 40_custom file in the grub directory. This is where you’ll add your custom configuration lines.

Now, let me give you an example of what those lines might look like:

# This script is used to add a custom configuration line to the 40_custom file in the grub directory.

# The following code creates a menu entry for Windows 10 with the label "Windows 10 (UEFI)".
# The curly braces indicate the start and end of the menu entry.
menuentry 'Windows 10 (UEFI)' {

    # The following code sets the root device to 'hd0,gpt2'.
    # This is the device where the Windows 10 operating system is installed.
    set root='hd0,gpt2'

    # The following code loads the Windows bootloader.
    # This is necessary for booting into Windows 10.
    chainloader +1
}

In this case, were adding a new entry for Windows 10 that uses UEFI boot. The “set” command sets the root directory to the second partition on our hard drive (which is where Windows is installed). And then the “chainloader” command tells Grub to load the operating system from there.

Once you’ve added your custom lines, save and close the file by pressing Ctrl + X, Y, Enter. Then run:

bash
#!/bin/bash # This line specifies the interpreter to be used for executing the script

sudo update-grub # This command updates the GRUB bootloader configuration, which is responsible for loading the operating system on boot.

# The following lines are custom commands to set the root directory and load the operating system from there.



sudo grub-install /dev/sda2 # This command installs GRUB on the second partition of the hard drive, where Windows is installed.
sudo update-grub # This command updates the GRUB configuration again to include the newly installed GRUB on the correct partition.

# The following lines are optional and can be used to customize the GRUB menu.
# They allow you to add custom entries or change the default operating system to be loaded on boot.

sudo nano /etc/default/grub # This command opens the GRUB configuration file in the nano text editor.
# Make any necessary changes to the GRUB menu and save the file.
sudo update-grub # This command updates the GRUB configuration again to include any changes made.

# Once all the necessary changes have been made, the script can be closed and the changes will take effect on the next boot.

This will regenerate Grub’s configuration files based on our changes. And thats it! Your new operating system should now appear in your boot menu.

Now, let me just say this if you mess up the syntax or forget a semicolon somewhere, then you might end up with an error message like “Error: unknown command ‘foo’”. But hey, that’s part of the learning process! Just remember to take it slow and double-check your work.

And there you have it modifying Grub configurations on Linux in a nutshell. It may not be as exciting as hacking into a government database or something, but trust me, itll make you feel like a real-life computer wizard!

SICORPS