January 22, 2019

Building Your Own Linux Kernel, part 1

Configure and Build

  • September 24, 2009
  • By Akkana Peck

The kernel folks at your favorite distro -- Ubuntu, Redhat, Debian, SuSE or whatever -- do a great job. Somehow, they come up with a kernel that works with just about machine, from 10-year-old klunkers to brand new 8-core speedsters. When you think about it, it's amazing your machine boots at all!

But writing a kernel for everyone means making compromises. You can get better performance, and sometimes bug fixes, by building your own kernel.

That's one reason to build your own kernel. But here are a few others:

  • You can try out brand new kernel features, long before your distro includes them
  • You can experiment with performance options, like different schedulers
  • Extra "geek cred". Come on, admit it, you care!

Today I'll cover how to build and install a kernel. Upcoming articles will dive into the details of how to customize your kernel and put it on a diet.

Which kernel?

Kernel source comes in two varieties. First, you can get the source package from your distro. But building a Debian or Ubuntu kernel package is different from building a Redhat or Fedora package. You can find howtos online, but in this article I'll be talking about the second option: a mainline kernel.

Kernel.org (Figure 1) is the official place to download the latest kernel releases. Start with the full source for the latest stable release (circled): it'll have a filename like linux-2.6.31.tar.bz2. Download it, unpack it, and cd into the directory it creates:

tar xvf linux-2.6.31.tar.bz2
cd linux-2.6.31
figure 1
figure 1

You can unpack it anywhere. You may hear about people building kernels in /usr/src/linux, but there's no advantage to it; you can build a kernel just as well under your home directory.

An initial configuration file

The first step in kernel building is to find an initial configuration that works, stored as .config inside the kernel source directory. You can configure kernels from scratch, but I don't recommend it. It's all to easy to make a tiny mistake early in the process and find out 45 minutes later that something is very wrong and you're not being shown the right options.

Where can you get an initial configuration? The easiest place to look is your distro kernel. Most modern distros, happily, give you a copy of their configuration file in /boot. Look for a file with "config" in the name, something like /boot/config-2.6.28-15-generic. Take the one with the latest version number you can find, and copy it to .config inside your new kernel source directory.

If you can't find a config file in /boot, you can try asking the currently running kernel. Look for a file named /proc/config.gz. If it exists, you can get the configuration this way:

zcat /proc/config.gz >.config
Unfortunately, many distros don't enable this option in their kernels. If all else fails, try downloading your distro's kernel source package, or find someone who has a machine similar to yours and ask for their kernel config file.

Most Popular LinuxPlanet Stories