Compiling a 'stock' kernel.

With many many thanks to Greg Kroah-Hartman for the book LINUX KERNEL IN A NUTSHELL which helped separate the wood from the trees. The book's web site is at

You can download of all the chapters in pdf format from or, if you want to update the book yourself, get the books own source code in DocBook format from

You have to love Open Source don't you? How many Microsoft books do you have whereby you can update the book itself?

Sometimes you simply have to compile a stock kernel for your distribution, just sometimes. I've had to do it to get my qdos module to compile because the installed one is too old - but I'm not allowed to change it - so this is what I did.

  • Download a stock kernel by browsing to
  • Locate the line near the top that says The latest stable version of the Linux kernel is.
  • Click on the F on the same line, just after the date. This will download a full source tree for the latest stable kernel.

This kernel will be different from the one your distro supplied for you - in my case, Suse Linux Enterprise Server which uses a because my qdos module needs a 2.6.18 or higher kernel, the installed one is no good to me!

We now need somewhere to put said kernel. I do my compilations in my home directory, so:

mkdir linux
mv linux- linux/
cd linux

Warning: If you intend installing any software from source code, you better not overwrite your /usr/src/linux directory with the source code from the stock kernen. Leave that location for the installed kernel 's source code.

Now it's time to unpack the source tree. Remember this is a stock kernel and has not been adulterated by the distro supplier.

tar --bzip2 -xvf linux-

Because this is a stock kernel, you can configure it to be like your distro in as much as the configured options and modules are concerned. It is possible that your distro has made code changes to their supplied version. Not to worry. When developing for the kernel, it's best to start with a stock kernel and work on that.

From 2.6 onwards, building a kernel is quite a simple affair and no longer frightening! First check that the extract has completed and you should find in your new linux directory a new sub-directory called linux-

linux-  linux-

So far so good. We need a file named .config now. We can build this manually if you like - and know exactly what options your distro's kernel has configured, but it's easier to copy the configuration information directly from your running kernel.

ls /proc/config.gz

That file, config.gz is a copy of the running kernel that has been created in the /proc virtual filesystem for you. Let's grab hold of it, and uncompress it.

cp /proc/config.gz ./
gunzip config.gz
config  linux-  linux-

The file needs to be in the root of our source tree, and needs to be called .config, so we copy it into the correct place next.

cp config linux-

Job done. You can now either build a stock copy of your running kernel, or make some changes. I removed a couple of built in options from my kernel that related to bluetooth as I don't have a bluetooth option on this PC. The command was:

make menuconfig

You can do this now if you like, and set the kernel up to remove things you don't need or want, or make them modules etc. When done, come back and we will compile the kernel.


Did that surprise you? A one word command to build a new kernel? Surely not! Welcome to Linux! You don't even have to be root to build a new kernel.

This will run for a wee while, depending on the speed of your system, so let it run. Unless you messed up your configuration, it should finish after it has done, and there should be no errors.

Drink lots of coffee, watch TV whatever you like. It will finish eventually, and probably just as soon as you go off to make another cup of coffee.

Once done, you have a new stock kernel. The chances are it's a different version from the one you are running, so the /lib/modules/<kernel_version> directory tree doesn't exist, so we have to create it properly.

ls /lib/modules
su root
make modules_install
  INSTALL sound/usb/snd-usb-lib.ko
  INSTALL sound/usb/usx2y/snd-usb-usx2y.ko
ls /lib/modules

And now, the moment of truth, installing our new kernel.

make install

The output from that command is as follows.

sh /media/usbdisk/norman/linux/linux- arch/x86/boot/bzImage "/boot"
Root device:    /dev/disk/by-id/scsi-SATA_WDC_WD800JD-60L_WD-WMAM9P907202-part2 (/dev/sda2) (mounted on / as ext3)
Module list:    ata_piix processor thermal fan jbd ext3 edd (xennet xenblk)
Kernel image:   /boot/vmlinuz-
Initrd image:   /boot/initrd-
Shared libs:    lib/ lib/ lib/ lib/ lib/ lib/ lib/ lib/ lib/ lib/ lib/ lib/ lib/ lib/ lib/ lib/ lib/
Driver modules: scsi_mod sd_mod dock libata ata_piix processor thermal fan edd
Filesystem modules:     jbd ext3
Including:      initramfs fsck.ext3
Bootsplash:     SuSE-NLD (800x600)
28390 blocks

That's it. Next time you reboot you will (should have) a kernel named on your list of available kernels.

If you want to immediately start using the new kernel, reboot and select the newly installed kernel from the list.

How easy was that?

  • linux/kernel/start.txt
  • Last modified: 2008/06/17 10:29
  • by norman