DEV Community

Nate
Nate

Posted on Originally published at shadeterm.com

SSH Could Not Resolve Hostname? Trace the Name Before Changing SSH

When SSH says it “could not resolve hostname,” it usually hasn’t reached the server yet. The client couldn’t turn the destination name into an address, so changing your password, SSH key, or server firewall is unlikely to help.

The fastest way to troubleshoot is to follow the connection in order: check what you asked SSH to connect to, inspect the effective SSH configuration, then test name resolution in the same environment where SSH is running.

1. Check the destination you typed

SSH expects one destination, usually in the form user@host. Extra arguments after it are treated as a remote command, not as another address.

# Wrong: SSH tries to resolve the literal hostname "server"
ssh root@server 10.0.2.15

# Right: specify the IP as the destination
ssh root@10.0.2.15
Enter fullscreen mode Exit fullscreen mode

If you need a non-default port, use -p:

ssh -p 2222 deploy@host.example
Enter fullscreen mode Exit fullscreen mode

A target written like deploy@host.example:2222 is not the usual SSH command syntax. Also check for typos, accidental spaces, and placeholder names such as server that were never replaced with your actual hostname or IP.

If connecting by IP works but connecting by name does not, the SSH service may be fine. The unresolved piece is the name-to-address mapping. That doesn’t yet tell you whether the issue is DNS, a local hosts file, an SSH alias, or a VPN-only name.

2. Find out what SSH will actually connect to

A short name like production might be an SSH configuration alias, not a DNS name. OpenSSH reads client configuration from files such as ~/.ssh/config; a Host entry can map an alias to a real hostname or IP.

Host production
    HostName 192.0.2.10
    User deploy
    Port 2222
Enter fullscreen mode Exit fullscreen mode

Here, production is the alias and 192.0.2.10 is the effective destination. That address is reserved for documentation, so replace it with a real server address before connecting.

Use ssh -G to print the settings SSH will use without making a connection:

ssh -G production | grep -E '^(hostname|user|port) '
Enter fullscreen mode Exit fullscreen mode

On Windows PowerShell, filter the output with:

ssh -G production | Select-String "^(hostname|user|port) "
Enter fullscreen mode Exit fullscreen mode

Check that the reported hostname is the name or IP you intended. If it says production when you expected a different destination, inspect the matching Host entry and any included configuration files.

For a comparison that ignores the usual SSH configuration on Linux or macOS, run:

ssh -F /dev/null -G production
Enter fullscreen mode Exit fullscreen mode

This can help identify whether an alias depends on your SSH config. It does not test DNS or establish a connection.

3. Test the system resolver

On Linux, getent asks the system’s name-service lookup mechanism for a result. That’s often a more useful first check than a DNS-only query because system resolution can include /etc/hosts and other configured sources.

getent ahosts host.example
Enter fullscreen mode Exit fullscreen mode

If dig is installed, compare its DNS result:

dig +short host.example
Enter fullscreen mode Exit fullscreen mode

These commands don’t test exactly the same thing. For example, a name in /etc/hosts may resolve through getent even if dig returns no DNS answer. If dig works but getent fails, look at the system resolver configuration. If getent works but dig does not, check whether the answer comes from a local mapping rather than DNS.

On Windows, you can query DNS with PowerShell:

Resolve-DnsName -Name host.example
Enter fullscreen mode Exit fullscreen mode

To compare against a DNS-only lookup that excludes the hosts file, use:

Resolve-DnsName -Name host.example -DnsOnly -NoHostsFile
Enter fullscreen mode Exit fullscreen mode

On macOS, a system lookup can be checked with:

dscacheutil -q host -a name host.example
Enter fullscreen mode Exit fullscreen mode

Run checks in the environment that runs SSH. A Windows host, WSL distribution, Docker container, and jump host may each have different resolver settings. A successful lookup on your laptop does not establish that a container or remote bastion can resolve the same name.

4. Check local names, VPNs, and search domains

On Linux and macOS, inspect /etc/hosts; on Windows, the hosts file is C:\Windows\System32\drivers\etc\hosts. A local entry maps a name to an address without requiring a DNS record. Only add or change one if you know the correct address and have permission to modify local mappings.

A stale entry can be just as confusing as a missing one. If the name maps to the wrong address, you may connect to the wrong machine or see a different error, such as a timeout, refusal, or host-key warning. Don’t disable host-key verification to mask a destination mismatch.

Short names such as db01 may only work on a company network, where a DNS search suffix is configured. Private names may also require a VPN-provided resolver. If a hostname works on the VPN but not off it, investigate the VPN and its DNS settings before switching to a public DNS server; public resolvers generally cannot look up private internal records.

For a jump-host connection such as ssh -J bastion private-host, pay attention to which machine needs to resolve each name. The target lookup may happen from the bastion side, so a local lookup alone may not explain the failure.

5. Read the error at the stage where it occurs

For a direct SSH connection, the rough order is: select the destination, resolve its name, open a TCP connection, perform the SSH handshake, then authenticate. The wording can vary by operating system, but the stage helps narrow down the cause:

  • Could not resolve hostname: Check command syntax, SSH aliases, and name resolution.
  • Connection refused: The name resolved and a connection attempt was rejected. Check the listener, port, and relevant firewall rules. See this guide to diagnosing SSH connection refused errors if that’s the message you’re seeing.
  • Permission denied (publickey): SSH reached the authentication stage; investigate the account and key rather than DNS. This public-key authentication troubleshooting guide covers the next checks.

Use verbose output to see how far the client gets:

ssh -vv -o ConnectTimeout=5 user@host.example
Enter fullscreen mode Exit fullscreen mode

Look for the destination SSH is using and whether output reaches a Connecting to line with an address. With ProxyJump or ProxyCommand, verbose output can also help identify which hop produced the error. Don’t assume every hostname in a multi-hop setup is resolved by your local computer.

A sensible order of operations

When you hit this error, work through these checks before changing server settings:

  1. Confirm the command has one correct destination and the right port syntax.
  2. Use ssh -G to inspect the effective hostname, user, and port.
  3. Test the name with the system resolver in the same environment as SSH.
  4. Compare local mappings with DNS, and check VPN or search-domain requirements.
  5. Use ssh -vv to confirm which stage fails.

That sequence separates a bad target or alias from a resolver problem—and both from later TCP or authentication errors. It also avoids spending time changing SSH keys, opening ports, or restarting a server when the client hasn’t found an address to connect to.

I originally published a more detailed version of this guide on the ShadeTerm blog.

I'm also building ShadeTerm — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)