How to Compile a New Kernel

Posted on November 3rd, 2009


Introduction

The Linux kernel is a complex program providing the underlying services to the rest of a Linux distribution. It is easy to add new features or improvements to the linux kernel because unlike commercial operating systems like Windows or MacOS, the source code for the linux kernel is freely available. It is common practice with a Linux based operating system to recompile the kernel from source and much effort has been put in to make this a relatively user-friendly experience.

Why Compile a New Kernel?

There are three reasons for a recompile. First, you may have some hardware that is so new that there’s no kernel module for it in on your distribution CD. Second, you may have come across some kind of bug which is fixed in a revision of the operating system. Finally, you may have some new software which requires a newer version of the operating system.

Where to Get the Kernel Sources

The most recent releases of the kernel sources are available for download on ftp.kernel.org. This site is mirrored worldwide so there is probably a fast copy of it somewhere near you. Locate the file for the latest version of the operating system and download it to /usr/src. Then issue the tar zxf command to unpack it.

Alternatively, if you already have the kernel sources and only need to upgrade one version number (from 2.2.1 to 2.2.2 for instance) then download the patch file to /usr/src and then use the command:

gzip -dc patch-2.2.2.gz |patch -p0

to upgrade your old kernel sources. Patch files are much smaller to download than the whole thing.

Configuring For a Build

The first step to configuring for a linux kernel build is to change the directory to /usr/src/Linux and issue the following command:

make menuconfig

This will build a few programs and then a window will pop up. The window menu lets you alter many aspects of kernel configuration.

After you have made any necessary changes, save the configuration and follow these instructions–

make dep; make clean

The first of these commands builds the tree of interdependencies in the kernel sources. These dependencies may have been affected by the options you have chosen in the configure step. The make clean purges any now-unwanted files left from previous builds of the kernel.

Then you are ready to go!

Issue this command:

make zImage

The kernel has a lot of source code as you may have noticed when downloading it. When this is complete issue the command:

make modules

Installing a New Kernel

The last step is installing your new kernel. On an Intel-based system the kernel is installed to the right place in /boot with the command

cp /usr/Linux/src/arch/i386/boot/zImage /boot/newkernel

then

make modules_install

This will install the modules in /lib/modules. Next, edit /etc/lilo.conf to add this section:

image = /boot/newkernel

label = new

read-only

At the next reboot, select the kernel ‘new’ in lilo, and it will load the new kernel. If it works fine, move it to the first position in the lilo.conf so it will boot every time by default.

Conclusion

Compiling the kernel is a relatively simple operation- if you have done it before! At first it can seem daunting. If you have any questions about how to install a kernel, feel free to leave them here.