Downloading and using kernel source code (2.6.32.12 and newer) on Ubuntu

If you know you need to get your kernel source (or at least its headers) for a kernel version 2.6.32.12 and newer, you can find instructions to do so on your Ubuntu server here.


Why would I want kernel source code?

There may come a time when you need the kernel source code, usually to build kernel headers, compile a module or some other server voodoo. Most users won't need to mess with the kernel source on a virtual server, so if you don't know that you need it, you probably don't and can safely skip this article. If you know you do, though, read on.

Note that the instructions in this article apply to kernels version 2.6.32.12 and newer. We'll cover checking the version in a bit. If you do have an older kernel version you'll need to check another article, or pop into support chat for help finding the right source code package.

Install development tools

To do much with the kernel source once you have it, you'll want some development tools installed on your server. If you don't know that you have those tools installed already, you can run an install of a convenience package to be sure.

sudo aptitude update
sudo aptitude install build-essential

Not everything that metapackage installs is needed to compile the kernel, of course, but it includes packages useful for other development as well (they might be needed if you're compiling a module next, for example).

If you're the curious sort and want to see just what actually is needed to compile the kernel, then once you have the source unpacked you can take a look in the "Documentation" directory. The beginning of the "Changes" file lists programs and libraries that can be used when building the kernel, along with their required versions.

Check the kernel version

While you're on the command line, it's a good idea to check the version of the kernel you're running. You can do this through your management control panel (which is where you can install a different kernel version), but it's most reliable to check the version right on the server. Just run:

uname -r

The output will be the version of the kernel running on your virtual server. Something like:

2.6.32.12-rscloud

The kernel repository

Slicehost and the Rackspace Cloud use the same kernels, so you'll see both names when you visit our kernel repository:

http://kernel.slicehost.com

When you visit that site in a web browser you'll see a simple list of directories for the kernel versions that have source code available there. Click the one that corresponds to your kernel.

A choice of sources

Inside each of those directories are some other directories that offer you a choice of "flavor" when selecting your kernel source, something like:

Index of /2.6.32.12-rscloud/

Name                Last Modified           Size    Type
Parent Directory/                           -       Directory
patched_source/     2010-May-19 20:35:30    -       Directory
patches/            2010-May-19 20:40:47    -       Directory
vanilla_source/     2010-May-19 20:39:05    -       Directory

If you're looking for the source code of the kernel you're actually running on your server, you'll want what's in the "patched_source" directory.

The "vanilla_source" directory holds the original kernel source, before it was patched for our virtual environment. The "patches" directory contains the patches that were applied to the vanilla_source to make the patched_source.

A choice of formats

Finally, in that patched_source directory, you'll find a package with the source code you want. Or...maybe more than one package, with different extensions at the end.

Index of /2.6.32.12-rscloud/patched_source/

Name                        Last Modified           Size    Type
Parent Directory/                                   -       Directory
2.6.32.12-rscloud.tar.bz2   2010-May-17 19:03:51    62.3M   application/octet-stream
2.6.32.12-rscloud.tar.gz    2010-May-17 19:03:52    80.4M   application/octet-stream

Each of those packages contains the same source code, they're just compressed in different formats. The ".tar.gz" package is tarred and gzipped, which is a more widely compatible package format. The ".tar.bz2" package was tarred and compressed with bzip, which makes a smaller package but isn't compatible with as many systems as gzip.

You can safely pick either one, since pretty much any Linux distribution will be using a version of tar that can unpack both formats.

Downloading the source

So now you've identified the package you need. Next it's a matter of getting it onto your server.

If you have wget or curl installed, copy the link and download the package directly to your server. If you don't know what I'm talking about, you can just download the package in your browser instead, then copy it to your server from there using scp or sftp. So long as the package eventually winds up on your server you're good.

Unpack the source

Let's change our working directory to a standard location for linux source code:

cd /usr/src

And then unpack your package (known as a "tarball", in the jargon). If you downloaded the bz2 file, run:

sudo tar -xjvf ~/2.6.32.12-rscloud.tar.bz2

Substitute the path to the tarball for the "~/" above if it isn't in your home directory, of course.

If you downloaded the gz file, run:

sudo tar -xzvf ~/2.6.32.12-rscloud.tar.gz

Note that the difference between the commands is that a "j" option is passed to tar for the bz2 file where a "z" is passed to tar for the gz file.

Either way, after watching a bunch of filenames scroll past you'll wind up with a directory named after your kernel version, containing the kernel source. Nice work.

Make the headers

One possible use for the kernel source is to build the kernel headers needed to compile some other program. If that's what you're after, it's a pretty easy task to accomplish now. Change your working directory to the kernel source directory:

cd /usr/src/2.6.32.12-rscloud

And then run the following command:

sudo make headers_install

That's it, you're set.

Preparing to compile a module

If you want to compile a module there's more than one step involved, alas. And you'll actually need more than just the kernel source for this. You'll also need the configuration file that was used to build your kernel.

Thankfully, that's easy to find and put in place. Make sure you're in the kernel source directory, and run:

sudo zcat /proc/config.gz > .config

There you go. No need to muck about in a configuration menu or answer a bunch of yes/no questions, all the work's been done for you.

One more step. Run:

sudo make modules_prepare

You'll see where it's copying files, and maybe a few warnings. Skim the warnings, but if they're just advising you that it's "ignoring return value" of some function you don't need to worry about them.

Now your system should be ready to compile your module. From there follow the directions that (hopefully) came with the module's source code.

Summary

You should have the basic steps for getting the source code for your kernel down now: Check your kernel version, hit the kernel repository, pick the package you want, and then download and unpack it on your server. You still won't be able to install any kernel you compile from source, but you should be well-prepared to grab the source again if you upgrade your kernel through the control panel later.

  • -- Jered

Article Comments:

Lipozene commented Thu Apr 04 06:01:12 UTC 2013:

Thanks so much with this fantastic new web site. I’m very fired up to show it to anyone. It makes me so satisfied your vast understanding and wisdom have a new channel for trying into the world. Lipozene

Lipozene commented Tue May 28 13:59:59 UTC 2013:

I've looked information on this very subject and I'm happy that I've found it here

Lipozene commented Tue May 28 14:00:12 UTC 2013:

I've looked information on this very subject and I'm happy that I've found it here

Lipozene commented Tue May 28 14:01:10 UTC 2013:

I've looked information on this very subject and I'm happy that I've found it here

Lipozene commented Fri Jun 21 07:34:33 UTC 2013:

Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon!

Lipozene commented Fri Jun 21 07:35:10 UTC 2013:

Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon!

Sensa commented Wed Jul 10 15:12:20 UTC 2013:

It is in point of fact a nice and useful piece of information. I'm satisfied that you simply shared this useful information with us. Please stay us informed like this. Thanks for sharing.

Lipozene commented Tue Jul 16 13:32:57 UTC 2013:

This article was quite interesting. I am going to read more on your blog. I do agree with everything you are saying.

Lipozene commented Wed Jul 24 18:30:01 UTC 2013:

Amazing article. I am so impressed. Could never think of such a thing is possible with it...I think you have a great knowledge especially while dealings with such subjects.

Lipozene commented Wed Jul 24 18:30:28 UTC 2013:

Amazing article. I am so impressed. Could never think of such a thing is possible with it...I think you have a great knowledge especially while dealings with such subjects.

PE Devices commented Sat Aug 09 15:12:20 UTC 2014:

Your article is very good,memory is too beautiful,I am so happy to read this. Thank you for sharing with us!

mejor ebook commented Wed Aug 20 14:12:02 UTC 2014:

It's an amazing article for all the web people; they will get benefit from it I am sure.

my web blog; mejor ebook

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)