Skip to content

Nuface Blog

隨意隨手記 Casual Notes

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

Ubuntu 關閉 IPv6 與強制 apt 使用 IPv4 的完整指南

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

(適用 Ubuntu 22.04 / 24.04 / 24.10 / 24.04 LTS “Noble”)

在企業或內部環境中,許多網路尚未支援 IPv6,導致 Ubuntu 系統在安裝套件時出現連線錯誤。
例如以下訊息:

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

這表示 apt 嘗試透過 IPv6 連線,但 IPv6 網路不可達,導致安裝或更新中斷。


🔍 問題說明

Ubuntu 預設會:

  1. 啟用 IPv6 網路堆疊;
  2. 在 DNS 查詢時優先使用 IPv6;
  3. apt 也會自動使用 IPv6 位址連線。

當伺服器、網關或防火牆未支援 IPv6 時,就會出現「Network is unreachable」或「Connection timed out」的錯誤。


🧰 解決步驟

步驟 1:永久關閉 IPv6

編輯設定檔:

sudo nano /etc/sysctl.conf

在最後加入以下內容:

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

套用設定:

sudo sysctl -p

確認是否關閉:

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

若輸出為 1,表示 IPv6 已停用。


步驟 2:強制 apt 使用 IPv4

建立設定檔:

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

內容輸入:

Acquire::ForceIPv4 "true";

儲存後執行:

sudo apt clean
sudo apt update

此時 apt 應只會使用 IPv4 位址,例如:

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

步驟 3(選用):讓系統整體偏好 IPv4

編輯 /etc/gai.conf:

sudo nano /etc/gai.conf

找到下列設定並取消註解(移除 #):

precedence ::ffff:0:0/96  100

這會讓系統在所有網路連線中優先使用 IPv4。


✅ 驗證方式

  1. 確認 IPv6 已停用: cat /proc/sys/net/ipv6/conf/all/disable_ipv6 顯示 1 即成功。
  2. 確認 DNS 僅回傳 IPv4: getent ahosts archive.ubuntu.com 若未出現開頭為 2620: 的位址(IPv6),則設定完成。
  3. 驗證 apt: sudo apt update 若更新正常且無 Network is unreachable,代表設定成功。

📘 總結

項目動作
關閉 IPv6編輯 /etc/sysctl.conf
強制 apt 使用 IPv4新增 /etc/apt/apt.conf.d/99force-ipv4
系統層級 IPv4 優先修改 /etc/gai.conf
驗證getent ahosts 與 apt update

💡 建議(企業環境)

在企業網路中,若 IPv6 未啟用或未通過防火牆,建議:

  • 預設停用 IPv6;
  • 對伺服器映像(image)預先加入 99force-ipv4 設定;
  • 於內部 SOP 中明列「Ubuntu IPv6 關閉標準作法」。

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