66

In notice my newly installed Ubuntu 10.10 always does an AAAA DNS lookup before A record lookups. Is there any way I can configure the system to do A lookups first ?

(While various browsers and other applications might contain their own resolvers, i'm talking about the ones in glibc)

nos
  • 1,243
  • To clarify, you do want to have both IPv4 and IPv6 present? – belacqua Mar 27 '11 at 21:40
  • Preferrably yes. But as I only have link local IPv6, it\s quite a waste asking for e.g. the AAAA record of google.com every time, and then ask for the A record afterwards. – nos Mar 28 '11 at 09:10

3 Answers3

80

It's the standard to prioritize IPv6 over IPv4 to allow next-generation IP take over :)

You can change this by editing the precedence blocks in /etc/gai.conf (gai stands for getaddrinfo, the standard system call for resolving host names). Just comment out the line as described in the file:

# For sites which prefer IPv4 connections change the last line to

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

A detailed explanation on gai.conf can be found in the man gai.conf(5).

pevik
  • 453
freddyb
  • 1,319
  • thanks, u saved my day! one of my server keeps getting ipv6 results from the dns server but my server just couldn't work properly w/ ipv6 – Boon Nov 21 '14 at 08:33
  • 1
    Hi, does this setting require a sysctl or any service reload or a reboot to take effect? I found your answer useful, but you might edit it in order to fix such doubts. – Marco Oct 20 '17 at 08:16
  • 2
    The man page for gai.conf says you have to un-comment all the lines, not just the IPv4 one, to get the correct modified behaviour. From gai.conf(5): "Once again, the presence of a single precedence line in the configuration file causes the default table to not be used." If you want the other default table lines to keep working, you have to un-comment them as well. – Ian D. Allen Feb 21 '19 at 03:28
18

My problem was the same, i had IPv6 queries made before IPv4 and they where wasting resources, so I did changed my /etc/gai.conf file as freddyb posted, but my scenario is a little bit different, my server is Natted, so IPv4 will not have precedence over IPv6 with only that line. The solution is also commenting out others lines at /etc/gai.conf so it will look like this

# For sites which prefer IPv4 connections change the last line to
precedence ::ffff:0:0/96 100
...
#    For sites which use site-local IPv4 addresses behind NAT there is
#    the problem that even if IPv4 addresses are preferred they do not
#    have the same scope and are therefore not sorted first.  To change
#    this use only these rules:
#
scopev4 ::ffff:169.254.0.0/112  2
scopev4 ::ffff:127.0.0.0/104    2
scopev4 ::ffff:0.0.0.0/96       14

So your server will try first ipv4 even if you are Natted!

pevik
  • 453
  • 2
    The /etc/gai.conf file in Ubuntu 15.04 "vivid" says those three scopev4 lines you suggest above are already the default. I made only the one-line precedence ::ffff:0:0/96 100 change and that worked even behind NAT. – Ian D. Allen Jun 04 '15 at 06:57
3

If you don't want IPv6 enabled at all, you can disable IPv6 to accomplish this. http://www.webupd8.org/2010/05/how-to-disable-ipv6-in-ubuntu-1004.html

if not, you can try addding

options single-request

To your /etc/resolv.conf file. (see http://udrepper.livejournal.com/20948.html for gory details)

  • 1
    The manual page for resolv.conf indicates that the option you suggest does exactly the oposite "This option [...] makes glibc perform the IPv6 and IPv4 requests sequentially (at the cost of some slowdown of the resolving process)." -- http://man7.org/linux/man-pages/man5/resolver.5.html – ndemou Sep 28 '15 at 19:10
  • sequential = first one, then the other - as opposed to parallel. it works fine here. – cweiske Feb 21 '17 at 11:57