The kernel is the heart of the operating system. Compiling your own custom version of the kernel can increase the speed, and allows you to customize it to your liking.

Note that these directions were made for a 32bit x86 system. If you have a different system, just put your system arch where it says i386

Warning

This page is outdated and things have likely changed. Kept here for historical reasons.

Setup and Configure

First, download the kernel from kernel.org and extract it to a directory. In this example we will use /usr/src/linx-(number) as the directory, where (number) is the kernel version, be that 3.12, 4.5, or 9.8. (Yes, that last one doesn’t exists as of now, but one day…)

cd /usr/src
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-(number).tar.gz
tar xjf linux-(number).tar.bz2
cd /usr/src/linux-(number)

You may need to modify your /etc/modprobe.conf file if you plan to include any of the modules listed there directly into the kernel.

And apply patches if you desire.

bzip2 -dc /usr/src/patch.bz2 | patch -p1 --dry-run
bzip2 -dc /usr/src/patch.bz2 | patch -p1

gunzip -c /usr/src/patch.gz | patch -p1 --dry-run
gunzip -c /usr/src/patch.gz | patch -p1

Now clean the directory, and copy your current config file. This make things easier.

make clean && make mrproper
cp /boot/config-`uname -r` ./.config

If you want, edit your Makefile and change the last part of the kernel name. This is where you get to pick exactly what you want.

Then run the config editor. You have these options:

EditorRequiresDescription
make xconfigqt-develSimple GUI config with search.
make menuconfigNcursesNcurses terminal config. Little hard to navigate
make gconfiggtk2-develGUI is nice, but no search

Compile and Install

RPM Style

To compile a kernel rpm package (may be a few hours):

make rpm

It will place the kernel package into the /usr/src/redhat/RPMS/i386/ folder. Install it as a regular user.

rpm -ivh /usr/src/redhat/RPMS/i386/kernel-(number).rpm

Compile an initrd package so the kernel can load on boot.

mkinitrd /boot/initrd-2.6.18.3-default.img 2.6.18.3-default

Traditional Style

You can try make && make install, or the following.

Compile the kernel:

make && make modules_install

Copy the kernel, system-map, and config to /boot

cp arch/i386/image/bzimage /boot/kernel-(number)
cp System-map /boot/Ssytem-map-(kernel)
cp .config /boot/config-(number)

If you need to you can make a initrd image

mkinitrd /boot/initrd-2.6.18.3-default.img 2.6.18.3-default

Final Config

If you did not run make install, you use grub, or you used the rpm method, you will need to edit your grub file so the machine will boot. Just add an entry like the following above the others.

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

Then reboot and you should be ready to go.

Sources