How to compile a new kernel ( in Linux CentOS 5.3 / Red Hat 5.3

You purchased a new hardware but the current kernel does not support it, or need to add functionality to the kernel that does not come from the factory.

It is at those times that is necessary to compile a new kernel for the device to be recognized or a new functionality can be used by the software, for example.

I will show you how to compile a new kernel on CentOS / Red Hat Linux.


Download latest kernel do Linux

The latest version can be get straight from

Downloading the kernel source (at this moment is


After you have done it, extract to /usr/src

tar jxvf linux- -C /usr/src

To made things simple, go to /usr/src and create a symlink to source code called linux, do this:

cd /usr/src
ln -sf linux- linux


ls -al
drwxr-xr-x  3 root root 4.0K Sep  2 06:18 .
drwxr-xr-x 13 root root 4.0K Aug 31 14:25 ..
lrwxrwxrwx  1 root root   14 Sep  2 06:18 linux -> linux-
drwxrwxr-x 22 root root 4.0K Aug 16 18:19 linux-

Preparing the environment to compile Kernel

To compile the Kernel on Linux, the OS need to be prepared, with gcc, ncurses etc.

If your CentOS or Red Hat installation is the minimal, the devel packages weren’t installed, it is needed to do it now, it’s simple with Yum that will resolve all dependencies.

yum install gcc make bison ncurses-devel rpm-build
Installed: bison.i386 0:2.3-2.1 gcc.i386 0:4.1.2-44.el5 ncurses-devel.i386 0:5.5-24.20060715
Dependency Installed: cpp.i386 0:4.1.2-44.el5 glibc-devel.i386 0:2.5-34.el5_3.1 glibc-headers.i386 0:2.5-34.el5_3.1 kernel-headers.i386 0:2.6.18-128.7.1.el5 libgomp.i386 0:4.3.2-7.el5
Updated: glibc.i686 0:2.5-34.el5_3.1 glibc-common.i386 0:2.5-34.el5_3.1 nscd.i386 0:2.5-34.el5_3.1

Compiling the kernel to CentOS or Red Hat

Before starting the compilation, it is interesting to clean the garbage that should left behind.

make clean

A good idea is to take the parameters of configuration that comes with the factory one, this file can be found at /boot dir, let’s copy it to the root of our kernel source and execute make menuconfig.

cd /usr/src/linux
cp /boot/config-`uname -r` .config

Be sure to stay on /usr/src/linux.
Accessing the configuration menu is the easy way to configure it.

make menuconfig

As this is a custom compilation, I suggest you to add a custom tag that will identify the new kernel, to accomplish this, do that:

  • On initial screen, go to General setup  —>
  • Select Local version – append to kernel release

With this done, the resulting kernel.rpm will have the tag identifying it.

It is time to customize the kernel configuration to fit with your hardware.

After this task, it’s time to quit from menuconfig and start the compilation.

While you get out the configuration, you will be asked to save the changes. Answer Yes.

The most awaited time… compiling!

Run make rpm

make rpm
  HOSTLD  scripts/kconfig/conf
scripts/kconfig/conf -s arch/x86/Kconfig
make clean
set -e; cd ..; ln -sf /usr/src/linux- kernel-
/bin/sh /usr/src/linux- > /usr/src/linux-
set -e; cd ..; tar -cz --exclude SCCS --exclude BitKeeper --exclude .svn --exclude CVS --exclude .pc --exclude .hg --exclude .git -f kernel- kernel-

The compilation will start, it will take a looong time.

At the end of process, without errors:

Wrote: /usr/src/redhat/SRPMS/kernel-
Wrote: /usr/src/redhat/RPMS/i386/kernel-

The kernel was compiled and a .rpm was generated to be installed.

To confirm that the new kernel really exists in a rpm format, run:

ls -alh /usr/src/redhat/RPMS/i386/
total 164M
drwxr-xr-x 2 root root 4.0K Sep  2 11:45 .
drwxr-xr-x 9 root root 4.0K Sep  2 10:39 ..
-rw-r--r-- 1 root root 164M Sep  2 11:45 kernel-

RPM of the new kernel (32 bits)

I’m putting the compiled kernel and packaged here, feel free to download it.

Installing the new kernel

How was generated a RPM of kernel, the installation becomes quite simple

rpm -ivh /usr/src/redhat/RPMS/i386/kernel-
Preparing...                ########################################### [100%]
   1:kernel                 ########################################### [100%]

Confirming that the new kernel was installed.

ls /boot
config-2.6.18-128.el5                 symvers-2.6.18-128.el5.gz
initrd-2.6.18-128.el5.img             vmlinux-
initrd-  vmlinuz-2.6.18-128.el5
lost+found                            vmlinuz-

Creating the initrd for the new kernel

To create the dependencies for the modules:


You must create a new initrd so the OS can start and pre-load the modules necessary to your hardware.

mkinitrd -v /boot/initrd-

In case the following error occour:

No module dm-mem-cache found for kernel, aborting.
To work around this, use the parameter--without-dmraid with mkinitrd
mkinitrd --without-dmraid -v /boot/initrd-

Editing grub

Final phase, it remains only edit the grub.conf to use the new kernel.

With your favorite text editor, add the following lines:

title CentOS (
        root (hd0,0)
        kernel /vmlinuz- ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-

The file /boot/grub/grub.conf will be like this:

# 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/VolGroup00/LogVol00
#          initrd /initrd-version.img
title CentOS (2.6.18-128.el5)
	root (hd0,0)
	kernel /vmlinuz-2.6.18-128.el5 ro root=/dev/VolGroup00/LogVol00
	initrd /initrd-2.6.18-128.el5.img
title CentOS (
        root (hd0,0)
        kernel /vmlinuz- ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-

For the first boot, let the line default=0 to test, if the new kernel boot with no trouble it will be ok to change the line to default=1 to always boot with the new kernel.

Testing a boot with the new kernel


shutdown -r now
  • Press any key on boot screen to enter the selection kernel
  • Select the new kernel configured into Grub
  • At the end of the boot, it will be possible to see that the new one booted properly
Share this post


Join the conversation
  • Richard Holloway UNITED KINGDOM - 03/03/2010 reply

    I am getting the message ‘Could not find filesystem /dev/root. ‘

    I have tried many various suggestions found online with no success, are you able to provide me with any advice on trouble shooting this?

    Daniel Kühl Lima BRAZIL - 03/03/2010 reply

    Looks like the kernel haven’t found the root partition.

    Starting looking for the root= entry into your grub.conf and make sure you have compiled the chipset driver to recognize your HD, be sure to know it and if it is SATA or SCSI or SAS to build the properly driver.

    Richard Holloway UNITED KINGDOM - 03/03/2010 reply

    I have found a solution on

    I needed to edit .config and set CONFIG_SYSFS_DEPRECATED_V2=y

    Thank you for this excellent guide.

    Daniel Kühl Lima BRAZIL - 09/03/2010 reply

    Thanks for sharing your solution!

    vishal INDIA - 23/11/2011 reply

    Thanks a lot, it took me almost 1 day to compile the custom kernel.

  • Rocky INDIA - 23/07/2010 reply


    I did same process which u mentioned here.At last when i booted my machine with new kernel, i got Kernel Panic….! error.


  • Esonz PHILIPPINES - 21/08/2010 reply

    bash: depmod command not found
    bash: mkinitrd command not found

    how to solve this one? im stocked in “Creating the initrd for the new kernel”

    Daniel Kühl Lima BRAZIL - 21/08/2010 reply

    To install mkinitrd use the command:

    yum install mkinitrd

    To install depmod:

    yum install module-init-tools

    And you are good to go :)

  • Esons PHILIPPINES - 22/08/2010 reply

    bro it indeed installed depmod but still it give me the same error :(

    Daniel Kühl Lima BRAZIL - 22/08/2010 reply

    It’s look like you do not have /sbin in your PATH variable.

    May you are using just ‘su’ to get root user or just haven’t PATH set.

    To use the su command and get the root ENV variables, use:
    su - root

    And even after that, if you got no luck, set the PATH variable:

    export PATH=/sbin:/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH

    It’s good to put it into /etc/profile file so you won’t need to set it every time.

    You can also specify the full path for mkinitrd:

    /sbin/mkinitrd -v /boot/initrd-

    The same go to ‘depmod’:

    /sbin/depmod -a

    Esons PHILIPPINES - 22/08/2010 reply

    ok depmod and mkinitrd [SOLVED]

    and hopefully this is the last and final error.
    after reboot then i chosen my

    Please bare with me a little longer daniel, Im really new in configuring kernels.

    thank you very much for the Great Help!

Join the conversation