HOW TO COMPILE THE LINUX KERNEL

Copyright (C) 2004 Daniel Brodzik
This document is free software; you may distribute and/or modify it under the terms of the GNU Free Documentation License; either version 1.2 of the license or, at your option, any later version, with no Front Cover Texts, no Back Cover Texts, and no Invariant Sections.

I would like to thank Jesse Goerz for instructions on compiling the kernel the Debian Way and Emma Jane Hogbin for her instructions on compiling both the Debian Way and the 'Normal' Way in her ACPI HOWTO. Now that I have both Debian GNU/Linux and Red Hat Fedora Core Linux, I have verified that both ways work.

INTRODUCTION
The Linux kernel, as all Linux users should know, is under the GNU General Public License. That license guarantees that the source code to Linux is open to the public. Thus, the kernel source code is included with most major distributions.

When a distribution is put together, the distributor has to pick and choose which parts of the kernel to compile. Since Linus distributes the kernel as source code, the distributor has to try to compile a kernel generic enough to be 'universal' (i.e., any machine of that architecture capable of running Linux can boot the kernel without trouble). Most distributors make what must be compiled into the kernel (i.e., stuff that must be loaded immediately, like a driver for the root filesystem) built-in, and compile other, non-urgent drivers as kernel modules (i.e., objects that can be loaded and unloaded from a running kernel). Linux can also be compiled to run on a certain generation of processors or later, but not on earlier versions of the processor (i.e., a kernel compiled for a Pentium II will run on a Pentium II and above but not on a 386, 486, or Pentium Classic). To avoid incompatibilities, most distributors compile a kernel for the earliest supported generation of the processor (on the x86 architecture, this would be a 386).

Even though most distributors try their best to make a kernel that fits everybody's needs, there are times when people need/want to compile a custom kernel. For example, the kernel that I was using under Debian had a VGA framebuffer driver compiled into the kernel (meaning that I had a console in VGA 640x480x4 bit graphics mode), but I wanted to make it a module so that I could turn it on and off as I wanted. For another example, you may find you need to use a device driver that is in the Linux source code, but it wasn't compiled and included with the kernel that came with your distribution. A third case is if you need the full, configured source code to compile and install a driver (one example of this being ALSA, the Advanced Linux Sound Architecture).

Thanks to Emma Jane Hogbin's ACPI HOWTO, I now have a section on compiling the Linux kernel on a non-Debian system. Please be aware that I haven't had a need to compile the kernel the non-Debian way, but I do trust her directions.

Here's what you'll need to compile a kernel in Debian. These are the names of packages you'll need to compile the kernel image. Install them by running dselect as root.

In Red Hat Fedora Core, you'll need to download the kernel source code from http://www.kernelhq.org and install the packages in the section that has Linus' picture next to it (Kernel Development) as well as the Development tools.

In all distributions, you will also need to have access to your root account during the process. Now, on with the steps!


STEPS FOR CREATING A CUSTOM LINUX KERNEL UNDER DEBIAN GNU/LINUX USING FRESH SOURCE CODE

