How to Disable IPv4 or IPv6 in the Kernel on FreeBSD Operating System

Learn how to disable IPv4 or IPv6 at the kernel level on a FreeBSD system by modifying kernel configurations and recompiling the kernel.

Introduction

FreeBSD, a powerful and flexible UNIX-like operating system, provides robust networking capabilities, including support for both IPv4 and IPv6. However, there are situations where administrators may need to disable either IPv4 or IPv6 for security, performance optimization, or specific application requirements. Disabling these protocols at the kernel level ensures that they are not available at all, rather than merely disabling them at the interface level.

This guide covers how to disable IPv4 or IPv6 at the kernel level on a FreeBSD system, providing steps to modify kernel configurations and recompile the kernel.

Understanding Kernel-Level Networking in FreeBSD

In FreeBSD, IPv4 and IPv6 are implemented as separate kernel modules. By default, both are enabled to provide full networking support. If you need to disable either protocol completely, you must modify the kernel configuration and recompile it.

Checking Current IPv4 and IPv6 Status

Before making any modifications, check the current status of the networking protocols:

  • To check IPv4 interfaces:

    ifconfig -a | grep inet
    
  • To check IPv6 interfaces:

    ifconfig -a | grep inet6
    

If you see active IPv4 or IPv6 addresses, it indicates that the respective protocol is enabled.

Disabling IPv4 in the FreeBSD Kernel

To completely disable IPv4 support in the FreeBSD kernel, follow these steps:

Step 1: Modify the Kernel Configuration File

  1. Navigate to the kernel configuration directory:

    cd /usr/src/sys/{ARCH}/conf
    

    Replace {ARCH} with your system architecture (e.g., amd64 for 64-bit systems).

  2. Copy the default kernel configuration file:

    cp GENERIC MYKERNEL
    

    Replace MYKERNEL with a meaningful name.

  3. Edit the copied kernel configuration file using a text editor such as vi or nano:

    nano MYKERNEL
    
  4. Locate the following lines and remove or comment them out by adding # at the beginning:

    options INET  # Comment out or remove this line to disable IPv4
    

Step 2: Compile and Install the New Kernel

  1. Compile the kernel:

    cd /usr/src
    make buildkernel KERNCONF=MYKERNEL
    
  2. Install the new kernel:

    make installkernel KERNCONF=MYKERNEL
    
  3. Reboot the system to apply changes:

    reboot
    
  4. Verify that IPv4 has been disabled:

    ifconfig -a | grep inet
    

    There should be no IPv4 addresses listed.

Disabling IPv6 in the FreeBSD Kernel

To disable IPv6 in FreeBSD, follow similar steps as for IPv4:

Step 1: Modify the Kernel Configuration File

  1. Edit the kernel configuration file:

    nano /usr/src/sys/{ARCH}/conf/MYKERNEL
    
  2. Locate and remove or comment out the following line:

    options INET6  # Comment out or remove this line to disable IPv6
    

Step 2: Compile and Install the New Kernel

  1. Compile the modified kernel:

    cd /usr/src
    make buildkernel KERNCONF=MYKERNEL
    
  2. Install the new kernel:

    make installkernel KERNCONF=MYKERNEL
    
  3. Reboot the system:

    reboot
    
  4. Verify that IPv6 has been disabled:

    ifconfig -a | grep inet6
    

    No IPv6 addresses should be listed.

Alternative: Disabling IPv4 or IPv6 Without Kernel Compilation

If you prefer not to recompile the kernel, you can disable IPv4 or IPv6 at the system level using rc.conf:

  • To disable IPv4:

    ifconfig_DEFAULT="inet6 auto"
    
  • To disable IPv6:

    ifconfig_DEFAULT="inet -inet6"
    

Then restart networking or reboot the system:

service netif restart
service routing restart

Conclusion

Disabling IPv4 or IPv6 at the kernel level in FreeBSD ensures that the protocol is completely unavailable on the system. This is useful for security-hardened environments or specialized networking setups. While kernel recompilation is the most robust method, administrators can also disable these protocols at the system level using rc.conf for a more flexible approach.