Compile Linux Kernel

· Linux Scripts

At the heart of every Linux distribution is the kernel. A great way to get more familiar with the system is to compile a customized version of the kernel for your own machine. The kernel is relatively easy to build because of the different customization options users can choose.

In order to customize the kernel, the command, make menuconfig, will bring up a nice menu that allows the addition of removal of included drivers. If a system is not recognizing a bluetooth, ethernet, wireless, or any of kind of adapter, a quick Google search may tell you exactly where in the menus the driver is inside the kernel menu. Just press “Space” to enable the driver and finish building the kernel.

Don’t use this script as a guaranteed and support way of creating a kernel. Many users will tell you to NOT compile the kernel as root for various issues, but if you just want to learn for a basic understanding, the code should work with minor code changes. I would run each line of code individually to ensure there are no errors. I’ve commented out other ways of doing the same task and left them in the code for reference.

Note: If care is not taken on the steps below, the current kernel may be overwritten or the system may fail to boot. As always, please test any of the scripts on this site on a virtual machine. VMWare Server (which is free and web based) is preferred, but VirtualBox is a great alternative. I am currently using Hyper-V, but it requires Windows Server 2008 and integration driver support is very weak.

### ——————————————————————– ###
# This script compiles the linux kernel.
# Location:
# Date Created: 4/29/2010
# Date Modified: 4/29/2010
# Permissions Required: Non-root
# Notes: Not every link will be valid or the latest, double
# check before relying on the code
### ——————————————————————– ###
# Download the kernel source
# Uncompress the source
tar xvf linux-
#gzip -dc linux- | tar xvf -
# Move the kernel source folder
mv linux- /usr/src
# Navigate to the kernel folder
cd /usr/src/linux-
# Install the required packages
apt-get install kernel-package ncurses-devel mkinitrd-tools
apt-get install initramfs-tools
#yum -y install kernel kernel-package ncurses-devel
# Clean the kernel source
make mrproper
# Configure the kernel
make menuconfig
#make oldconfig
# Build the kernel
make modules modules_install bzImage
# This step moves the kernel, bzImage, to the /boot folder and adds an entry to the /boot/grub/menu.lst file
make install
# Create the RAM drive
#mkinitrd -f --preload hv_vmbus --preload hv_storvsc --preload hv_netvsc --preload hv_blkvsc /boot/initrd- - (Hyper-V only)
mkinitrd -f /boot/initrd-
#mkinitramfs -o /boot/initrd-
# Copy the config file to the boot folder
cp -v .config /boot/config-
echo Please reboot the system now.
author image

About Joseph Spurrier

I'm passionate about building solutions to automate tasks and improve efficiency. I worked for a few companies in the digital healthcare space. My education is in forensics. Tech guy. Traveler. Runner. Guitar player. Scotch and beer enthusiast. GitHub LinkedIn Twitter Google+

Share this post

Comments powered by Disqus