How to Fix “Could Not Resolve Host” Error on Linux

When you use the terminal in Linux to update your system, install a package, or download something from the internet, you might sometimes see this message:

Could not resolve host

This means your computer can’t connect to the internet properly. More specifically, it can’t turn a website name like google.com into an IP address. This process is called DNS resolution. DNS stands for Domain Name System. It’s like a phone book. It helps your system match website names with the correct numbers (IP addresses).

If DNS doesn’t work, you’ll have trouble browsing the web, updating packages, or doing anything that needs a network. I’ve run into this problem a few times, especially when working on older systems, misconfigured servers, or networks with no internet.

In this post, I’ll show you how I fix it step by step. You don’t need to be an expert. I’ll explain each command and option in simple words, just like I’d tell a friend.


What Causes the Error?

There are a few reasons why your system might say “Could not resolve host.” Some common ones:

  • Your DNS settings are wrong.
  • The network connection is broken.
  • The DNS server you’re using is down.
  • A typo in your command or hostname.
  • You’re behind a firewall or VPN blocking DNS traffic.

Sometimes it’s something simple. Other times, it takes a few steps to figure out what’s wrong.


How I Start Troubleshooting

Let’s break it down. When I see this error, I go through a short checklist.

1. Check My Internet Connection

First, I check if I’m connected to the internet at all.

ping -c 3 8.8.8.8

This command tries to reach Google’s public DNS server by its IP. If that works, your network is up.

If it fails, your problem isn’t DNS. It’s your general internet connection.


2. Try a DNS Lookup

If my internet works, I test DNS:

nslookup google.com

or

dig google.com

These commands ask your DNS server to translate the domain into an IP address. If this doesn’t work, that confirms a DNS problem.


How to Fix It

Here are the steps I take to solve this issue.

Step 1: Check for Typos

It sounds obvious, but I often make small mistakes in commands.

Let’s say I type:

curl http://gooogle.com

That extra “o” means the hostname can’t be found.

Always check your spelling before going deeper.


Step 2: Check /etc/resolv.conf

This file tells Linux which DNS servers to use.

I open it with:

cat /etc/resolv.conf

It should look like this:

nameserver 8.8.8.8

If it’s empty or wrong, that’s your issue. You can edit it:

sudo nano /etc/resolv.conf

And add:

nameserver 8.8.8.8
nameserver 1.1.1.1

These are public DNS servers from Google and Cloudflare. They’re reliable and fast.

⚠️ Some systems reset this file when you reboot or reconnect to Wi-Fi. If you’re using NetworkManager, I’ll explain how to make changes permanent below.


Step 3: Restart Networking Services

Sometimes, restarting your network fixes things.

Try:

sudo systemctl restart NetworkManager

or, on older systems:

sudo service networking restart

This refreshes your connection and applies any changes you made.


Step 4: Use a Static DNS with NetworkManager

If your DNS settings keep resetting, you can set them in NetworkManager.

Edit this file:

sudo nano /etc/NetworkManager/NetworkManager.conf

Add this under [main]:

dns=none

Then create or edit this:

sudo nano /etc/resolv.conf

And add:

nameserver 1.1.1.1
nameserver 8.8.8.8

Then restart NetworkManager:

sudo systemctl restart NetworkManager

Now your system will use the DNS you set manually.


Step 5: Try Different DNS Servers

Some networks block or redirect DNS. Try using a public one like:

  • 1.1.1.1 – Cloudflare
  • 8.8.8.8 – Google
  • 9.9.9.9 – Quad9

Try pinging each one:

ping 1.1.1.1

If one works better, switch to it in your config.


Step 6: Test with Another Tool

If you’re still stuck, test using wget, curl, or a browser.

curl https://example.com

If it fails, compare the error with your ping and DNS test results.


Three Useful Commands

Here are a few commands that help me diagnose DNS issues quickly:

  • ping – Test basic internet connection.
  • dig – Test DNS resolution.
  • systemd-resolve --status – See DNS settings if you use systemd-resolved.

These tools help you know where the problem is—your network, DNS, or something else.


Three Causes I Often See

In my experience, these three things often cause the problem:

  • Using a VPN that blocks DNS.
  • Switching Wi-Fi networks that assign bad DNS servers.
  • A firewall or router blocking UDP port 53 (used for DNS queries).

Understanding what changed before the problem started helps you fix it faster.


Three Fixes That Usually Work

If you’re in a hurry, try these:

  • Manually set your DNS in /etc/resolv.conf.
  • Restart your network with systemctl restart NetworkManager.
  • Try a different network or hotspot to rule out local router issues.

I’ve used these on laptops, servers, and Raspberry Pi setups. They usually get DNS working again quickly.


Bonus Tip: Test with IP Address

If DNS isn’t working but the internet is, try accessing sites by IP. For example:

curl http://216.58.214.14

That’s Google. If this works but curl http://google.com doesn’t, DNS is definitely the issue.


Conclusion

The “Could not resolve host” error on Linux can be annoying, but it’s usually fixable. It just means your system can’t find the IP address for the site you want to reach.

I always start by testing my internet connection. Then I check the DNS settings. And I make sure there are no typos in my commands. Most of the time, a simple fix like updating /etc/resolv.conf or restarting the network works.

Have you ever run into this error? What did you do to fix it? If you’re still stuck, feel free to share your setup, and I can help you figure it out.

Let me know if you want a printable cheat sheet with all the commands from this post.

Leave a Reply