How to fix E: Could not resolve hostname in APT
This error occurs when apt cannot translate a hostname (like archive.ubuntu.com) into an IP address, typically due to DNS misconfiguration, network issues, or lack of connectivity. Resolving it involves checking your network connection and DNS settings.
Quick fix
Fixes E: Could not resolve hostname
ping -c 4 8.8.8.8- Verify your internet connection
- Check your current DNS configuration
- Add reliable DNS servers (quick fix for /etc/resolv.conf)
- Configure DNS permanently (systemd-resolved)
ping -c 4 8.8.8.8E: Could not resolve hostname
What this error means
When you run `apt update` or `apt install`, the system needs to resolve package repository hostnames to IP addresses using DNS (Domain Name System). The "Could not resolve hostname" error means apt attempted to reach a repository server but failed to translate its hostname into an IP address. This typically happens because: 1. Your system has no valid DNS servers configured 2. The DNS server you're using is unreachable or misconfigured 3. Your network connection is down or unstable 4. You're behind a proxy server that's blocking DNS traffic 5. The /etc/hosts file is missing critical entries for your system
Typical symptoms
- apt update fails immediately with hostname resolution error
- apt install commands cannot proceed to download packages
- Only apt fails but other programs like ping or curl work
- DNS works for some services but not for apt
- Error specifically mentions archive.ubuntu.com, deb.debian.org, or similar repository domains
Common causes
- No DNS servers configured in /etc/resolv.conf (most common in fresh installs or containers)
- DNS servers are unreachable, slow, or misconfigured by your ISP
- Network connectivity is down or unstable
- Hostname is not properly set in /etc/hosts file
- systemd-resolved service is not running or incorrectly configured
- Firewall or proxy is blocking DNS traffic (port 53)
How to fix "E: Could not resolve hostname"
Start by checking if your network is working at all:
ping -c 4 8.8.8.8If this fails, your network is down. Check your network adapter status:
ip link show
ip addr showIf the network is up but apt fails, proceed to the next step (DNS issue).
View your current DNS settings:
cat /etc/resolv.confYou should see at least one nameserver line with an IP address (e.g., nameserver 8.8.8.8). If the file is empty or missing, or if you only see nameserver 127.0.0.53 without a working systemd-resolved, that's your problem.
For modern systems using systemd-resolved, check its status:
sudo systemctl status systemd-resolvedEdit the resolv.conf file to add public DNS servers:
sudo nano /etc/resolv.confAdd or replace the nameserver lines with:
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.1.1.1Save and exit (Ctrl+X, then Y, then Enter). Test immediately:
sudo apt updateNote: This change is temporary. On many systems, /etc/resolv.conf is regenerated, so changes may not persist after reboot.
For a permanent fix on systems using systemd-resolved (Ubuntu 18.04+, Debian 11+):
sudo nano /etc/systemd/resolved.confFind or add the DNS= line and configure it:
DNS=8.8.8.8 8.8.4.4 1.1.1.1
FallbackDNS=9.9.9.9Save and restart the service:
sudo systemctl restart systemd-resolvedVerify the change took effect:
systemd-resolve --status
sudo apt updateCheck if your system's hostname is properly configured:
cat /etc/hosts
hostnameYour /etc/hosts should have entries like:
127.0.0.1 localhost
127.0.0.1 my-hostname
::1 localhost my-hostnameIf your hostname is missing, add it:
sudo nano /etc/hostsAdd a line matching your hostname (get it from hostname command).
After making changes, clear any cached DNS data and try apt again:
sudo systemd-resolve --flush-caches
sudo apt updateIf you're using a different DNS system, the cache flush command may differ.
Docker containers and Windows Subsystem for Linux (WSL) have special network configurations:
Docker: If apt fails inside a container, the Docker host's DNS is likely the issue. Pass DNS to the container:
docker run --dns 8.8.8.8 --dns 8.8.4.4 <image> apt updateOr in docker-compose.yml:
services:
myservice:
dns:
- 8.8.8.8
- 8.8.4.4WSL 2: Edit /etc/resolv.conf inside the WSL distro and add public DNS servers. Changes may not persist on reboot—use wsl.conf for permanent configuration.
Advanced notes
systemd-resolved vs traditional DNS: Modern Linux systems use systemd-resolved as the default DNS resolver. If systemd-resolved is not running, /etc/resolv.conf may be a static file or symlink. Always check what DNS system your distro uses.
ISP DNS vs Public DNS: Some ISP DNS servers are slow or unreliable. Public DNS like Google's (8.8.8.8) or Cloudflare's (1.1.1.1) are more reliable but may be blocked in some corporate networks.
Persistent changes: Direct edits to /etc/resolv.conf are overwritten by network managers (NetworkManager, systemd-resolved, dhclient). Use the appropriate configuration tool for your system for permanent changes.
Behind a proxy: If your organization uses a proxy server, apt may need proxy configuration separate from DNS. Check /etc/apt/apt.conf or /etc/apt/apt.conf.d/ for proxy settings.
Container/VM-specific: Virtual machines and containers inherit DNS from the host. If apt fails in a container but works on the host, the container's networking layer needs adjustment.
Frequently asked questions
- What does "E: Could not resolve hostname" mean?
- When you run apt update or apt install, the system needs to resolve package repository hostnames to IP addresses using DNS (Domain Name System). The "Could not resolve hostname" error means apt attempted to reach a repository server but failed to translate its hostname into an IP address. This typically happens because: 1.
- How do I fix "E: Could not resolve hostname"?
- This error occurs when apt cannot translate a hostname (like archive.ubuntu.com) into an IP address, typically due to DNS misconfiguration, network issues, or lack of connectivity. Resolving it involves checking your network connection and DNS settings.
- What causes "E: Could not resolve hostname"?
- No DNS servers configured in /etc/resolv.conf (most common in fresh installs or containers) DNS servers are unreachable, slow, or misconfigured by your ISP Network connectivity is down or unstable
- Is the "E: Could not resolve hostname" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚APT Official Documentation
- 📖Ubuntu man pages: resolv.conf(5)(Official Docs)
- 📖Debian Wiki: DNS Configuration(Official Docs)