connecting to archive.ubuntu.com takes too long

I solved this on 12.10 by editing /etc/gai.conf and uncommenting the line:

#
#    For sites which prefer IPv4 connections change the last line to
#
precedence ::ffff:0:0/96 100

This lets you keep IPv6 enabled, but sets the order of precedence to prefer IPv4 over IPv6.


Solution:

I figured out the problem. I had to disable IPv6 connectivity, as The Lord Of Time pointed out.

Running the following command in Terminal tells if IPv6 is enabled or not:

cat /proc/sys/net/ipv6/conf/all/disable_ipv6

0 means its enabled, while 1 means its disabled.

To disable IPv6 from within Terminal, enter the following:

echo "#disable ipv6" | sudo tee -a /etc/sysctl.conf
echo "net.ipv6.conf.all.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
echo "net.ipv6.conf.lo.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Re-run the first command, and it should be 1 now.


If you only want to modify apt-get, then you can force IPv4 with:

apt-get -o Acquire::ForceIPv4=true update

To make the setting persistent, create the file 99force-ipv4 in /etc/apt/apt.conf.d/.

sudoedit /etc/apt/apt.conf.d/99force-ipv4

Put the following contents in it:

Acquire::ForceIPv4 "true";

Save the file, and you're good to go. If you want to force IPv6 instead, change the 4 to 6 in the file name and settings. To choose between one of them, test with the fast method and see which works better.