Convince apt-get *not* to use IPv6 method

Add -o Acquire::ForceIPv4=true when running apt-get.

If you want to make the setting persistent just create /etc/apt/apt.conf.d/99force-ipv4 and put Acquire::ForceIPv4 "true"; in it:

echo 'Acquire::ForceIPv4 "true";' | sudo tee /etc/apt/apt.conf.d/99force-ipv4

Config options Acquire::ForceIPv4 and Acquire::ForceIPv6 were added to version 0.9.7.9~exp1 (see bug 611891) which is available since Ubuntu Saucy (released in October 2013) and Debian Jessie (released in April 2015).


As Gilles says, use gai.conf. Notes:

  1. This works at a much lower level (DNS and IP networking) than APT, so it will change how all your applications network--at least, all that use getaddrinfo.
  2. Before editing your gai.conf, you should back it up, and also read it (don't worry, it's short). The edits below are probably already mentioned in your current file; if the current file indicates something different from what's mentioned below, you should probably prefer what's in your current file.

But if this is what you want (which it probably is), let's proceed. Say we have two hosts www.he.net and www.ripe.net :

$ host www.he.net
www.he.net is an alias for he.net.
he.net has address 216.218.186.2
he.net has IPv6 address 2001:470:0:76::2

$ host www.ripe.net
www.ripe.net has address 193.0.6.139
www.ripe.net has IPv6 address 2001:67c:2e8:22::c100:68b

Case 1: prefer IPV4 for all hosts

Append to /etc/gai.conf the following line:

precedence ::ffff:0:0/96  100

After saving the edited file (no need to restart), you should see networking apps (e.g., telnet) using IPV4: e.g.,

$ telnet www.ripe.net 81
Trying 193.0.6.139...
^C
$ telnet www.he.net 81
Trying 216.218.186.2...

Case 2: prefer IPV6 for specific hosts

If we want to prefer IPV6 only for www.he.net or its network, we can append a mask/prefix for all, or just some part, of its IPV6 address to /etc/gai.conf. E.g., the following line:

precedence 2001:470::/32 100

(after saving the edited file) produces

$ telnet www.ripe.net 81
Trying 193.0.6.139...
^C
$ telnet www.he.net 81
Trying 2001:470:0:76::2...
^C

Case 3: prefer IPV4 for specific hosts

If we invert the mask will the reverse be true? According to @GrueMaster, appending

precedence 2001:470::/96 100

worked for him after disabling IPV6 for security.ubuntu.com (otherwise it stalls forever).


See also:

  • http://www.funtoo.org/wiki/IPv6_Networking#Prefer_IPv4_over_IPv6 (archived @ https://web.archive.org/web/20180905053005/https://www.funtoo.org/IPv6_Networking#Prefer_IPv4_over_IPv6 )
  • http://www.getipv6.info/index.php/Customer_problems_that_could_occur#GNU.2FLinux (archived @ https://web.archive.org/web/20180920175314/https://getipv6.info/display/IPv6/IPv6+Info+Home#GNU.2FLinux )

You could setup apt-cacher-ng on a spare machine to act as a proxy/cache for all of your hosts. You can force the configuration to only use specific hosts or use the /etc/hosts trick suggested by @badp on that one machine.

apt-get install apt-cacher-ng

Once you have apt-cache-ng setup you just need to drop the following line (with IP address/hostname altered to point at your cacher machine) in /etc/apt/apt.conf.d/90httpproxy

Acquire::http { Proxy "http://[192.168.1.254]:3142"; };

I use that setup to reduce bandwidth usage but it should workaround your problem. Unfortunately I'm not aware of a way to directly disable ipv6 lookups for apt-get itself.

Tags:

Ipv6

Apt