Your Personal Guide: Forcing IPv4 Priority on Debian

August 20, 2025 5 minutes minutes read ayie

Fed up with Debian's default preference for IPv6? Let's rewire the system's address resolution to prioritize the older, often more reliable IPv4 protocol. This isn't just a generic tutorial; it's a deep dive into the "why" and "how," tailored for your specific use case.

The Core Concept: Hijacking getaddrinfo

When an application on your system needs to resolve a hostname (like connecting to google.com), it doesn't do the heavy lifting itself. Instead, it calls a system function named getaddrinfo. This function is the traffic cop, responsible for sorting all available IP addresses and presenting them in a specific order to the application.

By default, this "sorting algorithm" follows RFC 3484, which, in a modern Debian installation, is configured to favor IPv6 addresses. We're going to change that algorithm's rules.


Your Step-by-Step Operation

Step 1: Access the Configuration Engine

The rules for getaddrinfo are stored in a single, powerful file: /etc/gai.conf. Let's open it with root privileges.

sudo nano /etc/gai.conf
# or, if you prefer the elegance of vim:
# sudo vim /etc/gai.conf

Step 2: Locate and Modify the Key Directive

Inside gai.conf, you'll find a list of commented-out "precedence" rules. These rules assign a priority number to different types of addresses. The lower the number, the higher the priority.

Scan for this specific line:

#precedence ::ffff:0:0/96  100

This line is your target. The ::ffff:0:0/96 block is the secret code for "IPv4-mapped IPv6 addresses"—essentially, a way of wrapping a standard IPv4 address in an IPv6 format. The 100 is its current, low-priority ranking.

Your Mission: Uncomment the line and give it a top-secret high priority by assigning a very low number. Let's use 50.

Change it to:

precedence ::ffff:0:0/96  50

Why 50? It's low enough to supersede the default IPv6 precedence values (which are typically 40 and below) without interfering with critical system addresses like loopback (::1/128, prec. 50).

Step 3: Activate the Change

Save the file and exit the editor. In nano, that's Ctrl+X, then Y, then Enter.

Crucially, no reboot or service restart is needed. The new rule is live immediately. Any new application that calls getaddrinfo will now receive sorted addresses based on your new priority list.


What You've Achieved

You haven't disabled IPv6. You've become a network traffic maestro. Now, when a hostname has both an IPv4 (A record) and an IPv6 (AAAA record) address, your system will present the IPv4 address first. Applications, which typically try addresses in the order they are given, will now establish connections over IPv4 by default.

This is perfect for bypassing broken IPv6 tunnels, avoiding dual-stack latency issues, or simply preferring the well-trodden path of IPv4 for specific tasks. The change is system-wide but application-respectful, meaning any app that chooses to ignore the sorted list and implement its own logic (like web browsers sometimes do) can still do so.

You've successfully overridden a default RFC behavior and taken precise control of your system's network stack. Well done.

Problem solver

Ayie