Official website for Linux User & Developer
FOLLOW US ON:
Mar
9

Compile your own kernel – Tutorial

by Michael Reed

We’ll take you through the steps you need to compile your own customised kernel for performance, specialised use and simply to learn how the plumbing works

Back in the mid-1990s, recompiling the kernel was something of a necessity, and it was also a good test that a user had mastered the basics of administering Linux. These days, the stock kernel that comes with most distros has much improved, removing the necessity of kernel recompilation for basic use. However, there are cases where it’s well worth becoming familiar with this area of tweaking your system. For one thing, it’s a must if you want to access the latest and greatest kernel improvements, hot off the press, so to speak. It’s also a good way of understanding how the kernel and other fundamental parts of a Linux system actually work. It can also be useful when troubleshooting: the newest kernel might bug-fix the problem you’re having. On the other hand, an older kernel might be the workaround that you need.

We’ll start you off with a simple example that begins with fetching the source archive for the kernel that you are currently running, proceeding through to configuration, compilation and installation. Following this, we’ll go through some examples that are a bit more specialist. Most of the examples are for Debian-derived distros, but we’ve deliberately kept things as neutral as possible and added some notes for how to handle things on Red Hat-based distros such as Fedora.

Compile your own kernel - Tutorial
Once the kernel is complete, you can add it to the system using the standard packaging tools

Step by Step

Step 01

Install prerequisites

Begin by fetching the tools needed to create a suitable build environment. Enter ‘sudo apt-get install fakeroot crash kexec- tools makedumpfile kernel-wedge git-core libncurses5 python libncurses5-dev kernel- package libelf-dev binutils-dev’ followed by ‘sudo apt-get build-dep linux-image’.

Step 02

Fetch the kernel (source archive)

If you need the latest kernel, use Git to fetch it (see later step), but we are going to use the standard package tools in the first example. Use ‘apt-get source linux-image-$(uname -r)’ to install the source for the currently running kernel.

Step 03

Examine the source directory

You should now have a source directory in the current directory. Move into it using the cd command. Note that there is an archived (tar.gz) copy as well. In addition, there is a diff file that contains the Ubuntu-specific additions to the standard kernel source tree.

Step 04

Generate a .config file

The (hidden) file ‘.config’, located in the source code directory, tells the compiler what to build. The configuration file for each installed kernel is stored in the /boot directory, but you can capture the configuration of the current kernel (a good starting point) by typing ‘make oldconfig’.

Step 05

Edit .config

Open up .config in a text editor. Note that there are thousands of options, and this approach is best if you know exactly what settings you would like to edit. It’s a good idea to search for ‘CONFIG_LOCALVERSION’ to add a small identifying string for your custom kernel.

Step 06

Turn off debugging

One way to speed things up and produce smaller files is to turn off debugging.
It’s a specialist feature and mainly used by developers. You can use xconfig for this. Set ‘CONFIG_DEBUG_INFO:’ to ‘n’.

Step 07

Invoke xconfig

Type ‘make xconfig’ to launch the GUI config file editor. It’s a good way to gain an overview, and it offers information for most of the options. Run ‘sudo apt-get install libqt4-core libqt4-dev libqt4-gui’ if it complains about not being able to find Qt.

Step 08

Prepare Debian scripts

Some required scripts lose their execution privileges due to how apt-get works. Rectify this by typing ‘chmod -R u+x debian/ scripts/*’ and then ‘chmod u+x debian/rules’.

Step 09

Recreating the Source Tree

If you mess things up and want to start from scratch, delete the source directory. Backup your .config file first, if needed. Then run “tar xzf” on the source archive to unpack it. Move into the directory and type “zcat ../[name of diff archive] | patch -p1” to add the Ubuntu patches into the source tree.

Step 10

Compile the kernel

Tell the build environment how many cores you want to use with ‘export CONCURRENCY_LEVEL=[number of cores]’. Begin the build process with ‘fakeroot make- kpkg –initrd –append-to-version=-luad kernel- image kernel-headers’, which takes about an hour on a Core Duo 2.7GHz system.

Step 11

Install kernel

Warning: This is stage where you actually make some changes to your machine. Using our method means that we can install our custom kernel just like any other package. Type ‘sudo dpkg -i linux-image[version].deb’ and then, ‘sudo dpkg -i linux-headers-[version].deb’.

Pages: 1 2
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • gg
    • Pingback: Links 14/3/2013: Atheros Publishes Open-Source Wi-Fi Firmware, W3C Behind DRM | Techrights

    • dan

      your make command does not work.
      you should make your command line instructions more easily copied so they can be pasted.

    • Pingback: Compile your own kernel | 0ddn1x: tricks with *nix

    • supriady

      Packages should be installed : gcc, libnucses5-dev(ubuntu) and ncurses-devel(Fedora/CentOS), perl (kernel 3.0) , make, xterm (run menuconfig).You should copy existing config file from your own linux system to /usr/src/linux-3.xx/.config and run make menuconfig. So you can customize your own kernel.I don’t think it can edit all parameter on .config file. The result can cause kernel PANIC.

    • http://www.twoje-stron hydrofory

      It’s a shame you don’t have a donate button! I’d most certainly donate to this outstanding blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to new updates and will share this website with my Facebook group. Chat soon! hydrofory http://www.twoje-strony.pl/al-ko-kober-sp-z-o-o-kosa-spalinowa-s-560.html

    • http://sklep.poradnia.pl/zwichniecie-barku-jak-sobie-z-nim-poradzic.html cisnieniomierze

      If some one wishes expert view regarding blogging and
      site-building then i advise him/her to pay a quick visit this
      website, Keep up the fastidious job.

    • http://www.cacash.com payday Loans

      Thank you for the auspicious writeup. It in fact was a amusement account it.
      Look advanced to more added agreeable from you! However, how could we communicate?

    • Pingback: install kernel