How To Use apt-get with IPv4 or IPv6 Transport (address) on a Ubuntu or Debian or Mint Linux

It is possible that I can forces my apt-get command command only use IPv6 or Ipv4 transport on a Debian or Ubuntu Linux server?

Yes. You need to configure the acquire group option on a Debian or Ubuntu Linux. The Acquire group of options controls the download of packages as well as the various “acquire methods” responsible for the download itself as follows:[donotprint][/donotprint]

  1. ForceIPv4 : When downloading, force to use only the IPv4 protocol (must set to true).
  2. ForceIPv6 : When downloading, force to use only the IPv6 protocol (must set to true).

Of course you need properly configured IPv6 based system with support from your router and ISP too otherwise you might see or have to wait for a some timeout whenever you are try downloading updates:

Fig.01: How to force apt-get to use IPv4 or IPv6 on Ubuntu or Debian Linux to avoid timeouts

My apt-get session hangs out as I am unable to reach to IPv6 address of (2001:67c:1560:8001::14). In this quick tutorial, you will learn how to use apt-get command IPv4 instead of IPv6 address.


The apt-get syntax for the Acquire group is:

apt-get -o Acquire::option command 
apt-get -o Acquire::option=value command 
apt-get -o Acquire::option=value install pkg1
apt-get -o Acquire::option=value update
apt-get -o Acquire::option=value upgrade
apt-get -o Acquire::option=value dist-upgrade


In this example, force IPv4 transport:

sudo apt-get -o Acquire::ForceIPv4=true install pkg
sudo apt-get -o Acquire::ForceIPv4=true update
sudo apt-get -o Acquire::ForceIPv4=true upgrade
sudo apt-get -o Acquire::ForceIPv4=true dist-upgrade
sudo apt-get -o Acquire::ForceIPv4=true install ksh

In this example, force IPv6 transport:

sudo apt-get -o Acquire::ForceIPv6=true install pkg
sudo apt-get -o Acquire::ForceIPv6=true update
sudo apt-get -o Acquire::ForceIPv6=true upgrade
sudo apt-get -o Acquire::ForceIPv6=true dist-upgrade
sudo apt-get -o Acquire::ForceIPv6=true install ksh

Persistent option with bash shell aliases

Add the following bash shell aliases option to your ~/.bashrc file:

## Always use IPv6 ##
alias apt-get='sudo apt-get -o Acquire::ForceIPv6=true'
## Example for IPv4 transport 
# alias apt-get='sudo apt-get -o Acquire::ForceIPv4=true'

Persistent IPv6 transport option with apt-get config file

Create the following file /etc/apt/apt.conf.d/1000-force-ipv6-transport using vi text editor:
$ sudo vi /etc/apt/apt.conf.d/1000-force-ipv6-transport
Append the following option:

Acquire::ForceIPv6 "true";

Save and close the file.

Persistent IPv4 transport option with apt-get config file

Create the following file /etc/apt/apt.conf.d/1000-force-ipv4-transport using vi text editor:
$ sudo vi /etc/apt/apt.conf.d/1000-force-ipv4-transport
Append the following option:

Acquire::ForceIPv4 "true";

Save and close the file.

Test it

Type the following apt-get command or apt command as follows:
$ sudo apt-get update
Sample outputs:

Hit:1 xenial InRelease
Hit:2 xenial-updates InRelease
Hit:3 xenial-backports InRelease
Hit:4 xenial-security InRelease               
Reading package lists... Done

$ sudo apt-get upgrade
Sample outputs:

Calculating upgrade... Done
The following packages will be upgraded:
  accountsservice apparmor apt apt-transport-https apt-utils base-files bind9-host curl dbus distro-info-data dnsutils gcc-5-base grub-common grub-pc grub-pc-bin
  grub2-common ifupdown init init-system-helpers initramfs-tools initramfs-tools-bin initramfs-tools-core isc-dhcp-client isc-dhcp-common kbd klibc-utils language-pack-en
  language-pack-gnome-en libaccountsservice0 libapparmor-perl libapparmor1 libapt-inst2.0 libapt-pkg5.0 libbind9-140 libc-bin libc6 libcurl3-gnutls libdbus-1-3
  libdns-export162 libdns162 libisc-export160 libisc160 libisccc140 libisccfg140 libklibc liblwres141 libpam-systemd libprocps4 libpython2.7-minimal libpython2.7-stdlib
  libpython3.5-minimal libpython3.5-stdlib libssl1.0.0 libstdc++6 libsystemd0 libudev1 locales multiarch-support openssl procps python2.7 python2.7-minimal
  python3-distupgrade python3-update-manager python3.5 python3.5-minimal systemd systemd-sysv tar tzdata ubuntu-release-upgrader-core udev update-manager-core vim-common
75 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 32.9 MB of archives.
After this operation, 2,193 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 xenial-updates/main amd64 base-files amd64 9.4ubuntu4.3 [67.7 kB]
Get:2 xenial-updates/main amd64 init-system-helpers all 1.29ubuntu3 [32.4 kB]
Setting up ubuntu-release-upgrader-core (1:16.04.20) ...
Setting up update-manager-core (1:16.04.5) ...
Processing triggers for libc-bin (2.23-0ubuntu5) ...
Processing triggers for initramfs-tools (0.122ubuntu8.8) ...
update-initramfs: Generating /boot/initrd.img-4.4.0-36-generic
Processing triggers for systemd (229-4ubuntu13) ...
Processing triggers for ureadahead (0.100.0-19) ...

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.