keepcalculator

06 Numbers and Powers

SSH Could Not Resolve Hostname: Fix

Card No. 06·26647 · Updated · Standard formula, shown below

The “ssh: Could not resolve hostname” error means your computer cannot translate the server name you typed into an IP address, so it cannot start a connection. The fix is usually a simple typo, a missing DNS server, or a missing entry in your local hosts file — check those three things first.

What This Error Actually Means

When you run ssh user@example.com, your SSH client needs to find the network address of example.com. It does this through a process called name resolution, which typically uses the Domain Name System (DNS). If the name cannot be resolved, SSH aborts with an error that looks like:

ssh: Could not resolve hostname example.com: Name or service not known

This is not an SSH authentication problem — it happens before any connection is attempted. The error is identical to what you would see with ping example.com or curl example.com, so the cause is in your network configuration or the name itself, not in SSH’s settings.

Most Common Causes (And Quick Checks)

Before digging into configuration files, run these quick checks in order:

  • Typo in the hostname — a missing dot, an extra letter, or a wrong TLD (.com vs .net) is the most frequent cause. Re-type the command slowly.
  • Forgot the port or user — if you meant to connect to a non-default port, you need ssh -p 2222 user@host. A missing -p flag can make the hostname look wrong if you accidentally included it in the hostname string.
  • Local network name — if you are connecting to a machine on your LAN, try its IP address directly: ssh user@192.168.1.50. If that works, your router’s DNS or your hosts file is the issue.
  • Internet connection — a temporary network drop can cause resolution to fail. Try ping 8.8.8.8 to test basic connectivity.

How Name Resolution Works on Linux and macOS

On Unix-like systems, the order of name resolution is defined in /etc/nsswitch.conf. The typical line for hosts looks like:

hosts: files dns

This means the system first checks /etc/hosts (the “files” source), then queries the DNS servers configured in /etc/resolv.conf. If neither has an answer, you get the “Name or service not known” error.

For example, if you have a line in /etc/hosts like:

192.168.1.100 myserver.local

then ssh user@myserver.local will work even without DNS. If that line is missing, the system falls through to DNS, which may not know about .local names.

Step-by-Step Fixes

Work through these in order — the first one that applies usually solves the problem.

  1. Check for typos — compare your command with the server’s exact hostname or IP. Pay attention to case sensitivity (hostnames are case-insensitive, but usernames are not).
  2. Test with the IP address — run ssh user@203.0.113.5 (use the actual IP). If this works, the problem is name resolution, not SSH.
  3. Verify DNS servers — run cat /etc/resolv.conf and check for a nameserver line. If it’s empty or points to an unreachable server, set a public DNS like 1.1.1.1 or 8.8.8.8 (temporarily, for testing).
  4. Flush the DNS cache — on macOS: sudo dscacheutil -flushcache; on Linux with systemd: sudo systemd-resolve --flush-caches (or resolvectl flush-caches on newer versions).
  5. Add a hosts entry — for a local server, add a line to /etc/hosts mapping the IP to the hostname. This is immediate and permanent.
  6. Check your SSH config — if you use an alias like ssh myserver, look in ~/.ssh/config for a Host myserver block with a HostName line. A missing or misspelled HostName will cause this exact error.

Windows (PowerShell and CMD) Notes

Windows uses a different resolver. The error appears as ssh: Could not resolve hostname ... Name or service not known as well. Fixes are analogous:

  • Check the hosts file at C:\Windows\System32\drivers\etc\hosts for stale or missing entries.
  • Flush DNS with ipconfig /flushdns.
  • Verify your DNS servers with ipconfig /all.
  • For WSL (Windows Subsystem for Linux), the resolution uses the Linux environment, so follow the Linux steps above.

When It’s Not DNS: Other Causes

Sometimes the error message is misleading. A few situations produce the same text but have different roots:

  • IPv6 issues — if the hostname resolves to an IPv6 address but your network has no IPv6 route, SSH may fail with a similar message. Try ssh -4 user@host to force IPv4.
  • Proxy or VPN — a VPN that changes your DNS servers can break resolution. Disconnect and retry.
  • Firewall blocking DNS — some networks block outbound DNS on port 53 to force their own resolver. Test with dig example.com (if installed) to see if the query returns an answer.
  • Hostname with a trailing dot — ssh user@example.com. (with a dot at the end) is technically a fully qualified domain name, but many resolvers treat it incorrectly. Remove the trailing dot.

Frequently Asked Questions

Why does SSH say “Name or service not known” when I can ping the IP?

Because you are using a hostname, not an IP. The IP works because it does not require DNS lookup. The hostname requires a resolver to translate it to an IP, and that resolution is failing. Check your DNS settings or add the hostname to /etc/hosts.

Can I use an IP address instead of a hostname in SSH?

Yes, always. ssh user@192.168.1.50 bypasses name resolution entirely. This is a reliable workaround for local servers. For remote servers, the IP may change, so a hostname is more practical once DNS is fixed.

What is the difference between “Could not resolve hostname” and “Connection refused”?

“Could not resolve” means the name lookup failed — the server’s address is unknown. “Connection refused” means the address was found, but nothing is listening on the SSH port (usually 22) or a firewall is blocking it. The fixes are completely different.

How do I permanently add a hostname to my hosts file?

Open /etc/hosts (Linux/macOS) or C:\Windows\System32\drivers\etc\hosts (Windows) with administrative privileges. Add a line like 192.168.1.100 myserver.local, save, and it takes effect immediately. No restart needed.

Why does SSH work on one computer but not another on the same network?

Each computer has its own DNS configuration and hosts file. The working computer may have a hosts entry or a different DNS server. Compare /etc/resolv.conf and /etc/hosts between the two machines.

Can a VPN cause this SSH error?

Yes. A VPN can override your DNS servers, and if the VPN’s DNS cannot resolve the hostname, you get this error. Disconnect the VPN and test. If it works, either reconfigure the VPN’s DNS or use the IP address while connected.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →