Back in the old days, the compiling of a Linux kernel was often a necessity. If you wanted to get a particular piece of hardware working or if you wanted to prune some of the "fat" from the kernel you had to compile a kernel specific to your machine. It was never an easy process (at least until you got the hang of it) and always took a long period of time.
So why bother with it now? When machines are large and fast enough to not have to worry about such issues as extra kernel modules or space? There are still plenty of reasons for wanting to compile your own kernel. One of those reasons is the experience. Nothing says "Linux" like compiling ones own kernel. But beyond that - gaining as much performance out of your server or eking out a bit more security are two fine examples as to why one should learn how to compile their own kernel.
In this article, I am going to show you how to do just that - compile your own Linux kernel.
A word of warning
Before you attempt this task, it's best that you read through this document in its entirety, instead of just marching through, step by step. You will also want to make sure you keep a working kernel on your system. That way, if the kernel you build doesn't work, you will have one to fall back on.
Also, my intention with this is to be as generic as possible. There might be tools or tasks that are specific to your distribution. If you find something is lacking for your particular distribution, or a particular step isn't working for your distribution, please not it in the comments so it can be resolved.
What you will need
You won't need much more than the latest kernel source (download the latest from kernel.org), you will need to make sure you have gcc installed, and you will need a lot of patience. This process can take a lot of time...so make sure you have plenty of time blocked off.
If you do not have gcc installed you can do so with a command similar to either of these (depending upon your distribution):
sudo apt-get install gcc
yum install gcc
NOTE: The latter command will require you to su to root first.
Much of the kernel building process will require root access.
The compilation process
Now it's time to do the work. I will list out the steps for compilation:
Unpack the source code into /usr/src/ with the command
tar -xjvf linux-2.6.XXX.tar.bz2 -C /usr/src. Where XXX is the release number.
Move into the newly created /usr/src/2.6.XXX directory with the command
mv /usr/src/linux-2.6.XXX (Where XXX is the release number).
Issue the command
At this ncurses based you will need to go through the entire kernel and remove (or add) the modules that you want. This can take some time, so go through this step very carefully.
Once you have finished selecting all modules exit out of that tool and issue the command make. This will compile the kernel.
After the initial make is complete issue the command make modules to compile the modules for your kernel.
Now it's time to install the kernel modules with the command make modules_install.
Now, install the kernel with the command make install.
The above last command will install the following files into /boot:
Where XXX is the release number.
Now you have to create the initrd file with the commands:
cd /boot/ Where XXX is the release number.
mkinitrd -o initrd.img-2.6.XXX 2.6.XXX
You now have to update Grup to include the new kernel. This will depend upon the distribution you are using. For this we will tackle in a separate article.
You have just compiled your first Linux kernel. You may not even have a need to do this, but it's always good to have an understanding of what old school Linux users had to go through. To be honest, I haven't had to compile my own kernel for quite a while. But knowing how to do is allows you to have complete control over your Linux system.
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.