niels / Software / #linux,#ubuntu

Preferring IPv4 over IPv6

Heads up! This post is more than a year old.

As explained in my previous post, I get my IPv6 subnet through a tunnelbroker. This is a great way to use IPv6 when your Internet provider does not yet support it.

There’s a downside though: the tunnel adds overhead in terms of both latency and a lower MTU. It also introduces an additional point where congestion or failure can occur.

Getaddrinfo

Most Linux apps use the getaddrinfo function to obtain the IP address for a hostname. It is this function that causes your apps to prefer IPv6 over IPv4.

Fortunately it comes with a configuration file, which we can modify to have it prefer IPv4 over IPv6.

Open the configuration file by entering:

sudo vi /etc/gai.conf

(replace vi with your preferred editor)

Then scroll down to the following section:

#precedence  ::1/128       50
#precedence  ::/0          40
#precedence  2002::/16     30
#precedence ::/96          20
#precedence ::ffff:0:0/96  10
#
#    For sites which prefer IPv4 connections change the last line to
#
#precedence ::ffff:0:0/96  100

Uncomment that last line, so it looks like this:

#precedence  ::1/128       50
#precedence  ::/0          40
#precedence  2002::/16     30
#precedence ::/96          20
#precedence ::ffff:0:0/96  10
#
#    For sites which prefer IPv4 connections change the last line to
#
precedence ::ffff:0:0/96  100

Save the file.

That’s it! Most apps will now prefer IPv4 over IPv6.

0 comments