Building and Installing the latest kernel on CentOS 7

4
by on September 23, 2015 at 7:47 pm

The linux kernel is a constantly developing piece of software; new features and drivers are being added all the time.  Fortunately for administrators, the system call API is very stable, so using a newer kernel with your distribution is typically quite painless.  Building and installing a new kernel from source sounds quite intimidating, but in reality, could not be easier.  While you can find a 3rd party repo to install a newer kernel version from, I’m going to walk you through the steps to accomplish such a process.

Recently, I need to establish GRE tunnels over IPv6 transport on CentOS 7.  The easiest solution was to upgrade the kernel from 3.10 to 3.13.  Those steps are outlined here for your enjoyment: OpenVswitch GRE over IPv6

Why build a new kernel?  Well, as I stated in the introduction of this article, and clearly demonstrate a usecase in my GRE over IPv6 article, often times new software features require a kernel of at least a certain level, if not a specific level.  Additionally, if you are trying to keep disk space to a bare minimum, you can build and include on the kernel modules and components your system requires.

Building Kernel From Source

Disclaimer:  This may break who knows what on your specific system.  On my systems, everything worked just great.  Also, building a later kernel from source means you won’t be getting updates from the standard CentOS / RHEL repos when you run yum update.  It will be up to you to ensure your kernel is patched with the latest security patches and bug fixes.  It might be worth your time using Ubuntu or another main-stream distro that provides the updated software you need.  In my case, these are not public facing servers and are for internal reports only, and I just don’t have the time to port a bunch of custom RPMs over to .debs.

yum groupinstall "Development Tools"
yum install openssl-devel
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
cd linux-stable
git checkout v3.13.11
make oldconfig
make menuconfig

make rpm

Please note, you shouldn’t technically build software as root (it might break your system if something goes wrong). So, feel free to do everything after yum install as a regular user. I won’t tell if you build it as root though ;)

If you’ve never used git, you really should get familiar with the tool.  It might seem a little foreign at first, and I hate downloading tools that have only a very narrow use-case to my system, but git is pretty ubiquitous at this point.  The main two points of the git command are cloning (downloading the software), and checkout, which switches to a branch or tag within the version control system.  Be sure to checkout whatever version of the kernel you desire to test/use.

The line ‘make oldconfig’ will instruct make to copy the contents from your currently running system kernel into the config of the kernel you are about to compile.  This is a really handy feature to use when compiling newer versions later, but if you are on the default distro kernel, it probably contains a lot of modules you don’t want or need.  It will also annoyingly prompt you for your preferences for any new modules not defined in your current config file.  I just press and hold enter because the default is N.

The line ‘make menuconfig’ will start an ncurses menu to select components and modules of the kernel to include.  You probably need the ncurses-devel package for this, but I don’t recall.

‘make rpm’ is a really handy shortcut that kernel maintainers have included with the Makefile.  It consists of running all the rpmbuild commands for you, generating the spec file, and tar’ing up the source.  Very handy indeed.

So, that should build 3 RPMs containing kernel 3.13.  The kernel, kernel headers (kernel-devel) and kernel-debug.  I installed all 3.

After you have built the kernel, install the RPMs you need (at the least kernel and kernel-devel) you need to update grub2.  You can run the following:

grub2-mkconfig
grub2-editenv /boot/grub2/grub.cfg unset saved_entry

This will update grub to have the new kernel boot by default; otherwise you will boot into the old kernel by default.  You might want to manually boot into the kernel first to ensure it’s working if you’re not comfortable recovering from such a situation.  Go ahead and reboot your system so you can use the new kernel.

in CentOS, How-To

You can skip to the end and leave a response. Pinging is currently not allowed.

  • Pheckphul

    Coming here from Linux Today’s posting of this article. I came here to see how you were going to approach doing what the article states. However, I was disappointed to see the title is completely incorrect. This article doesn’t describe compiling and installing the latest kernel, 4.2.1, nor even the latest kernel with longterm support, but rather some random ephemeral 3.x kernel. How does the content jibe with the title?

    • http://www.zipref.com Mike

      Excellent comment. Compiling the latest kernel is exactly the same as the kernel I showed in the article. Just checkout the version you want using git.

  • Paranoid

    Hi! I’ve used this article to build a “make localmodconfig” kernel, 4.6.4 stable, centos7, based upon the modules currently running, with total success. Didn’t realize it is so trivial to do this. Amazing.

    • http://www.zipref.com Mike

      I’m glad you found it useful! Thanks for the comment.

Categories