Force apt-get to use IPv4 or IPv6 on Ubuntu

Occasionally I have found myself unable to install updates after a fresh Ubuntu server install because running apt-get update or apt-get upgrade hangs when attempting to resolve the IPv6 address of the update repos. It seems it defaults to IPv6, instead of IPv4… but I guess that’s the price you pay for trying to usher the future in. I don’t mind 🙂

This hasn’t been an issue with any other IPv6-compatible devices, such as my Android phone or Windows PCs/VMs, and frankly it’s not important enough to diagnose and fix, since my IPv6 network is used only for browsing, and barely at that. Here’s a quick workaround that forces IPV4 connectivity through apt-get on a temporary or a permanent basis:

Temporary/one time use:

~$ apt-get -o Acquire::ForceIPv4=true <rest of commands go here>

example:

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

The above command will run an apt-get update command while forcing IPv4 DNS resolution.

 

To make this change permanent, a new file must be created in /etc/apt/apt.conf.d/:

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

Enter the following in the editor:

Acquire::ForceIPv4 "true";

Press Ctrl-O to save the file, confirm, and press Ctrl-X to exit the editor. No reboot necessary – the next time apt-get runs, it will default to IPv4.

If you’d like to enforce IPv6 DNS resolution instead of IPv4, change the entries above from a 4 to a 6.