Build a new Linux Kernel
How to build and install a new linux kernel from source
Remember, this how to works in a debian-like systems. It’s necessary some adjustments to use in another system.
- Install dependencies:
$ sudo apt -y -q install bc flex bison build-essential git libncurses-dev libssl-dev libelf-dev wget xz-utils
- Go to the kernel.org, choose the repository, and clone it. For exemple, if you want the new stable:
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git --depth=1
Why –depth=1? This way just the last commit will be cloned. This is a good thing if you want to spend less space in disk (a completed cloned linux repo has more than 8GB).
- Now it’s necessary to create a valid config file to build, the easy way is to copy a valid old .config inside the /boot folder. First enter in the cloned folder and copy the config file from the default kernel running in the system:
cd linux
cp -v /boot/config-$(uname -r) .config
- To avoid some build issues, run the command:
$ make oldconfig
make oldconfig is a command used during the linux kernel compilation process to upgrade an existing kernel configurations to a new kernel version. Probably several questions will be asked after run this command, just press Enter to get through them all.
If it’s necessary more configurations, just run: make menuconfig do the adjusts and exit.
- Disable the security certificates (basicaly if you are in Ubuntu):
$ scripts/config --disable SYSTEM_TRUSTED_KEYS
$ scripts/config --disable SYSTEM_REVOCATION_KEYS
- Build the kernel:
$ make -j$(nproc)
- Install the modules:
$ sudo make modules_install
- Install the new kernel:
$ sudo make install
- Reboot the system and choose the new kernel in grub if necessary:
$ sudo shutdown -r now
How to unistall the new kernel
Just delete these files:
$ sudo rm /boot/vmlinuz-[TARGET]
$ sudo rm /boot/initrd-[TARGET]
$ sudo rm /boot/System-map-[TARGET]
$ sudo rm /boot/config-[TARGET]
$ sudo rm -rf /lib/modules/[TARGET]
And update grub:
$ sudo update-grub