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.
Fixes W: Failed to fetch URL Could not connect to hostname:port
ping -c 4 google.comping -c 4 google.comW: Failed to fetch URL Could not connect to hostname:port
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.
First, verify your system can reach the internet:
ping -c 4 google.comIf 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.
DNS resolution converts repository hostnames to IP addresses. Test it:
nslookup archive.ubuntu.com
# or
host archive.ubuntu.com
# or
dig archive.ubuntu.comIf DNS fails:
1. Check your current DNS servers:
cat /etc/resolv.conf2. Try using a public DNS server (Google's 8.8.8.8 or Cloudflare's 1.1.1.1):
sudo nano /etc/resolv.conf
# Add or replace with:
nameserver 8.8.8.8
nameserver 1.1.1.13. Test DNS again after updating the file.
Some systems have broken IPv6 support. Force apt to use IPv4 only:
sudo apt-get -o Acquire::ForceIPv4=true updateIf this works, make it permanent:
echo 'Acquire::ForceIPv4 "true";' | sudo tee /etc/apt/apt.conf.d/99-ipv4Then try updating again:
sudo apt updateEnsure your firewall allows outbound HTTP and HTTPS connections:
sudo ufw statusIf ufw is active, allow outbound traffic on ports 80 and 443:
sudo ufw allow out 80/tcp
sudo ufw allow out 443/tcpIf using iptables directly:
sudo iptables -L -n
# Check OUTPUT chain rules; ensure they allow port 80/443 or are set to ACCEPT by defaultAfter updating firewall rules, test apt again.
Sometimes corrupted cache causes issues. Clean and retry:
sudo apt clean
sudo apt autoclean
sudo apt updateThe apt clean command removes all cached package files, forcing apt to re-download fresh package lists from repositories.
If the default mirror is down, try an alternative:
sudo nano /etc/apt/sources.listIf using Ubuntu, you can replace the mirror URL. For example, change:
deb http://archive.ubuntu.com/ubuntu/ jammy main restrictedTo 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:
sudo apt updateIf your network requires a proxy, configure apt:
sudo nano /etc/apt/apt.conf.d/proxy.confAdd your proxy details:
Acquire::http::Proxy "http://proxy.example.com:8080/";
Acquire::https::Proxy "http://proxy.example.com:8080/";For authenticated proxies:
Acquire::http::Proxy "http://username:[email protected]:8080/";Save the file and retry:
sudo apt updateFor 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