Skip to content

Nuface Blog

隨意隨手記 Casual Notes

Menu
  • Home
  • About
  • Services
  • Blog
  • Contact
  • Privacy Policy
  • Login
Menu

Ubuntu — Disable IPv6 and Force APT to Use IPv4

Posted on 2025-11-102025-11-10 by Rico

(For Ubuntu 22.04 / 24.04 / 24.10 / 24.04 LTS “Noble”)

In many enterprise or internal environments, IPv6 is not yet supported.
This often causes apt to fail when trying to download packages, showing messages like:

Cannot initiate the connection to archive.ubuntu.com:80 (2620:2d:4000:1::102).
Network is unreachable

This means Ubuntu tried to connect over IPv6, but the network doesn’t support it.


🔍 Problem Description

By default, Ubuntu:

  1. Enables IPv6 globally
  2. Prefers IPv6 in DNS resolution
  3. Lets apt connect using IPv6 first

When your network or firewall does not allow IPv6 traffic, apt fails with connection errors.


🧰 Step-by-Step Solution

Step 1: Permanently Disable IPv6

Edit the sysctl configuration:

sudo nano /etc/sysctl.conf

Add these lines at the end:

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1

Apply changes:

sudo sysctl -p

Check the status:

cat /proc/sys/net/ipv6/conf/all/disable_ipv6

If you see 1, IPv6 is disabled.


Step 2: Force APT to Use IPv4

Create a config file:

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

Insert:

Acquire::ForceIPv4 "true";

Save and run:

sudo apt clean
sudo apt update

Now apt will connect via IPv4 only:

Hit:1 http://archive.ubuntu.com/ubuntu noble InRelease

Step 3 (Optional): Prefer IPv4 for All Connections

Edit /etc/gai.conf:

sudo nano /etc/gai.conf

Uncomment this line:

precedence ::ffff:0:0/96  100

This forces the system to prefer IPv4 over IPv6 globally.


✅ Verification

  1. Check IPv6 status: cat /proc/sys/net/ipv6/conf/all/disable_ipv6 Should return 1.
  2. Verify DNS uses IPv4 only: getent ahosts archive.ubuntu.com No 2620: entries = IPv6 disabled.
  3. Run update test: sudo apt update Should complete without “Network is unreachable”.

📘 Summary

TaskFile / Command
Disable IPv6/etc/sysctl.conf
Force APT IPv4/etc/apt/apt.conf.d/99force-ipv4
System IPv4 Priority/etc/gai.conf
Verifygetent ahosts, apt update

💡 Recommendation for IT Admins

In enterprise environments where IPv6 is not supported:

  • Disable IPv6 in system images
  • Add 99force-ipv4 to default configuration
  • Include these steps in your internal Ubuntu deployment SOP

Recent Posts

  • Postfix + Let’s Encrypt + BIND9 + DANE Fully Automated TLSA Update Guide
  • Postfix + Let’s Encrypt + BIND9 + DANE TLSA 指紋自動更新完整教學
  • Deploying DANE in Postfix
  • 如何在 Postfix 中部署 DANE
  • DANE: DNSSEC-Based TLS Protection

Recent Comments

  1. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on High Availability Architecture, Failover, GeoDNS, Monitoring, and Email Abuse Automation (SOAR)
  2. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on MariaDB + PostfixAdmin: The Core of Virtual Domain & Mailbox Management
  3. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Daily Operations, Monitoring, and Performance Tuning for an Enterprise Mail System
  4. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Final Chapter: Complete Troubleshooting Guide & Frequently Asked Questions (FAQ)
  5. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Network Architecture, DNS Configuration, TLS Design, and Postfix/Dovecot SNI Explained

Archives

  • December 2025
  • November 2025
  • October 2025

Categories

  • AI
  • Apache
  • Cybersecurity
  • Database
  • DNS
  • Docker
  • Fail2Ban
  • FileSystem
  • Firewall
  • Linux
  • LLM
  • Mail
  • N8N
  • OpenLdap
  • OPNsense
  • PHP
  • QoS
  • Samba
  • Switch
  • Virtualization
  • VPN
  • WordPress
© 2025 Nuface Blog | Powered by Superbs Personal Blog theme