Operating System Debian

Force APT to Use IPv4 or IPv6 on Ubuntu 26.04 and Debian 13

Fix slow or hanging apt updates on dual-stack servers by forcing APT to IPv4 or IPv6, either for a single command or permanently.

When a server has both an IPv4 and an IPv6 address, APT connects to package mirrors over whichever protocol the system resolver prefers, usually IPv6. If the IPv6 route is broken or slow, apt update appears to hang on "Connecting to…" or fails with timeouts even though the mirror is reachable over IPv4. This guide shows how to make APT use only IPv4 or only IPv6, for a single command or permanently, on Ubuntu 26.04 LTS and Debian 13. The same options work unchanged on Ubuntu 24.04 LTS and Debian 12.

Requirements

  • A server running Ubuntu 26.04 LTS, Ubuntu 24.04 LTS, Debian 13 or Debian 12.
  • A user with sudo rights.

APT has two configuration options for this, both documented in apt.conf(5): Acquire::ForceIPv4 ("when downloading, force to use only the IPv4 protocol") and Acquire::ForceIPv6, its IPv6 counterpart. They have been part of APT since version 0.9.7.9, so every supported Ubuntu and Debian release has them and there is no version to check first.

Check which protocol is failing

Before forcing anything, confirm that one protocol really is the problem. Test the mirror over each protocol with curl (install it with sudo apt install curl if needed; use archive.ubuntu.com on Ubuntu):

curl -4 -sI --max-time 10 http://deb.debian.org/debian/ | head -n 1
curl -6 -sI --max-time 10 http://deb.debian.org/debian/ | head -n 1

A working protocol prints an HTTP status line. If one of the two commands prints nothing, that protocol cannot reach the mirror from your server.

Force the protocol for a single command

Every APT tool accepts -o to set a configuration option for one run. To update the package lists over IPv4 only:

sudo apt -o Acquire::ForceIPv4=true update

To use IPv6 only:

sudo apt -o Acquire::ForceIPv6=true update

The option works the same way with apt-get and with any subcommand that downloads, for example:

sudo apt-get -o Acquire::ForceIPv4=true dist-upgrade

This is the right method for a one-off fix or for finding out which protocol is faster, because nothing is changed permanently.

Make the setting permanent

APT reads every file in /etc/apt/apt.conf.d/ in alphanumeric order; the file name may contain only letters, digits, hyphens, underscores and periods, and must have either no extension or the extension .conf. Create a file for the setting:

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

To force IPv6 instead, use the other option and a matching file name:

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

Create only one of the two files. The setting applies to everything that downloads through APT's own methods on that machine: apt, apt-get, and automatic updates through unattended-upgrades.

Verify the setting

Ask APT to print its effective configuration:

apt-config dump | grep -i force

With the IPv4 file in place the output contains:

Acquire::ForceIPv4 "true";

Then run an update and look at the addresses APT reports. With IPv4 forced, a slow or failing connection shows an IPv4 address rather than an IPv6 one in the "Connecting to" progress line:

sudo apt update

Undo the change

Delete the file you created and APT goes back to the system's default address selection:

sudo rm /etc/apt/apt.conf.d/99force-ipv4

If you only need to override a permanent setting once, pass the opposite value on the command line; command-line options are applied after the configuration files:

sudo apt -o Acquire::ForceIPv4=false update

Troubleshooting

  • APT ignores the file. Check the file name: a name with another extension, such as .bak or .txt, is skipped. Also check the syntax: the value is in double quotes and the line ends with a semicolon.
  • Downloads fail completely after forcing a protocol. Some mirrors and third-party repositories publish only IPv4 addresses. With ForceIPv6 set, those sources cannot be reached at all; force IPv4 instead or remove the setting.
  • The delay is in name resolution, not in connecting. These options change which address family APT connects to, not how DNS lookups are sent. If the pause happens before "Connecting to", check the resolvers in /etc/resolv.conf.
  • A proxy is configured. When Acquire::http::Proxy is set, APT connects to the proxy, and the proxy decides which protocol is used to reach the mirror.

Frequently asked questions

Should I force IPv4 permanently?

Only as a workaround. The real problem is broken IPv6 connectivity on the server, and it affects every other program too. If IPv6 is assigned but does not work, fix the address, gateway or firewall configuration, or ask your provider; then remove the APT setting.

Can I make the whole system prefer IPv4 instead of just APT?

Yes. The address order returned by the C library's resolver is controlled by /etc/gai.conf. Uncommenting the line precedence ::ffff:0:0/96 100 in that file makes programs that follow this order, APT included, try IPv4 first while still falling back to IPv6. Unlike ForceIPv4, this is a preference, not a restriction.

Does this affect snap, pip or Docker downloads?

No. Acquire::ForceIPv4 and Acquire::ForceIPv6 are APT options and are read only by APT's download methods. Other tools have their own switches, such as curl -4 or wget -4.

Is there a shorter form than -o Acquire::ForceIPv4=true?

The -o form is the one documented in the APT manual pages and works with every APT tool, so it is the safe one to use in scripts. If you type it often, the permanent file is less work than a long command line.

Share story:

Continue reading other guides on How VPS.