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/apt 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:
Tutorial details
DifficultyEasy (rss)
Root privilegesYes
RequirementsDebian/Ubuntu/Mint & Friends
Time2m

ADVERTISEMENTS

  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

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 security.ubuntu.com (2001:67c:1560:8001::14). In this quick tutorial, you will learn how to use apt-get command IPv4 instead of IPv6 address.

Syntax to force apt-get with IPv4/IPv6 Transport

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

Forcing IPv4 transport with apt-get

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

Forcing IPv6 transport with apt-get

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 http://mirrors.linode.com/ubuntu xenial InRelease
Hit:2 http://mirrors.linode.com/ubuntu xenial-updates InRelease
Hit:3 http://mirrors.linode.com/ubuntu xenial-backports InRelease
Hit:4 http://security.ubuntu.com/ubuntu 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
  vim-tiny
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 http://mirrors.linode.com/ubuntu xenial-updates/main amd64 base-files amd64 9.4ubuntu4.3 [67.7 kB]
Get:2 http://mirrors.linode.com/ubuntu 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) ...

Conclusion

You learned how to force the apt-get command/apt command to use either IPv4 or IPv6 transport using various options. A properly configured server with DNS resolution can handle both IPv4 and IPv6 networking in dual-stack mode. Hence it would be best if you found out the root cause of such failures.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
3 comments… add one
  • home account Sep 16, 2015 @ 15:19

    s/IPv5/IPv4/

    • 🐧 Vivek Gite Sep 16, 2015 @ 16:44

      Thanks for the heads up!

      • Aram Iskenderian Sep 21, 2015 @ 23:30

        Hi Vivek,

        It is still not fixed.
        The last section is discussing how to force IPv4, while the last bolded text says IPv6.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.