Compiling a custom FreeBSD kernel gives you the ability to bake in specific drivers and modules, or tweak settings from what ships out of the box.

This guide covers pulling the source, configuring, compiling, and running your own Linux and FreeBSD kernels.


DEBIAN-BASED DISTROS

Install dependencies and get the source code.

sudo apt update
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev bc ccache dwarves git
git clone --depth 1 --branch v7.1 https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git /usr/src/linux
cd /usr/src/linux

Configure the kernel.

cp /boot/config-$(uname -r) .config
scripts/config --disable SYSTEM_TRUSTED_KEYS
scripts/config --disable SYSTEM_REVOCATION_KEYS
make olddefconfig
make menuconfig

Compile and install the kernel and modules.

make -j$(nproc)
sudo make modules_install
sudo make install

Update GRUB (bootloader).

sudo update-grub

Reboot and verify.

sudo reboot
uname -r

FREEBSD

Get the source code.

pkg install git-lite
git clone --branch releng/15.1 https://git.freebsd.org/src.git /usr/src

Create your custom configuration.

cd /usr/src/sys/amd64/conf
cp GENERIC MYKERNEL
nano MYKERNEL

Compile the kernel.

cd /usr/src
make buildkernel KERNCONF=MYKERNEL -j$(sysctl -n hw.ncpu)

Install the kernel.

make installkernel KERNCONF=MYKERNEL

Reboot and verify.

shutdown -r now
uname -a

REFLECTIONS

For Both

While the kernel itself is monolithic, both operating systems support Kernel Objects (.ko files) that can be loaded at boot time or at runtime (modprobe on Linux, kldload on FreeBSD) to extend functionality without requiring recompilation each time.
For Linux
The kernel (/boot) and its modules (/lib/modules/) live in two separate parts of the filesystem.
Linux also creates an initramfs / initrd (Initial RAM Filesystem): a small, temporary root filesystem loaded into RAM that contains just enough .ko drivers to initialize your storage and hand off control to the real root filesystem.
For FreeBSD
The kernel and its modules are kept together in a single directory (/boot/kernel/).
The kernel itself is the core monolithic binary, but you will also find many .ko files alongside it for loadable drivers (e.g., zfs.ko for the ZFS filesystem).