sodcalculator

10 Tech How-To

Fix Could Not Resolve Host Error in Linux

Guide 10-53855 · Updated · Step by step

The “could not resolve host” error in Linux means your system cannot translate a domain name into an IP address, usually due to DNS misconfiguration, network issues, or a faulty hosts file. Fix it by checking your DNS settings, verifying network connectivity, and testing with tools like dig and ping. Below are the most common causes and step-by-step solutions.

What Causes the Error

When you run a command like curl example.com or git clone https://github.com/..., your system asks a DNS resolver to convert the hostname into an IP address. If that lookup fails, you see could not resolve host. The failure can happen at several points:

  • DNS server unreachable – The configured nameserver (e.g., 8.8.8.8) is down or blocked.
  • Wrong DNS settings – /etc/resolv.conf has a bad IP or is overwritten by a network manager.
  • Network interface down – No active connection, Wi-Fi disconnected, or ethernet cable unplugged.
  • Hosts file misconfiguration – A typo in /etc/hosts can redirect a domain to a non-existent IP.
  • IPv6 issues – Some systems try IPv6 first and fail if the router doesn’t handle it correctly.
  • Firewall or proxy – A local firewall or an incorrectly set HTTP proxy can block DNS queries.

Check Your Network Connection First

Before touching DNS, confirm your machine actually has network access. Run ping 8.8.8.8 (Google’s public DNS IP). If that succeeds, you have a route to the internet. Then try ping google.com – if the IP ping works but the hostname ping fails, it’s definitely a DNS problem.

If even the IP ping fails, your network interface is down or misconfigured. Check with ip addr (look for an IP address on your interface) and ip route (should show a default gateway). Restart networking with sudo systemctl restart NetworkManager (or sudo systemctl restart networking on Debian-based systems).

Inspect and Fix DNS Settings

The file /etc/resolv.conf lists your DNS servers. Open it with cat /etc/resolv.conf. You should see lines like nameserver 8.8.8.8. If the file is empty or points to an unreachable IP, edit it (you’ll need root):

  1. Backup the current file: sudo cp /etc/resolv.conf /etc/resolv.conf.bak
  2. Edit with sudo nano /etc/resolv.conf and add a reliable nameserver, e.g., nameserver 8.8.8.8 and nameserver 1.1.1.1.
  3. Save and test with dig google.com (install dnsutils if needed).

Note: On many modern distros, resolv.conf is managed by systemd-resolved or NetworkManager. If your changes get overwritten, you need to configure the network manager instead. For NetworkManager, edit the connection with nmcli con mod "YourConnection" ipv4.dns "8.8.8.8 1.1.1.1" and then restart it.

Test DNS Resolution with dig and nslookup

Once you have a nameserver configured, test it directly:

  • dig google.com – shows the answer section; if you get a status: NOERROR and an IP, DNS works.
  • nslookup google.com – simpler output, but also useful.
  • host google.com – quick and concise.

If dig fails but ping 8.8.8.8 works, your DNS server is unreachable. Try a different public DNS like 1.1.1.1 (Cloudflare) or 9.9.9.9 (Quad9). Sometimes your ISP’s DNS is having issues; switching to a public resolver is a common fix.

Check /etc/hosts and /etc/nsswitch.conf

The /etc/hosts file maps hostnames to IPs locally. If a domain is listed there with a wrong IP, it will override DNS. Check for typos or old entries:

cat /etc/hosts

Look for lines like 127.0.0.1 example.com – that would send all traffic to localhost. Remove or correct any such lines.

Also check /etc/nsswitch.conf – it defines the order of name resolution. The hosts line should read hosts: files dns. If it says hosts: dns files and your DNS is broken, it will fail before trying the hosts file. Change it to files dns to prioritize local entries.

Flush DNS Cache and Restart Services

Your system may have cached a failed lookup. Flush the cache depending on your resolver:

  • systemd-resolved: sudo systemd-resolve --flush-caches (or resolvectl flush-caches on newer versions).
  • dnsmasq: sudo systemctl restart dnsmasq
  • nscd: sudo systemctl restart nscd

After flushing, restart the network manager: sudo systemctl restart NetworkManager. Then test again.

Common Misconceptions and Edge Cases

One common mistake is thinking the error means the website is down. In most cases, it’s your local DNS. Another is overlooking a proxy environment variable. If you have http_proxy or https_proxy set (check with env | grep proxy), your system may try to use a proxy that doesn’t exist, causing resolution failures. Unset them with unset http_proxy https_proxy or fix the proxy address.

Also, some corporate networks require specific DNS servers; using public DNS might be blocked. In that case, ask your network admin for the correct DNS IPs.

Finally, if you’re using a VPN, the VPN’s DNS might be misconfigured. Disconnect the VPN and test – if it works, the VPN is the culprit.

Frequently Asked Questions

Why does “could not resolve host” happen only for some domains?

If some domains resolve but others don’t, it’s usually a DNS filtering issue (e.g., your ISP blocks certain domains) or a typo in the URL. Try dig on the failing domain – if it returns NXDOMAIN, the domain doesn’t exist; if it times out, your DNS server is having trouble with that query.

How do I permanently change DNS on Ubuntu?

On Ubuntu 18.04 and later, edit /etc/netplan/01-netcfg.yaml (or similar) and add nameservers: with addresses: [8.8.8.8, 1.1.1.1]. Then run sudo netplan apply. On older versions, edit /etc/network/interfaces or use NetworkManager’s GUI.

What does “Temporary failure in name resolution” mean?

This is the same as “could not resolve host” – the system tried to query a DNS server but got no response. The fixes are identical: check your network, DNS settings, and try a different resolver.

Can a firewall cause DNS resolution failure?

Yes. DNS uses UDP port 53. If your firewall blocks outbound UDP 53, all name lookups fail. Test with sudo ufw status or iptables -L. Temporarily disable the firewall to see if it’s the cause, then add an allow rule for port 53.

Is it safe to use public DNS like 8.8.8.8?

Yes, public DNS servers from Google, Cloudflare, and Quad9 are widely used and generally safe. However, they can see your DNS queries, so if privacy is a concern, consider a DNS-over-HTTPS (DoH) service or a VPN.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →