Saturday, February 4, 2017

Compiling Linux Kernel (on Ubuntu)

This guide may not exactly be relevant to this blog, but as an exercise in getting familiar with Linux, I'll post it anyways. Here are a few disclaimers-

  1. Don't follow this guide for compiling linux kernel, there are much better guides out there for that purpose (this is the one I followed). The guide exists to help you learn some new stuff which you didn't know before, and to improve your understanding of Linux a bit.
  2. My knowledge of Linux and operating systems, in general, is somewhat limited, and hence, some things might be wrong (or at least not perfectly correct).
  3. The main reason for writing this tutorial is because I had to submit a document showing what I did. It's not exactly related to hacking. It just gives you some insight into linux (which I perceive is helpful).
  4. Do everything on a virtual machine, and be prepared for the eventuality that you'll break your installation completely.

Linux Kernel

Running uname -r on your machine would show you what kernel version you're using. uname -a would give you some more details regarding that. 

Every once in a while, a new stable kernel release is made available on At the time of writing this, the release was 4.9.8. At the same time, there is also the latest release candidate kernel, which is not of our interest, as it's bleeding edge (latest features are available in the kernel, but there could be bugs and compatibility issues), and hence not stable enough for our use. 

I download the tar ball for the latest kernel (a compressed archive of ~100MB size, which becomes ~600 MB upon extraction). What we get upon extraction is the source files of your linux kernel. We need to compile this to get an object file which will run our OS. To get a feel for what this means, I have a little exercise for you-

Small (and optional) exercise

We will do the following-
  1. Make a folder, and move to that folder
  2. Write a small c++ hello world program
  3. Compile it, using make
  4. Run the compiled object file.
On the terminal, run the following-

Step 1:
mkdir testing
cd testing
Step 2:
cat > code.cpp
Paste this into the  terminal
#include <iostream>

int main(){
    std::cout << "Hello World\n";
    return 0;

After pasting this, press ctrl+d on your keyboard (ctrl+d = EOL = end of line).
If this doesn't work, just write the above code in your favourite text editor and save as code.cpp

Step 3:
make code
Step 4:
 Notice how we used the make command to compile our source code and get an executable. Also, notice how the make command itself executed this command for us-
g++ code.cpp -o code
In our case, since there was only one source file, make knew what to do (just compile the single file). However, in case there are multiple source, make can't determine what to do.

For example, if you have 2 files, and the second one depends on the first one in some way. Then, you need the first one to be compiled before the second one. In case of the kernel, there are possibly millions of source code files, and how they get compiled is a very complex process.

If you navigate to the folder containing linux kernel (the folder where you extracted the tar ball), you'll get an idea of the sheer magnitude of complexity behind a kernel. For example, open the Makefile file in that folder in your favourite text and editor and see the contents of the folder. Makefile contains instructions which make (the command line tool we used earlier) uses to determine how to compile the source files in that directory (and subdirectories).

Some tools

Compiling our simple c++ program didn't need much, and your linux distribution (I'm using Ubuntu 16 for this tutorial) would come with the required tools pre-installed. However, compiling kernel needs some more stuff, and you'll need to install the required tools. For me, this command installed everything that was needed-
sudo apt-get install libncurses5-dev gcc make git exuberant-ctags bc libssl-dev
 Many of these tools would actually be pre-installed, so downloading and installing this won't take too long.

