Compiling a Linux Kernel (Ubuntu)

45 minutes
  • 5 Learning Objectives

About this Hands-on Lab

For most Linux administrators, updating a kernel is done through the package manager for the Linux distribution that is being used. However, there can be situations where a particular kernel version or specific kernel configurations are needed. In this lab, you will be tasked with downloading the full kernel source tree, updating the kernel configuration to certain specifications, and then compiling a new kernel image.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Enable source URIs for apt repositories.
  1. Open the sources.lst file in /etc/apt/:

    vim /etc/apt/sources.lst
  2. Uncomment deb-src URIs manually or by substitution — :%s/# deb-src/deb-src/g

  3. Resynchronize the package index files:

    apt-get update
Install dependencies
  1. Install additional dependencies:
    apt-get install -y make gcc libncurses5-dev dpkg-dev build-essential bison flex libssl-dev libelf-dev
Download the Kernel source tree to /usr/src
  1. Switch to the /usr/src directory:
    cd /usr/src/
  2. Download the kernel source tree:
    apt-get install -y linux-source kernel-package
  3. Select the following option:
    keep the local versions currently installed
Update the kernel configuration
  1. Switch to the kernel source tree directory and extract the archive:
    cd /usr/src/linux-source-[kernel_version]
    tar jxvf linux-source-[kernel_version].tar.bz2
    mv linux-source-[kernel_version]/* .
    rm -rf ./linux-source-[kernel_version]/
  2. Get the necessary packages
    apt-get build-dep -y linux-source
    mkdir debian/stamps
  3. Update the kernel configuration with the make menuconfig command:
    cp /boot/config-$(uname -r) .config
    yes '' | make oldconfig
    make menuconfig
  4. Navigate with your arrow keys and use enter to go to Device Drivers > Block devices
  5. Using the “n” key, exclude the following two drivers – DRBD Distributed Replicated Block Device support and Packet writing on CD/DVD media (DEPRECATED)
  6. Exit the menu and save your configuration changes.
  7. Prepare to compile the Kernel:
    make clean
Compile the kernel image (this step can take 15-20 minutes to complete)
  1. Create the compressed kernel image by calling the bzImage target:
    make bzImage

Note: This can take 15-20 minutes to complete

Additional Resources

As one of the lead Linux administrators on your team, you are tasked with compiling a new kernel image that will be used for all of the servers in your data center. In order to accomplish this, you will need to enable the deb-src URIs to allow the kernel source to be downloaded. Once the kernel source has been downloaded (ensure that it is downloaded to the /usr/src directory for consistency), you will need to install any dependencies that are necessary for configuring and compiling the kernel. The Distributed Replicated Block Device (DRBD) support should be excluded since your company will no longer be using this utility. In addition to DRBD, the Packet writing on CD/DVD media should also be excluded, given that it is now deprecated. Once these configuration changes have been made, compile a new compressed kernel image using the bzImage target.

Note: All tasks should be performed as the root user.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?