Gentoo Kernel Configuration
July 25, 2026
Tags: linuxTable of Contents
Summary
Due to the modular nature of the Linux kernel, it is possible to strip away or add functionality without impacting the core system. Reasons for doing this may vary. Perhaps the target system is an embedded system limited in resources and the kernel must fit into a small amount of memory. Another reason could be to reduce the potential attack surface by stripping unnecessary device support. Or perhaps one simply wants a kernel tailored to their specific hardware and peace of mind that their system does not contain unnecessary bloat.
Although this article covers configuration for Gentoo Linux, configuring and installing a custom kernel is possible on any Linux distribution. Gentoo provides the sys-kernel/installkernel package, which automates kernel installation and bootloader configuration. Moreover, the kernel sources can be accessed at /usr/src/linux, which is a symlink pointing to the current kernel sources. Other distributions' conventions may vary, and managing the kernel sources may require more involvement from the user.
Obtaining Sources
It is enough to install the sys-kernel/gentoo-sources package. Enabling the symlink USE flag allows Portage to update the symlink at /usr/src/linux whenever the gentoo-sources package is updated. A sys-kernel/vanilla-sources package also exists, although it lacks Gentoo-specific patches.
root # emerge --ask sys-kernel/gentoo-sources
Manually changing the symlink at /usr/src/linux can be accomplished with the eselect system utility.
user $ eselect kernel list
Available kernel symlink targets:
[1] linux-6.18.41-gentoo
[2] linux-6.18.43-gentoo-dist *
user $ eselect kernel set 1
Configuring
The kernel sources should now be accessible at /usr/src/linux. Running make help will list parameters to be used with the make command
for various functionality.
To update an existing kernel configuration for a newer kernel, make oldconfig may be used. This option allows the user to interactively
decide how to set new kernel options. Alternatively, make olddefconfig may be used, which sets any new options to their defaults. If there is
no existing configuration (if no kernel has been installed yet), invoking make defconfig will generate a configuration with default options.
A number of different methods for configuration exist. It is possible to simply edit the configuration file in a text editor, or invoke make config
which prompts the user consecutively for each setting. The preferred method by most, however, is using menuconfig, which provides the user with an ncurses-based terminal user interface where settings are organized in a digestible hierarchy. menuconfig can be invoked with make menuconfig.
Configuration settings may be in one of three states. <Y> marks the functionality to be built into the kernel, <N> marks it to be excluded, and <M> modularizes it (that is, it can be loaded into the kernel when necessary).
The kernel settings are too numerous to cover in one article. Documentation on the various settings can be found here.
Compiling
Compiling the kernel with the desired configuration is as simple as running make inside /usr/src/linux. The compilation should preferably be
optimized by setting the number of jobs to the number of CPU cores of the host machine as otherwise a single core will be used and compilation will take significantly longer. The utility nproc can be used to get the number of CPU cores.
root # make -j$(nproc)
After the kernel has been compiled, modularized drivers (options set with <M>) must be installed.
root # make modules_install
Finally, the newly compiled kernel can be installed by invoking the Gentoo-specific installkernel script.
root # make install
Bootloader Configuration
Installing a new kernel does not touch already installed kernels. The installkernel script automatically creates a bootloader entry for the new kernel.
However, in the case of GRUB, the user will need to boot it from the 'Advanced Options' section. This can be simplified by editing the GRUB_DEFAULT option in /etc/default/grub. For example, if the new kernel entry is the third option under 'Advanced Options', one can set GRUB_DEFAULT="1>2" (entries are 0-indexed).
Finally, the bootloader can be re-configured and the new kernel should boot as the default.
root # grub-mkconfig -o /efi/EFI/Gentoo/grub.cfg
Cleaning Up
Old kernel sources can be deleted from the /usr/src directory.
root # rm -r /usr/src/linux-X.Y.Z
Similarly with old kernel modules.
root # rm -r /lib/modules/X.Y.Z
Finally, any files in /boot referencing old unwanted kernels can be deleted.
root # rm /boot/vmlinuz-X.Y.Z
root # rm /boot/System.map-X.Y.Z
root # rm /boot/config-X.Y.Z
root # rm /boot/initramfs-X.Y.Z