Prioritize IPv4 over IPv6

Prioritize IPv4 over IPv6


Lets check out how to prioritize IPv4 over IPv6 in CentOS / Ubuntu in this article. By default, if there is a IPv6 configuration in the network interface or configurations, the OS will try to establish connections over IPv6 than IPv4. If we dont have or dont want IPv6 or to use IPv4 for lookups or resolution keeping IPv6 configuration as it is, we can change the priorities of that. This will save us time, processing and resources. Usually, OS do a AAAA lookup first and then A record lookup if it fails. Also, some times it will result in issues with google captcha or other services as well. Lets see how to reduce the priority of IPv6 and make use of Ipv4 by default.

This is handled by gai.conf which is the configuration file of getaddressinfo .

In CentOS

Check for the file /etc/gai.conf and edit it with the contents below. If the file doesn’t exists, create a new file and add the contents.

label                     ::1/128            0
label                     ::/0                  1
label                     2002::/16       2
label                     ::/96                3
label                     ::ffff:0:0/96   4
precedence         ::1/128            50
precedence         ::/0                  40
precedence         2002::/16       30
precedence         ::/96                20
precedence         ::ffff:0:0/96   100

In Ubuntu

Open the file /etc/gai.conf and check for the line

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

And un-comment it. It will look like

precedence ::ffff:0:0/96 100

Once the changes are saved, your server(s) will use IPv4 over IPv6 without disabling IPv6 on the server. No server reboots or service restarts required for this change. Please see the man page for getaddressinfo for more details

Let me know how it goes. 🙂