How can I modify the kernel when I already have Ubuntu running on my machine?

Sorry if this is an extremely stupid question, but I'm brand new to Linux and I'd like to try to figure out how to modify my own system. From what little I know so far about Linux, I would imagine that what I'm actually looking for is the kernel's source code. I know that there are some resources out there that would allow me to download a copy of the kernel (like kernel.org), but presumably that is just a copy, and I would have to switch from my current kernel to that new one after modifying it in order to see the changes that I make take effect. Is there a way to avoid doing that, and just modify the kernel that I already have? And if not, how do I tell Ubuntu to use the new Kernel that I've downloaded rather than the original one? And how do I know whether or not my changes will screw up the system? Thanks in advance for your help!


Your question can be broken down into two parts, as you can tweak some kernel parameters during runtime and also bytepatching but as a beginner i would strongly not suggest you to try bytepatching.

In your case, to getting started would i strongly suggest to do modifications to the source code recompile it, and install and test it. It do also exist ways to test your kernel in emulators eg. without the need to reboot your system eg. QEMU is one...

It exist a lot of ways to do kernel development, here is way that should be simple if you have some expirence with using ubuntu.

Quick and dirty (you need to complete some arguments\paths):

sudo apt-get install libncurses5-dev binutils-dev linux-source 
sudo apt-get install fakeroot build-essential crash kernel-wedge kernel-package  

mkdir ~/src
cd ~/src
apt-get source linux-image-$(uname -r)
cd linux-3._LINUX_SRC_FOLDER_

Just enter the menu tweak around if you wanna or just exit and save.

make menuconfig

The level of parallelism when compiling it. (number of processor-cores +1)

export CONCURRENCY_LEVEL=5

Pick the targeted system, google it or leave it out. Two examples

export CHOST="x86_64-pc-Linux-gnu"
export CHOST="i686-pc-linux-gnu"

If you want the compiler to optimize eg.

export CFLAGS="-march=native -O2 -pipe"
export CXXFLAGS="$CFLAGS"

Do your tweaks, eg.

vim ~/src/LINUX_SRC_FOLDER/include/linux/hid.h 
cd  ~/src/LINUX_SRC_FOLDER

Compile the kernel and make deb packages, and report time used.

time fakeroot make-kpkg --initrd --append-to-version=-tweak-CUSTOM-NAME kernel-image kernel-headers

Install your compiled kernel and the headers.

sudo dpkg -i ../linux-image-3._YOUR_DEB_FILE_
sudo dpkg -i ../linux-headers-3._YOUR_DEB_FILE_

Your kernel should be installed and show in your grub-boot-menu. Reboot.

sudo reboot

Enable the grub boot menu or tap shift a few times during startup.

Select your desired kernel to boot.

If you use this guide, feel free to improve it.