Ubuntu – How to get the kernel source code

kernel

I am planning to write some device drivers and I need to get the Linux kernel source. My Linux kernel version is 3.2.0-23-generic-pae and I downloaded the image from this. In many of the articles I have read, it tells me that I need to have the entire kernel tree to start inserting new modules.

Is it enough if I download this image and paste it into the usr/src/ folder or do I have to do something else?

Best Answer

This will get the source of the stock kernel:

apt-get source linux-source

You can check what version of the kernel is running like this:

uname -r

Which will print something like:

3.13.0-46-generic

You can find a list of current source package versions available on your system via:

apt-cache search linux-source

To get the upstream version of the kernel:

git clone git://kernel.ubuntu.com/ubuntu/ubuntu-trusty.git

In the above link, 'trusty' is the codename for the version of Ubuntu. You can find out the codename for the version of Ubuntu you have installed via:

cat /etc/lsb-release