How to fix W: Failed to fetch URL Could not connect to hostname:port in APT

APTINTERMEDIATEHIGHUpdated Dec 29, 2025ยท 151 views

This error occurs when apt cannot establish a connection to a package repository server. It usually stems from network issues, DNS problems, or repository server unavailability and can be resolved by checking connectivity and updating repository configurations.

Quick fix

Fixes W: Failed to fetch URL Could not connect to hostname:port

ping -c 4 google.com
  1. Check basic internet connectivity
  2. Verify DNS resolution
  3. Force IPv4 and bypass IPv6 issues
  4. Check firewall rules
apt โ€” error
ping -c 4 google.com
W: Failed to fetch URL Could not connect to hostname:port

What this error means

The "Could not connect to hostname" error appears when apt attempts to fetch package lists or updates from configured repositories but fails to establish a TCP connection to the repository server. This typically means: - The system cannot reach the server's IP address at the specified port (usually 80 for HTTP or 443 for HTTPS) - DNS resolution may have succeeded (the hostname was converted to an IP), but the actual connection failed - Network routing, firewall rules, or the repository server itself may be blocking the connection The error is usually a symptom of underlying network or configuration issues rather than a problem with apt itself.

Typical symptoms

  • apt update or apt install commands hang or fail with connection errors
  • Error messages showing specific hostnames or IPs with port numbers (usually :80 or :443)
  • Some repositories may work while others fail
  • Warnings appear but apt continues with degraded functionality
  • Docker builds fail when running apt commands in containers
  • CI/CD pipelines fail during the package installation step

Common causes

  1. Network connectivity is down or unstable (WiFi drops, no internet access)
  2. DNS resolution is misconfigured or your DNS servers are unavailable
  3. Repository mirror server is temporarily offline or unreachable
  4. Firewall (ufw, iptables) is blocking outbound connections on ports 80 or 443
  5. Proxy server between your system and the internet requires authentication
  6. IPv6 connectivity issues causing unstable or failed connections
  7. Corporate network or ISP blocking access to specific repository domains

How to fix "W: Failed to fetch URL Could not connect to hostname:port"

1Check basic internet connectivity

First, verify your system can reach the internet:

bash
ping -c 4 google.com

If ping fails:
- Check your network interface is up: ip link show
- Check you have an IP address: ip addr show
- Restart networking: sudo systemctl restart networking (or NetworkManager if using NetworkManager)
- Check WiFi/ethernet is connected

If ping works but apt still fails, move to the next step.

2Verify DNS resolution

DNS resolution converts repository hostnames to IP addresses. Test it:

bash
nslookup archive.ubuntu.com
# or
host archive.ubuntu.com
# or
dig archive.ubuntu.com

If DNS fails:
1. Check your current DNS servers:

bash
cat /etc/resolv.conf

2. Try using a public DNS server (Google's 8.8.8.8 or Cloudflare's 1.1.1.1):

bash
sudo nano /etc/resolv.conf
   # Add or replace with:
   nameserver 8.8.8.8
   nameserver 1.1.1.1

3. Test DNS again after updating the file.

3Force IPv4 and bypass IPv6 issues

Some systems have broken IPv6 support. Force apt to use IPv4 only:

bash
sudo apt-get -o Acquire::ForceIPv4=true update

If this works, make it permanent:

bash
echo 'Acquire::ForceIPv4 "true";' | sudo tee /etc/apt/apt.conf.d/99-ipv4

Then try updating again:

bash
sudo apt update
4Check firewall rules

Ensure your firewall allows outbound HTTP and HTTPS connections:

bash
sudo ufw status

If ufw is active, allow outbound traffic on ports 80 and 443:

bash
sudo ufw allow out 80/tcp
sudo ufw allow out 443/tcp

If using iptables directly:

bash
sudo iptables -L -n
# Check OUTPUT chain rules; ensure they allow port 80/443 or are set to ACCEPT by default

After updating firewall rules, test apt again.

5Clean apt cache and retry

Sometimes corrupted cache causes issues. Clean and retry:

bash
sudo apt clean
sudo apt autoclean
sudo apt update

The apt clean command removes all cached package files, forcing apt to re-download fresh package lists from repositories.

6Switch to a different repository mirror

If the default mirror is down, try an alternative:

bash
sudo nano /etc/apt/sources.list

If using Ubuntu, you can replace the mirror URL. For example, change:

bash
deb http://archive.ubuntu.com/ubuntu/ jammy main restricted

To use a different region mirror (e.g., your country's mirror). Check [Ubuntu Mirror List](https://launchpad.net/ubuntu/+archivemirrors) for available mirrors.

After editing, test:

bash
sudo apt update
7Configure proxy settings if behind a corporate proxy

If your network requires a proxy, configure apt:

bash
sudo nano /etc/apt/apt.conf.d/proxy.conf

Add your proxy details:

bash
Acquire::http::Proxy "http://proxy.example.com:8080/";
Acquire::https::Proxy "http://proxy.example.com:8080/";

For authenticated proxies:

bash
Acquire::http::Proxy "http://username:password@proxy.example.com:8080/";

Save the file and retry:

bash
sudo apt update

Advanced notes

For container deployments (Docker/Kubernetes):
- Use apt-get update -y with a retry mechanism in Dockerfiles
- Consider using a package cache or local mirror for faster builds
- Use --fix-missing flag with caution: apt-get install --fix-missing

For systems behind proxies or firewalls:
- Test connectivity to specific repositories: curl -v http://archive.ubuntu.com/
- Use apt-helper wait-online to wait for network readiness before running apt
- Check if your organization's DNS or proxy is intercepting repository connections

For servers with IPv6 disabled:
- Disabling IPv6 can sometimes cause APT issues; verify with ip addr show and check if IPv6 addresses exist

Frequently asked questions

What does "W: Failed to fetch URL Could not connect to hostname:port" mean?
The "Could not connect to hostname" error appears when apt attempts to fetch package lists or updates from configured repositories but fails to establish a TCP connection to the repository server. This typically means: - The system cannot reach the server's IP address at the specified port (usually 80 for HTTP or 443 for HTTPS) - DNS resolution may have succeeded (the hostname was converted to an IP), but the actual connection failed - Network routing, firewall rules, or the repository server itself may be blocking the connection The error is usually a symptom of underlying network or configuration issues rather than a problem with apt itself.
How do I fix "W: Failed to fetch URL Could not connect to hostname:port"?
This error occurs when apt cannot establish a connection to a package repository server. It usually stems from network issues, DNS problems, or repository server unavailability and can be resolved by checking connectivity and updating repository configurations.
What causes "W: Failed to fetch URL Could not connect to hostname:port"?
Network connectivity is down or unstable (WiFi drops, no internet access) DNS resolution is misconfigured or your DNS servers are unavailable Repository mirror server is temporarily offline or unreachable
Is the "W: Failed to fetch URL Could not connect to hostname:port" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading