Opublikowany 2024-05-08 06:49:06 UTC przez: Redaktor

Ustawienie apt-get w sposób, by pobierał pakiety przez IPv4 or IPv6

To prevent timeouts on a Debian, Ubuntu, or Mint Linux, you can set the ForceIPv4 and ForceIPv6 variables to true, which will force the APT or APT-GET command to use either IPv4 or IPv6 network transport IP address. This can be done through the CLI, and we will now explain how to do so on Ubuntu or Debian Linux.

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

 

  1. ForceIPv4 : When downloading APT packages, force to use only the IPv4 protocol and must set to true.
  2. ForceIPv6 : When downloading APT-GET packages, force to use only the IPv6 protocol and 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 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.
Tutorial details Difficulty level Easy Root privileges Yes Requirements Linux terminal Category Package Manager Prerequisites apt, apt-get command OS compatibility Debian • Mint • Pop!_OS • Ubuntu Est. reading time 4 minutes

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 for apt-get:

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.

Forcing apt-get with IPv4/IPv6 and test it as follows

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
Here is what I see:

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. See the following manual pages using the help command or man command. For example:

man apt
man apt-get

Powrót do Listy artykułów