Force apt-get to use IPv4 or IPv6



  • Non-Persistent Option

    # Force apt-get to use IPv4
    apt-get -o Acquire::ForceIPv4=true update
    
    # Force apt-get to use IPv6
    apt-get -o Acquire::ForceIPv6=true update
    

    Persistent Option

    # /etc/apt/apt.conf.d/99force-IPv4or6
    sudo tee /etc/apt/apt.conf.d/99force-IPv4or6 <<EOF
    Acquire::ForceIPv4 "true";
    EOF
    
    # If you are using the persistent option, you can switch between IPv4 or IPv6 by changing 4 and 6
    
    # This is how you change them using sed
    # IPv4 to IPv6
    sudo sed -i 's/ForceIPv4/ForceIPv6/' /etc/apt/apt.conf.d/99force-ipv4or6
    
    # IPv6 to IPv4
    sudo sed -i 's/ForceIPv6/ForceIPv4/' /etc/apt/apt.conf.d/99force-ipv4or6
    
    # To confirm the changes
    cat /etc/apt/apt.conf.d/99force-ipv4or6
    

    Sources:
    https://www.vultr.com/docs/force-apt-get-to-ipv4-or-ipv6-on-ubuntu-or-debian
    https://askubuntu.com/a/20416