Debian From Scratch (DFS): A Step-by-Step Guide to Building a Custom Debian System
Debian From Scratch is a project that empowers users to create a fully customized Debian system from the ground up by compiling and configuring all core components (kernel, libraries, tools). This process is ideal for advanced users who want deep control over system behavior, optimization for specific hardware, or a minimal runtime environment. Below is a structured guide to the DFS workflow:
Before starting, ensure your environment meets the following requirements:
apt-get (Debian/Ubuntu) or your distribution’s package manager:sudo apt-get install build-essential gcc make debhelper fakeroot bc bison flex libncurses-dev
debian-archive-keyring, linux-source, glibc-source) from the official Debian website or mirrors. Store them in a dedicated directory (e.g., ~/dfs_sources).The kernel is the heart of your custom system. Follow these steps to build it:
tar -xvf linux-source-*.tar.xz -C ~/dfs_build/
cd ~/dfs_build/linux-source-*
make menuconfig (text-based GUI) to enable/disable drivers/features (e.g., CPU architecture, filesystem support). For most users, the default “General Setup” options work, but you may need to adjust network or storage drivers for your hardware.make -j$(nproc) # Use all available CPU cores for faster compilation
sudo make modules_install
sudo make install # Installs the kernel to /boot/
sudo update-grub to add the new kernel to the boot menu.Compile and install core system components to form a functional base:
tar -xvf glibc-source-*.tar.xz -C ~/dfs_build/
cd ~/dfs_build/glibc-*
mkdir build && cd build
../configure --prefix=/usr --disable-werror
make -j$(nproc)
sudo make install
bash, coreutils, findutils, and grep from source or use debootstrap to fetch pre-built binaries (faster for beginners). For example, with debootstrap:sudo debootstrap stable /mnt/debian http://deb.debian.org/debian
This creates a minimal Debian root filesystem in /mnt/debian.apt and dpkg to handle future software installations. These tools simplify dependency resolution and package installation.Populate the root filesystem (/) with essential directories and files:
/mnt/debian:sudo mount /dev/sdXn /mnt/debian # Replace sdXn with your target partition
sudo mkdir -p /mnt/debian/{dev,proc,sys}
sudo mount --bind /dev /mnt/debian/dev
sudo mount --bind /proc /mnt/debian/proc
sudo mount --bind /sys /mnt/debian/sys
rsync or cp to transfer files from the build directory to /mnt/debian. For example:rsync -a ~/dfs_build/linux-source-*/arch/x86/boot/bzImage /mnt/debian/boot/vmlinuz-$(uname -r)
sudo mknod /mnt/debian/dev/console c 5 1 and other necessary device nodes (refer to linux-source/Documentation/devices.txt for details).Customize the system to meet your needs:
/mnt/debian/etc/hostname:echo "my-custom-system" | sudo tee /mnt/debian/etc/hostname
timedatectl (if installed) or by symlinking the appropriate zoneinfo file:sudo ln -sf /usr/share/zoneinfo/America/New_York /mnt/debian/etc/localtime
/mnt/debian/etc/network/interfaces for static/DHCP configuration (see Network Setup section below).adduser:chroot /mnt/debian adduser myuser
chroot /mnt/debian usermod -aG sudo myuser # Add user to sudo group
For a quicker DFS setup, use debootstrap to create a minimal Debian system without full compilation:
debootstrap:sudo apt-get install debootstrap
sudo debootstrap stable /mnt/debian http://deb.debian.org/debian
sudo chroot /mnt/debian /bin/bash
dmesg, /var/log/syslog) for errors.apt to install additional software (e.g., sudo apt install vim git).dd or imaging tools) or create a custom Debian ISO for easy distribution.man pages (e.g., man make, man debootstrap) for detailed guidance.By following these steps, you can create a tailored Debian system optimized for your hardware and use case.