With the instructions in Recompiling the Kali Linux Kernel, we can recompile the whole linux kernel of our kali linux.
We need to start by installing several build dependency packages, some of them may have already been in installed.
Download the Kernel Source
For my system, the kernel version is 4.18, which will be used in following example. Of course, the workflow of other version is just the same.
1 2 3 4
Then we get the compressed archive of the kernel sources, and we’ll extract these files in our working directory, (no special permission need for compiling the kernel). In our example, we use /opt/kernel, and the ~/kernel is also an appropriate place.
Optionally, we may also apply the rt patch, which is for real-time os features.
Configure the Kernel
When building a more recent version of kernel (possibly with an specific patch), the configuration should at first be kept as close as possible to the current running kernel, shown by uname -r. It is sufficient to just copy the currently-used kernel config to the source directory.
If you need to make some changes or decide to reconfigure all things from scratch, just call make menuconfig command and inspect all the details. Note: we can tweak a lot in this phase.
Write Some Code
Add one line of code for test(fun), in file init/main.c, start_kernel function
Build the Kernel
Once configured, we can make the kernel. Rather than invoking make deb-pkg as the official doc suggested, we use make bindeb-pkg here, which will not generate Debian source package, or invoke make clean.
After a while, we get following package in the parent directory
1 2 3 4
Solve: comment/delete the corresponding config line.
Install/Remove the Kernel
Once booted, we can use dmesg to verify our printk message. Removing kernel can also be done with dpkg.