Building a new kernel

Building a new kernel lets you incorporate drivers and features which are not in the kernel which you installed originally. In particular, the standard kernels do not include IP Forwarding, so if you want to set up a gateway with Linux, you must build and install a new kernel.

Fetch and unpack the kernel source code

The kernel source comes in a compressed 'tar' archive - its name ends in .tar.gz or .tgz. This is the Unix equivalent of a 'pkzip' archive. You will need to fetch this file from
or one of the many mirror sites. Pick the latest version, e.g. linux-2.0.29.tar.gz. Note that there is also a v2.1 kernel, but this is a development or "experimental" kernel. In general, Linux kernel versions x.y where y is even are "stable" kernels which receive only bug fixes; where y is odd they are "experimental" kernels under development.

This file is BIG - around 6MB. To save having to download all of this, there are 'patch' files which contain only the changes between each version. So if you have, say, version 2.0.27 already (e.g. on CD-ROM) you can upgrade it to 2.0.29 by applying patch-2.0.28.gz and then patch-2.0.29.gz

Here are the commands to unpack the kernel source (replace '/tmp' with whatever directory the files are found in); this example also shows patch files being used.

cd /usr/src
tar -xvzf /tmp/linux-2.0.27.tar.gz
zcat /tmp/patch-2.0.28.gz | patch -p0
zcat /tmp/patch-2.0.29.gz | patch -p0

Configure and build the kernel

You can find detailled instructions on building kernels in file /usr/src/linux/README but the basic steps are as follows:
cd /usr/src/linux
make menuconfig
At this point you will be presented with a hierarchy of menus to select kernel features. Note that if you want to compile a kernel with IP Masquerading, you must go into "Code maturity level options" and enable "Prompt for development and/or incomplete code/drivers", otherwise the IP Masquerading option (under "Networking Options") will be hidden.

Many drivers have < > next to them. This means that they can either be compiled into the kernel, or built as a separate loadable module which you can load in at run time. Those options which have [ ] next to them can only be built into the kernel itself.

When you have finished, you will be asked if you wish to save your kernel configuration. Say YES.

make dep
make zImage
make modules
make modules_install
cp /usr/src/linux/arch/i386/boot/zImage /boot/kernelname
'make zImage' builds the main kernel file itself, as a compressed image file. When the compile has finished you need to copy this into your /boot directory and give it a name, perhaps zImage-2.0.29 which lets you identify the version. The modules are installed under /lib/modules/2.0.x by make modules_install

If you are compiling a kernel with the same version number as an existing one (e.g. 2.0.27) then you may want to keep a copy of the old modules to use with the old kernel, before doing 'make modules_install'

cd /lib/modules
mv 2.0.27 2.0.27.orig

Reinstall LILO

Finally, you need to reinstall LILO to use the new kernel; you should also keep the old kernel available so it can still be selected when the machine boots, in case there's a problem with the new one. To do this, you need to edit /etc/lilo.conf, leave the top part alone, and add another "Linux bootable partition" section. For example, if the end of /etc/lilo.conf looked like this before:
image = /boot/vmlinuz
  label = linux
  root = /dev/hda2
then you can add a new section so it looks like this:
image = /boot/zImage-2.0.29
  label = linux
  root = /dev/hda2
image = /boot/vmlinuz
  label = linux-old
  root = /dev/hda2
Note that the new kernel is listed first (so it is the default, if your system boots directly into Linux), and that we have changed the label for the old kernel to linux-old to differentiate it. In this way, you can have many different kernels on your hard drive and switch between them at boot time.

After editing /etc/lilo.conf, type lilo and the installation should go ahead; your default bootup option will be listed with a '*', e.g.

Added windows *
Added linux
Added linux-old
You will get an error message if something is not right in your lilo.conf file. Just correct it and rerun lilo.

Red Hat note: If your system loads kernel modules from an 'initial RAM disk' (e.g. for a SCSI card) you will need to built a new ram disk image like this:

mkinitrd /boot/initrd-2.0.29 2.0.29
and reference this in lilo.conf (e.g. "initrd=/boot/initrd-2.0.29")

However, the purpose of this is so that the kernel can load modules for drivers which are needed to continue with system bootup - for example, if you have a SCSI hard drive, the kernel needs the appropriate SCSI driver before it can mount the root filesystem. If you are building your own kernel, you know which SCSI driver you want, so you may as well compile this directly into the kernel, in which case you don't need an initrd at all. (initrd is there so that a single kernel supplied by Red Hat will boot on any type of hardware)

PCMCIA users may have to recompile PCMCIA modules. See your distribution documentation.

Last updated 19 Mar 1997