If you're recompiling a kernel using your old source code, skip these steps and go to the next section. If you're using fresh source code, follow these steps.


  1. Get to a terminal, either under X or in a full-screen console. Under KDE and GNOME, this can be accomplished by clicking a button on the panel that looks like a monitor. To go full-screen, press CTRL-ALT-F1.

  2. Log in as root. If you are using a terminal window, type 'su'. On a full screen, just log in as root.

  3. Type 'adduser username src', replacing username with the name of your normal user account. (You do have one and use it, don't you?) Since, under Linux, we want to avoid doing more than we have to as root, we try to make it so that we can do more as an unprivileged user. This command adds your normal user account to the src group so you can access /usr/src as a normal user. The root account has no security!

  4. Press CTRL-D to exit/log out. If you are prompted to log in, log in as a normal user.

  5. Type 'cd /usr/src' to go to the source code directory.

  6. Type 'ls'. If you see something like 'kernel-source-2.4.18.tar.bz2' listed, you're okay. If not, check to see that all the packages above are installed.

  7. If you've compiled a kernel before, type 'rm linux' to remove the old 'linux' link.

  8. Type 'tar -jxvf kernel-source-x.xx.tar.bz2', replacing 'x.xx' with the version number. Alternatively, you can type part of the filename and press TAB to auto-complete it. This command unpacks the kernel source code. If you are using a 486 or 386, this will take a long time. If you are using a Pentium or newer, this will take a few seconds.

  9. Type 'ln -s kernel-source-x.xx linux', following the tips for the name in the previous step. This creates a link to the kernel source so that any device drivers you install from source code can find the kernel source.

  10. Type 'cd linux'.

  11. Type 'make-kpkg clean' to clean out the kernel source tree.

  12. Now it's time to configure the kernel. What you do now depends on whether you want the Hard Way, the Text-Menu Way, or the Point-and-Click Way. For the Hard Way, which I do NOT recommend, type 'make config'. This method asks you the questions in order and does not let you go back. For the Text-Menu Way, which is the way I recommend, type 'make menuconfig'. This method is very easy to use. For the Point-and-Click Way, you must be in a console window, and type 'make xconfig'.

  13. Select what you want to compile into the kernel. If you compile something as a module, you can load and unload it as you please using an easy-to-use, menu-based utility called 'modconf'. However, there are some things you MUST compile directly into the kernel, like the driver for your root filesystem and the drivers for anything else that must be loaded immediately upon booting. Use your judgement. If you're unsure about something, there is a help function. Under menuconfig, you can press SHIFT-? to get help. Under xconfig, there should be a Help button you can click. The help is very good.

  14. When you're done going through all the options, exit the configuration utility and, if asked to save the configuration, select Yes.

  15. When you see a command prompt again, type 'su' to become root.

  16. When you're root, type

    make-kpkg --revision=custom.1.0 kernel_image

    Type this literally! That is, do not substitute anything for 'kernel_image'. If this is not your first time compiling a custom kernel, you may have to increase the revision number. I've compiled my kernel several times without having to increase the version number, but the document that taught me how to do this said I would have to. Anyway, this command compiles the kernel and makes a nice Debian package of it. This will take a while, so relax. The kernel package build script runs through all the configuration questions first. If it gets stuck while it's doing that, just hit ENTER and it will continue without an error.

  17. When it finishes, type 'cd /lib/modules', type 'ls', and look for a directory for the kernel version you just compiled. If you see a directory for that version (without any suffix like 2.4.18-bf24, just a number like 2.4.18), rename it with something like 'mv 2.4.18 2.4.18.backup'. This will get any old modules out of the way so that you can install the new package you just built.

  18. Type 'cd /usr/src' to get back to the source directory.

  19. Type 'ls *.deb' to see the name of the kernel package you made.

  20. Type 'dpkg -i package_name.deb' substituting the name of the package you compiled for package_name.deb.

  21. Follow the prompts, and, when you're done, type 'reboot'.

  22. Log in as root to a console session, and type 'modconf' to configure your modules.


STEPS FOR RECOMPILING A KERNEL USING THE OLD SOURCE CODE IN DEBIAN GNU/LINUX


  1. Get to a terminal, either under X or in a full-screen console. Under KDE and GNOME, this can be accomplished by clicking a button on the panel that looks like a monitor. To go full-screen, press CTRL-ALT-F1.

  2. Log in as a normal user if you have to log in.

  3. Type 'cd /usr/src' to go to the source code directory.

  4. Type 'cd linux'.

  5. Type 'make-kpkg clean' to clean out the kernel source tree.

  6. Now it's time to configure the kernel. What you do now depends on whether you want the Hard Way, the Text-Menu Way, or the Point-and-Click Way. For the Hard Way, which I do NOT recommend, type 'make config'. This method asks you the questions in order and does not let you go back. For the Text-Menu Way, which is the way I recommend, type 'make menuconfig'. This method is very easy to use. For the Point-and-Click Way, you must be in a console window, and type 'make xconfig'.

  7. Select what you want to compile into the kernel. If you compile something as a module, you can load and unload it as you please using an easy-to-use, menu-based utility called 'modconf'. However, there are some things you MUST compile directly into the kernel, like the driver for your root filesystem and the drivers for anything else that must be loaded immediately upon booting. Use your judgement. If you're unsure about something, there is a help function. Under menuconfig, you can press SHIFT-? to get help. Under xconfig, there should be a Help button you can click. The help is very good.

  8. When you're done, exit the configuration utility and, if asked to save the configuration, select Yes.

  9. When you see a command prompt again, type 'su' to become root.

  10. When you're root, type

    make-kpkg --revision=custom.1.0 kernel_image

    Type this literally! That is, do not substitute anything for 'kernel_image'. If this is not your first time compiling a custom kernel, you may have to increase the revision number. I've compiled my kernel several times without having to increase the version number, but the document that taught me how to do this said I would have to. Anyway, this command compiles the kernel and makes a nice Debian package of it. This will take a while, so relax. The kernel package build script runs through all the configuration questions first. If it gets stuck while it's doing that, just hit ENTER and it will continue without an error.

  11. When it finishes, type 'cd /lib/modules', type 'ls', and look for a directory for the kernel version you just compiled. If you see a directory for that version (without any suffix like 2.4.18-bf24, just a number like 2.4.18), rename it with something like 'mv 2.4.18 2.4.18.backup'. This will get any old modules out of the way so that you can install the new package you just built.

  12. Type 'cd /usr/src' to get back to the source directory.

  13. Type 'ls *.deb' to see the name of the kernel package you made.

  14. Type 'dpkg -i package_name.deb' substituting the name of the package you compiled for package_name.deb.

  15. Follow the prompts, and, when you're done, type 'reboot'.

  16. Log in as root to a console session, and type 'modconf' to configure your modules.


STEPS FOR CREATING A CUSTOM LINUX KERNEL ON A NON-DEBIAN SYSTEM USING FRESH SOURCE CODE

If you're recompiling a kernel using your old source code, skip these steps and go to the next section. If you're using fresh source code, follow these steps.


  1. Get to a terminal, either under X or in a full-screen console. Under KDE and GNOME, this can (sometimes) be accomplished by clicking a button on the panel that looks like a monitor. Under Red Hat, you need to go to the menu (lower left corner) and click on 'Terminal' under 'System Tools'. To go full-screen, press CTRL-ALT-F1.

  2. Log in as root. If you are using a terminal window, type 'su'. On a full screen, just log in as root.

  3. If you downloaded the source code from the Internet and did not go through your package manager (if any), copy or move your source code to /usr/src.

  4. Type 'cd /usr/src' to go to the source code directory.

  5. Type 'ls'. If you see something like 'kernel-source-2.4.18.tar.bz2' or 'linux-2.4.26.tar.bz2' listed, you're okay. If not, check to see that all the packages above are installed. If you used an RPM, look for a directory under /usr/src/RPM.

  6. If you've compiled a kernel before, type 'rm linux' to remove the old 'linux' link.

  7. Type 'tar -jxvf kernel-source-x.xx.tar.bz2' or 'tar -jxvf linux-x.xx.tar.bz2', replacing 'x.xx' with the version number. Alternatively, you can type part of the filename and press TAB to auto-complete it. This command unpacks the kernel source code. If you are using a 486 or 386, this will take a long time. If you are using a Pentium or newer, this will take a few seconds. If you used an RPM, ignore this step.

  8. Type 'ln -s kernel-source-x.xx linux' or 'ln -s linux-x.xx linux', following the tips for the name in the previous step. This creates a link to the kernel source so that any device drivers you install from source code can find the kernel source. If you used an RPM, make sure you're in /usr/src and do an 'ln -s RPM/linux-x.xx linux' or 'ln -s RPM/kernel-source-x.xx linux' (depending on what you saw when you checked the RPM directory.

  9. Type 'cd linux'.

  10. Now it's time to configure the kernel. What you do now depends on whether you want the Hard Way, the Text-Menu Way, or the Point-and-Click Way. For the Hard Way, which I do NOT recommend, type 'make config'. This method asks you the questions in order and does not let you go back. For the Text-Menu Way, which is the way I recommend, type 'make menuconfig'. This method is very easy to use. For the Point-and-Click Way, you must be in a console window, and type 'make xconfig'.

  11. Select what you want to compile into the kernel. If you compile something as a module, you can load and unload it as you please using an easy-to-use, menu-based utility called 'modconf'. However, there are some things you MUST compile directly into the kernel, like the driver for your root filesystem and the drivers for anything else that must be loaded immediately upon booting. Use your judgement. If you're unsure about something, there is a help function. Under menuconfig, you can press SHIFT-? to get help. Under xconfig, there should be a Help button you can click. The help is very good.

  12. When you're done going through all the options, exit the configuration utility and, if asked to save the configuration, select Yes.

  13. Type 'cd /lib/modules', type 'ls', and look for a directory for the kernel version you just compiled. If you see a directory for that version (without any suffix like 2.4.18-bf24, just a number like 2.4.18), back it up with something like 'mv 2.4.18 2.4.18.backup'. This will get any old modules out of the way so that you can install the new kernel you just built.

  14. Type 'cd /usr/src/linux'

  15. Type these commands in this order: 'make dep', 'make clean', 'make bzImage', 'make modules'

  16. Type the following to install the new kernel:

  1. When it finishes, type 'cd /lib/modules', type 'ls', and look for a directory for the kernel version you just compiled. If you see a directory for that version (without any suffix like 2.4.18-bf24, just a number like 2.4.18), rename it with something like 'mv 2.4.18 2.4.18.backup'. This will get any old modules out of the way so that you can install the new package you just built.

  2. Type 'cd /usr/src' to get back to the source directory.

  3. Type 'ls *.deb' to see the name of the kernel package you made.

  4. Type 'dpkg -i package_name.deb' substituting the name of the package you compiled for package_name.deb.

  5. Follow the prompts, and, when you're done, type 'reboot'.

  6. Log in as root to a console session, and type 'modconf' to configure your modules. This might not work on a non-Debian system.


STEPS FOR RECOMPILING A KERNEL USING THE OLD SOURCE CODE ON A NON-DEBIAN SYSTEM


  1. Get to a terminal, either under X or in a full-screen console. Under KDE and GNOME, this can be accomplished by clicking a button on the panel that looks like a monitor. To go full-screen, press CTRL-ALT-F1.

  2. Log in as root.

  3. Type 'cd /usr/src' to go to the source code directory.

  4. Type 'cd linux'.

  5. Type 'make-kpkg clean' to clean out the kernel source tree.

  6. Now it's time to configure the kernel. What you do now depends on whether you want the Hard Way, the Text-Menu Way, or the Point-and-Click Way. For the Hard Way, which I do NOT recommend, type 'make config'. This method asks you the questions in order and does not let you go back. For the Text-Menu Way, which is the way I recommend, type 'make menuconfig'. This method is very easy to use. For the Point-and-Click Way, you must be in a console window, and type 'make xconfig'.

  7. Select what you want to compile into the kernel. If you compile something as a module, you can load and unload it as you please using an easy-to-use, menu-based utility called 'modconf'. However, there are some things you MUST compile directly into the kernel, like the driver for your root filesystem and the drivers for anything else that must be loaded immediately upon booting. Use your judgement. If you're unsure about something, there is a help function. Under menuconfig, you can press SHIFT-? to get help. Under xconfig, there should be a Help button you can click. The help is very good.

  8. When you're done, exit the configuration utility and, if asked to save the configuration, select Yes.

  9. Type these commands in this order: 'make dep', 'make clean', 'make bzImage', 'make modules'

  10. When it finishes, type 'cd /lib/modules', type 'ls', and look for a directory for the kernel version you just compiled. If you see a directory for that version (without any suffix like 2.4.18-bf24, just a number like 2.4.18), rename it with something like 'mv 2.4.18 2.4.18.backup'. This will get any old modules out of the way so that you can install the new package you just built.

  11. Type the following to install the new kernel:

  1. When you're done, type 'reboot'.

  2. Log in as root to a console session, and type 'modconf' to configure your modules.

1