(if you're not on Ubuntu/Kali, then refer to this guide, as it has instruction for Red Hat based and SUSE based systems as well)

Download kernel

In the guide that I followed, he suggested that I clone this repository-
git clone git://
After cloning the repo, I had to choose the latest stable kernel and then proceed further with it. This would be useful when you want to keep pulling updates and recompiling your kernel. However, for the purpose of this tutorial, let's ignore this possibility (because cloning the git repo took a lot of time and the downloaded file was huge and everything was taking forever).

Instead, we just download and extract the tarball (as discussed earlier in the Linux Kernel section).


Here, we have two options. 
  1. Use a default configuration
  2. Use the configuration of your current kernel (on which your OS is running right now).
As in downloading the kernel step, I tried both methods, and for me, the default one worked better. Anyways, for current configuration, run the following-
cp /boot/config-`uname -r`* .config
This copies the configuration for your current kernel to a file in the current folder. So, before running this command, navigate to the folder containing the extracted tarball. For me, it was /home/me/Download/linux-4.9.8

For default config (recommended), run
make defconfig

If you don't see a config file, don't worry. In linux, files/directories starting with . are hidden. On your terminal, type vi .config (replace vi with your favourite text editor) and you can see the config file.


Similar to the way you compiled your c++ program, you can compile the kernel. In case of c++ program, we didn't have any Makefile, so we had to specify the name of the source file (make code), however, since we have a Makefile here, we can simply type make, and our Makefile and .config file (and probably many more files) will tell make what to do. Note that the config file contains the options which were chosen for your current kernel. However, on a later kernel, there might be some choices which weren't available in the the previous kernel (the one you're using). In that case, make will ask you what to do (you'll get to choose between option - yes and no, or options - 1,2,3,4,5,6, etc.). Pressing enter chooses the default option. Again, I suggest you use the default configuration file to avoid any issues.

To summarise, simply run this command-
If you have multiple cores, then specify it as an argument (compilation will be faster). For example, if you have two cores, run make -j2
If you have 4 cores, run make -j4 

Now, you can do something else for a while. Compilation will take some time. When it's finished, follow the remaining steps.


Simply run this command-
sudo make modules_install install

Fixing grub

There are following things that need to be changed in the /etc/default/grub file. Open this file as sudo, with your favourite text editor, and do the following.

  1. Remove GRUB_HIDDEN_TIMEOUT_QUIET line from the file.
  2. Change  GRUB_DEFAULT to 10 from 0
This is how my file looks after being edited. 

What these changes do is-
  1. Grub menu for choosing OS to boot from is hidden by default in Ubuntu, it changes that to visible.
  2. The menu shows up for 0secs, before choosing the default option. It changes it to 10 secs, so we get a chance to choose which OS to boot from.

After all this, just run the command to apply the changes.
sudo update-grub2

Now restart the machine.

Did it work?

If it worked, then you'll ideally see something like this upon restart -

In advanced options, you'll see two kernels. If you did everything perfectly, and no drivers issues are there, then your new kernel will boot up properly (4.9.8 for me). If you did everything reasonably well, and didn't mess things up too bad, then at least your original kernel should work, if not the new one. If you messed things up completely, then the new kernel won't work, nor would the old kernel (which was working fine to begin with). In my case, in the first trial, my new kernel wasn't working. In the second trial, both kernels were working.

Once you have logged in to your new kernel, just do a uname -r and see the version, and give yourself a pat on the back if it is the kernel version you tried to download.
I did give myself a pat on the back
If your new kernel is not working, then either go through the steps and see if you did something wrong, or compare with this guide and see if I wrote something wrong. If it's none of these, then try the other methods (default config instead of current kernel config, and vice versa). If that too doesn't work, try out some other guides. The purpose of the guide, as explained already, isn't to teach you how to compile linux kernel, but to improve your understanding, and I hope I succeeded in that.

Removing the kernel (optional and untidy section)

The accepted answer here is all you need. I'm gonna write it here anyways. Note that I'm writing this from memory, so some things may be a bit off. Follow the AskUbuntu answer to be sure.

Remove the following (this is correct)-

For me, Kernel version is 4.9.8. I don't remember exactly what commands I typed, and am too lazy to check them again, but I think these would work (no guarantee).

cd /boot/
rm *4.9.8*
cd /lib/module 
rm *4.9.8*
cd /var/lib/initramfs
rm *4.9.8*

Also, I have a faint recollection that the name of the initramfs folder was something a bit different in my case (not sure).



  1. Extraordinary post! I am enthused about watching narratives. The universe of workmanship is in every case profound and fascinating, and rouses composing different incredible articles. There lies a minor line and a point of separation in the middle of the two ideas of being a human and being a normal individual which has just been clarified by the obtaining of instruction and information that changes the individual totally and causes him include into somebody better.

  2. Recently, a friend of mine showed me a site where you can order study materials. There you can buy a ready-made article or essay on any topic. I'm glad I can save my time.

  3. That was an informative writeup. Kudos to the author! The large pool of native writers associated with the Cloud Computing Assignment Help Canada service of the LiveWebTutors platform guarantee to provide premium quality content with different assignment formats ranging from simple essays to complicated dissertations in record time.

  4. With the help of such step-by-step instructions, everyone can master this program on their own and get rid of all the errors, if any. I want to spend more time programming so I decided to use the services

  5. The best post with a piece of acquainted data. Much obliged to you for sharing such data. In the event that you need any scholastic level myassignmenthelp at sensible cost with better work, then, at that point call us now!

  6. Yes, you know this guide is not very relevant for this blog, but there will probably be people who will be interested. I would like to share with you also something useful, namely a service where you can buy pre written essays for sale

  7. Hello friends, my name is Rahul, who has brought something special for you. I am a resident of Jalandhar city. I have brought something for you, you will find it on my website. You can visit my website.
    Jalandhar Escort Service
    Escorts Service in Jalandhar


  8. Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks!


  9. Yes, you know this guide is not very relevant for this blog, but there will probably be people who will be interested.

  10. This is a great post I seen because of offer it. It is truly what I needed to see seek in future you will proceed after sharing such a magnificent post. 먹튀검증

  11. What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much.

  12. Gil Kenan (Poltergeist) is directing this adaptation of Scott Cawthon's horror video game Five Nights at Freddy's, in which security officer Mike Schmidht is hired by a pizza parlor that is terrorized by unknown monsters every night. The goal of this video game is to make it through each day of perilous labor alive.

  13. The bank is the only doubtlessly profitable place, and this profit will usually be negated by the commission charged by the casino. Traditionally, on both sides of the table the gamers take turns to be the lively player. If the lively player wins the coup, the 카지노사이트 same player continues to play for that facet of the table within the subsequent coup. If the player loses, the turn to play the hand passes to the next player on that facet in counter-clockwise rotation. Now, if the banker's whole is 7 or much less, the banker must decide whether or not to draw a third card.

  14. Thank you very much for providing this information. The best article with some well-known information about Compiling Linux Kernel. Call us right away if you require any type of academic level administrative law assignment topics at a reasonable price and of higher quality.

  15. I find this post so informative. Thanks for sharing this one!

  16. I wish it were easier than this. According to Mark Kriski wife Jessica, it could always be better to stay calm. Even Amber Lovato claims that her sister Demi is quite crazy.

  17. All Linux distributions are built upon the Linux Kernel. The kernel is in charge of resource allocation and communication between hardware and software.

  18. The blog "compiling-linux-kernel-on-ubuntu.html" offers a comprehensive guide for users eager to delve into kernel compilation on Ubuntu. Its clear instructions and insights make it an invaluable resource for both beginners and experienced users. Kudos to the author for simplifying a complex process and empowering readers to enhance their Linux experience. This blog exemplifies the collaborative spirit of the open-source community, fostering learning and innovation.
    new york uncontested divorce instructions
    new york state divorce mediation

  19. Here are the list of Hottest Female celebrities Visit Hottest Female Athletes

  20. These detailed tutorials enable anyone to become proficient with the programme independently and eliminate any faults, if any. I chose to use postal service because I want to spend more time programming. How have codes systems adjusted to fit modern technology as they have evolved throughout different nations? Any recent advancements that might have an impact on the future operations of international Postal Codes particularly catch my attention. I'm willing to assist.


© Kali Tutorials, 2016. Unauthorized use and/or duplication of this material without express and written permission from this site’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Shashwat Chaudhary and Kali Tutorials with appropriate and specific direction to the original content.