Compiling Your Own Linux Kernel (Red Hat)

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
364
Reaction score
403
Credits
13,257
For this article, I list the title as Red Hat, but I have only tested this on CentOS 9 and Fedora 39.

I am also compiling these on new installs as a virtual machine on Oracle's VirtualBox.

There are a few minor differences between compiling a kernel on CentOS or Fedora, but we'll get to that soon.

Kernel Version

After installing a new Operating System (OS), it most likely does not have the most current kernel. Some systems can have the kernel update when you perform a system update, but at other times, you will need to compile your own.

By compiling your own kernel, you can select the version as well as which drivers to include in the kernel.

The place to get the kernel source code for Linux is at www.kernel.org. Here, you can find the most current stable version, or if you prefer, older versions.

Just select the version you want to use. In examples for this article, I will look at kernel version 6.6.9. Looking at the website, www.kernel.org, I can get the compressed code at 'https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.6.9.tar.xz'.

Before we get the source code, we need to prepare the system for compiling the kernel.

Preparing the System

Before getting to the packages to compile the source, we should update the system:

Code:
sudo yum update -y

To compile and install the kernel source code, you need to install the packages. Install:

Code:
sudo yum install bc bison dwarves flex git ncurses-devel.x86_64 rpm-build rsync wget -y
sudo yum groupinstall "Development Tools" -y

For Fedora, you need to also install:

Code:
sudo yum install openssl perl -y

I won't worry about the Graphical User Interface (GUI) version since it has limitations compared to the text-based version.

Now that we have the basics, we can get the source code. Before downloading the source code, we need to get root privileges. Perform a 'sudo -i'. This should move you to '/root/' as the Home directory.

When we download the source code, it will go into '/root' and not our personal Home folder. Perform:

Code:
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.6.9.tar.xz

As long as you typed the command correctly and the file exists on the 'kernel.org' server, it should download.

A copy of the file should now be local, and we can extract the compressed file.

Code:
tar xvf linux-6.6.9.tar.xz

Depending on your system, this could take a bit to extract all the files into a folder with the same name in the current folder. For example, my folder is 'linux-6.6.9'. Once completing the extraction, change into the new folder, such as 'cd linux-6.6.9'.

To make things easier, we can get the information on the current kernel to be used when creating the new one:

Code:
cp -v /boot/config-$(uname -r)* .config

The new '.config' file has settings in it for the current kernel and we can use this as a base to start.

If you haven't done it, you need to turn off your 'Power' option to turn the screen of if there is inactivity. Sometimes, I have noticed that the screen won't update, but the screen does not flip to an inactivity screen until you move the mouse. This is more important once you start the actual compiling.

Changing Settings

There are a few settings we need to change that we can do through a script. We can simply run the following three commands to make these changes:

Code:
scripts/config --disable DEBUG_INFO
scripts/config --set-str SYSTEM_TRUSTED_KEYS ""
scripts/config --set-str SYSTEM_REVOCATION_KEYS ""

It would not be easy to go through the '.config' file to make changes. So, we have a text-based tool, as shown in Figure 1. We start the program with the command 'make menuconfig'.

Figure 1.JPG

FIGURE 1

You can use the arrow keys to move up and down the list. Press enter to enter a sub-menu. Press Escape twice to exit the sub-menu. You can use the spacebar to change options. The system loads anything that is marked with an asterisk (*) in memory. If the option has an (M), this is a loadable module, and unless you load it, it is not in memory. You can remove anything you do not need. Be careful to not remove anything you need.

If there is an option you wish to search for, use the forward slash (/) key. When prompted, type in the string that you are searching.

On Fedora, two options you can search for are 'PCMCIA' and 'Radio' to disable.

One option is to remove network cards you do not use on a system. Go to 'Device Drivers', 'Network Device Support', then 'Ethernet Driver Support'. Go through the list and disable any Interface Card manufacturers that you do not use.

Once finished, save the changes to the '.config' file and exit the menuconfig.

Now comes the part that takes a while. To start the compiling, use the command:

Code:
make -j# binrpm-pkg LOCALVERSION=-test

You can replace the hashtag (#) with the number of cores you want to use for compiling the kernel. For your preference, you can change the suffix '-test' to whatever you may want. Remember that the appended suffix to the kernel name and shown in the GRUB menu. Another thing, this will build the kernel to your specs that you specified and create an RPM file for installation on Red Hat systems.

Once the compiling has completed, you can find the RPM files in '/root/linux-6.6.9/rpmbuild/RPMS/x86_64/'. There should be two files: one is the kernel and the other is the kernel headers. Also, the folder 'linux-6.6.9' will vary depending on the version of the kernel you are compiling.

To install the files, switch into the folder and run the command:

Code:
rpm -i *.rpm

Now you only need to reboot, and GRUB default should load the new kernel.

NOTE: On Fedora, you cannot install the RPM files. You need to perform the command 'make install' to get the compiled information installed.

Before installing, you need to run a command to remove any kernel headers that conflict with the new headers:

Code:
yum remove kernel-headers -y

Conclusion

After rebooting the system, use your new kernel. You can tell by opening a terminal and running 'uname -mrs' to see the kernel version.

For other systems, you can copy the RPM files that were created and copy them to an accessible media for installing.
 
Last edited:


I like xmenuconfig, because I can use a mouse. :)
If you're creating a kernel for use on a system with secure boot, you'll have include the ssl stuff also.

Nice article.
 

Members online


Latest posts

Top