Obtaining the source for an Ubuntu release
There are a number of different ways of getting the kernel sources. The two main ways will be documented here.
If you have installed a version of Ubuntu and you want to make changes to the kernel that is installed on your system, use the apt-get method (described below) to obtain the sources.
However, if you wish to get the most up to date sources for the Ubuntu release you are running and make changes to that, use the git method (described below) to obtain the sources.
Obtaining the kernel sources for an Ubuntu release using apt-get
The literal source code which generated a specific binary package may be obtained using the apt-get source <package> command. For example to obtain the source for the currently running kernel you can use the command below:
apt-get source linux-image-unsigned-$(uname -r)
or failing that:
apt-get source linux-image-$(uname -r)
Obtaining the kernel sources for an Ubuntu release using git
The source for each release is maintained in its own git repository on Launchpad.
The git repository is listed in the Vcs-Git: header in the source package and is of the following form:
For example, the standard Cosmic kernel is available at:
There is a tree for each of the currently supported releases as well as any open development and upcoming releases:
The distro kernel is always on the master branch in these repositories. Each release also has a master-next branch containing the commits that will go onto the master branch and become the next release for that release.
A number of releases also have other source packages which represent other related but divergent kernels for other purposes. For example, there is a specialised AWS kernel available in the linux-aws source package. (Previously these sorts of things were done in Topic Branches and some older kernels and projects still use them.)
If you cannot use the git protocol (perhaps because of a firewall), you can use the slower http protocol. For example:
Obtaining a copy
To obtain a local copy you can simply git clone the repository for the release you are interested. The git command is part of the git package.
For example to obtain the Bionic tree:
This will download several hundred megabytes of data. If you plan on working on more than one kernel release you can save space and time by downloading the upstream kernel tree. Note that once these two trees are tied together you cannot remove the virgin Linus tree without damage to the Ubuntu tree:
git clone git://kernel.ubuntu.com/ubuntu/linux.git git clone --reference linux git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/bionic git clone --reference linux git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/cosmic
In each case you will end up with a new directory ubuntu-<release> containing the source and the full history which can be manipulated using the git command from within each directory.
By default you will have the latest version of the kernel tree, the master tree. You can switch to any previously released kernel version using the release tags. To obtain a full list of the tagged versions in the release as below:
$ git tag -l Ubuntu-* Ubuntu-2.6.27-7.10 Ubuntu-2.6.27-7.11 Ubuntu-2.6.27-7.12 Ubuntu-2.6.27-7.13 Ubuntu-2.6.27-7.14 $
To look at the 2.6.27-7.13 version you can simply checkout a new branch pointing to that version:
git checkout -b temp Ubuntu-2.6.27-7.13
You may then manipulate the release - for example, by adding new commits.