Khắc phục apt update treo do lỗi DNS với systemd-resolved

Vấn đề ban đầu

apt update bị treo do không phân giải được DNS, cụ thể là các host như deb.debian.orgdownload.docker.com.

Các bước đã thực hiện

Bước 1 - Cấu hình DNS tạm để có thể kết nối internet

# Xóa symlink hỏng (nếu có)
rm -f /etc/resolv.conf

# Gán DNS tạm thời trực tiếp vào file (có hỗ trợ IPv6)
cat > /etc/resolv.conf << 'EOF'
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 2001:4860:4860::8888
nameserver 2001:4860:4860::8844
EOF

Bước này đảm bảo máy có thể ra ngoài internet trong khi chưa có DNS service nào chạy.

Bước 2 - Cài đặt systemd-resolved

apt install systemd-resolved -y
rm /etc/resolv.conf
ln -s /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf

Symlink này tự động trỏ về 127.0.0.53 (stub resolver của systemd) và ::1, đây là cách Debian khuyến nghị.

Bước 4 - Bật systemd-resolved

systemctl enable --now systemd-resolved

Bước 5 - Cấu hình DNS upstream cho systemd-resolved (có hỗ trợ IPv6)

cat > /etc/systemd/resolved.conf << 'EOF'
[Resolve]
DNS=8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844
FallbackDNS=1.1.1.1 1.0.0.1 2606:4700:4700::1111 2606:4700:4700::1001
DNSStubListener=yes
EOF

DNS IPv6 sử dụng:

  • Google: 2001:4860:4860::8888, 2001:4860:4860::8844
  • Cloudflare: 2606:4700:4700::1111, 2606:4700:4700::1001

Bước 6 - Restart service và kiểm tra

systemctl restart systemd-resolved

# Kiểm tra DNS đã nhận IPv6 chưa
resolvectl status

apt update  # ✅ Thành công

Kết quả

/etc/resolv.conf trỏ về 127.0.0.53::1 (stub của systemd-resolved), sau đó systemd-resolved forward truy vấn lên cả IPv4 (8.8.8.8) và IPv6 (2001:4860:4860::8888) nên apt update hoạt động bình thường.