Course contentsShow
← Linux
Lesson 1561 of 1,81913. Containerization Primitives and NamespacesFree lesson

Read-Only Kernel Root Filesystems

Configure the host kernel to deny modifications to sensitive system-level directories.

In this lesson, you will learn how to harden your Linux kernel environment by enforcing a read-only root filesystem to prevent unauthorized modifications to critical system binaries and configuration files.

The Principle of Immutability

At the advanced stage of container orchestration, we treat the root filesystem not as a workspace, but as an immutable artifact. When a kernel is configured to treat its root partition as read-only, it effectively mitigates entire classes of exploits that rely on persistence—such as rootkits writing to /bin or /etc. Even if a process gains elevated privileges within a namespace, the kernel layer prevents the filesystem from committing those changes to the underlying storage.

Configuring the Read-Only State

The primary mechanism for enforcing this at the kernel level is through the boot command line parameters. By passing ro to the kernel, the bootloader instructs the kernel to mount the root filesystem in read-only mode immediately after the initial RAM disk (initrd) phase.

If you are inspecting a running system, you can verify the current mount state using findmnt:

# Verify the root mount status
findmnt /
# Look for the 'ro' flag in the OPTIONS column

To transition a system into this mode, you typically define the kernel command line in your bootloader (e.g., GRUB). In /etc/default/grub, you would ensure the GRUB_CMDLINE_LINUX_DEFAULT includes:

root=/dev/sda1 ro

Bridging the Gap with Overlays

Since a strictly read-only filesystem breaks standard Linux operations (as services expect to write to /var/log or /tmp), we combine this with tmpfs mounts. By mounting a transient, memory-backed filesystem over sensitive directories, we create an illusion of writeability while maintaining a tamper-proof underlying root.

For instance, your init script would mount the root as ro, then perform a recursive mount:

mount -t tmpfs tmpfs /var/log

This ensures that logs are captured in memory for the duration of the container's lifecycle, but any attempt to modify the /usr or /bin trees is rejected by the kernel's virtual filesystem (VFS) layer, providing a robust defense-in-depth posture.

Key Takeaway: Implementing a read-only root filesystem moves the security boundary from user-space permissions to the kernel's VFS layer, rendering sensitive system directories immune to persistent unauthorized changes.