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.
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
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.
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.
E: Could not connect to proxy server
Could not connect to proxy server
E: Package 'package:i386' has no installation candidate
How to fix "Package package:i386 has no installation candidate" in apt
E: The value 'value' is invalid for APT::Default-Release
How to fix invalid APT::Default-Release value in APT
dpkg: error: unable to create new file 'path': Permission denied
How to fix dpkg permission denied errors in APT
subprocess installed post-removal script returned error exit status 1
How to fix "subprocess installed post-removal script returned error exit status 1" in APT