How to compile a kernel on CentOS 7

Install Required Packages for Kernel Compilation

To get started we’ll need to make sure all repositories are up to date and that te system its self is up to date as well, then we’ll install the development tools that we’ll need to compile the kernel, and we should also install the nurses library using the following yum command.

# yum update
# yum install -y ncurses-devel make gcc bc bison flex elfutils-libelf-devel openssl-devel grub2

Compile and Install Kernel in CentOS 7

Let’s download the latest Kernel 4.17 sources using wget command under /usr/src/ directory or you can also download the latest kernel by going to kernel.org

# cd /usr/src/
# wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.17.11.tar.xz

Extract the archived files and change directories using following commands.

# tar -xvf linux-4.17.11.tar.xz
# cd linux-4.17.11/

Configure the Kernel in CentOS 7

The Kernel must be correctly configured with the following required configuration options within the CentOS 7environment.

CONFIG_KVM_GUEST=y
CONFIG_VIRTIO_PCI=y
CONFIG_VIRTIO_PCI_LEGACY=y
CONFIG_BLK_DEV_SD
CONFIG_SCSI_VIRTIO=y
CONFIG_VIRTIO_NET=y
CONFIG_SERIAL_8250=y
CONFIG_SERIAL_8250_CONSOLE=y

It is important that you copy the running kernel configuration (.config) from the /boot directory to new kernel linux-4.17.11 directory.

# cp -v /boot/config-3.10.0-693.5.2.el7.x86_64 /usr/src/linux-4.17.11/.config

We’re now ready to run the make menuconfig command to configure the Linux kernel. Once you execute the below command a pop up window will appear with all the menus. This is where you can enable or disable kernel features, however if you unfamiliar with these menus, it’s best if you just hit ESC key to exit and continue as is.

# cd /usr/src/linux-4.17.11/
# make menuconfig

Once your kernel configuration options are set, click on Save to save the configuration interface and exit from the menu.

Make Menuconfig

Save Kernel Configuration

Compile the Kernel in CentOS 7

Before starting kernel compilation, make sure your system has more than 25GB of free space on the file system. To confirm, you can check the file system free space using df command as shown.

# df -h

Now we can compile and install the kernel and modules using following commands (it may take several hours depending on the available resources). The compilation process place files under /boot directory and also make a new kernel entry in your grub.conf file.

# make bzImage
# make modules
# make
# make install
# make modules_install

Once the compilation completes, reboot the system and verify newly installed Kernel.

# uname -sr

You should be able to verify the kernel version from the output shown by the previous command and that’s it!