How to fix E: Could not resolve hostname in APT

APTBEGINNERMEDIUMUpdated Dec 29, 2025· 156 views

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
  1. Verify your internet connection
  2. Check your current DNS configuration
  3. Add reliable DNS servers (quick fix for /etc/resolv.conf)
  4. Configure DNS permanently (systemd-resolved)
apt — error
ping -c 4 8.8.8.8
E: 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

  1. No DNS servers configured in /etc/resolv.conf (most common in fresh installs or containers)
  2. DNS servers are unreachable, slow, or misconfigured by your ISP
  3. Network connectivity is down or unstable
  4. Hostname is not properly set in /etc/hosts file
  5. systemd-resolved service is not running or incorrectly configured
  6. Firewall or proxy is blocking DNS traffic (port 53)

How to fix "E: Could not resolve hostname"

1Verify your internet connection

Start by checking if your network is working at all:

bash
ping -c 4 8.8.8.8

If this fails, your network is down. Check your network adapter status:

bash
ip link show
ip addr show

If the network is up but apt fails, proceed to the next step (DNS issue).

2Check your current DNS configuration

View your current DNS settings:

bash
cat /etc/resolv.conf

You 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:

bash
sudo systemctl status systemd-resolved
3Add reliable DNS servers (quick fix for /etc/resolv.conf)

Edit the resolv.conf file to add public DNS servers:

bash
sudo nano /etc/resolv.conf

Add or replace the nameserver lines with:

bash
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.1.1.1

Save and exit (Ctrl+X, then Y, then Enter). Test immediately:

bash
sudo apt update

Note: This change is temporary. On many systems, /etc/resolv.conf is regenerated, so changes may not persist after reboot.

4Configure DNS permanently (systemd-resolved)

For a permanent fix on systems using systemd-resolved (Ubuntu 18.04+, Debian 11+):

bash
sudo nano /etc/systemd/resolved.conf

Find or add the DNS= line and configure it:

bash
DNS=8.8.8.8 8.8.4.4 1.1.1.1
FallbackDNS=9.9.9.9

Save and restart the service:

bash
sudo systemctl restart systemd-resolved

Verify the change took effect:

bash
systemd-resolve --status
sudo apt update
5Verify /etc/hosts file (if system hostname resolution fails)

Check if your system's hostname is properly configured:

bash
cat /etc/hosts
hostname

Your /etc/hosts should have entries like:

bash
127.0.0.1 localhost
127.0.0.1 my-hostname
::1 localhost my-hostname

If your hostname is missing, add it:

bash
sudo nano /etc/hosts

Add a line matching your hostname (get it from hostname command).

6Flush DNS cache and retry

After making changes, clear any cached DNS data and try apt again:

bash
sudo systemd-resolve --flush-caches
sudo apt update

If you're using a different DNS system, the cache flush command may differ.

7For Docker or WSL environments

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:

bash
docker run --dns 8.8.8.8 --dns 8.8.4.4 <image> apt update

Or in docker-compose.yml:

yaml
services:
  myservice:
    dns:
      - 8.8.8.8
      - 8.8.4.4

WSL 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