Recompiling Your Linux Kernel

This is a brief guide on how to recompile your Linux kernel, based on Ubuntu.

Please note, this post only deals with recompiling your kernel (especially for first-timers) with no configuration changes and no upgrades to a different version. This is not aimed at more advanced users, rather, this is only a guide for people who are planning to give their first shot at recompiling the kernel.

To get the current kernel version on your Ubuntu, type the command:

uname -r

Recompiling the kernel will require some features to be installed (if you don’t have them already), so run:

sudo apt-get install build-essential

For making configuration changes with menuconfig, Ncurses needs to be installed, but it is not required in this particular guide.

Now, login as root user by typing:

sudo su (for Ubuntu)

The following commands for recompilation must be executed as root.

Change your current working directory to /usr/src:

cd /usr/src

Before proceeding, see the list of folders that are already there in /usr/src by running ls. This is because later, a new folder will get added to this directory, and you must be able to identify it.

Now, download the source code for your current version by executing this command (Note: This will download about 125 MB of data over the Internet):

apt-get source linux-image-$(uname -r)

If you are recompiling the kernel for the first time, you now need to run this command (downloads about 75 MB over the Internet):

apt-get build-dep linux-image-$(uname -r)

Now, you will see a folder in /usr/src with the source code you just downloaded. Run ls and identify the folder that has been newly created. You will have to cd to that directory.

cd linux-3.13.0 (EXAMPLE – change this folder name to the one you have downloaded)

To recompile the kernel with no changes, the old configuration must be reused. Run:

cp -vi /boot/config-$(uname -r) .config (NOTE: my recompilation worked WITHOUT this step. The next command created the .config file for me. Try running make oldconfig and if that doesn’t work, come back to this step.)

Now run this command:

make oldconfig

This may or may not prompt you a few times to give a response such as Y/n or N/m, or something similar. If you are prompted, press ? to see what that current feature does and enable or disable it. If you are unsure of what to do, try enabling it. (What worked for me was to press ‘y’ wherever possible, and if ‘y’ was not an option, then ‘m’. As of the time I am writing this post, I am unsure what each means, but it was a method that worked for me.)

Now, the actual recompiling starts.

The three commands that must be executed in succession are:

make

make modules_install

make install

make might take 1-2 hours depending on your computer. To avoid waiting till it is over to type the next command, type this instead of the above three:

make && make modules_install && make install

If successful, no errors will be thrown.

Your newly compiled kernel is ready, but you must be able to boot into it to use it. So add the entry to the bootloader using this command:

update-initramfs -c -k <insert your new kernel name here>

(Kernel name: see it in /boot, as a series of numbers following vmlinuz-. Example: vmlinuz-3.13.0 would give the name as 3.13.0).

Now update grub:

update-grub

And grub 2 if you have it on your machine:

update-grub2

If you are having trouble modifying grub to be able to boot into the new kernel as well as the old one based on your choice, install the grub customizer.

Your recompilation is done and an alternate kernel is ready.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s