Linux Error: Name or service not known — Cause, Fix, and Troubleshooting Guide
How to fix the Linux 'Name or service not known' error (EAI_NONAME): diagnose failed DNS lookups, /etc/hosts, resolv.conf and systemd-resolved on Ubuntu and RHEL.
- #linux
- #troubleshooting
- #dns
- #networking
Summary
Name or service not known is glibc’s EAI_NONAME from getaddrinfo() — the resolver looked up a hostname (or service name) and got a definitive “no such name” answer. Unlike a temporary DNS timeout, this is usually deterministic: the name is wrong, does not exist in DNS, or the resolver is not consulting the source that would know it. It can also mean an unknown port/service name.
Common Symptoms
ssh,curl,ping, orgitfail against one specific hostname while others work.- The error is immediate (no long timeout), signalling a hard NXDOMAIN rather than a slow lookup.
- Internal/
.local/short hostnames fail while fully-qualified public names resolve. getent hosts <name>returns nothing.
Most Likely Causes of the ‘Name or service not known’ Error
- Typo or non-existent hostname — the name simply is not in DNS (NXDOMAIN).
- Missing search domain — a short name like
db01needs asearch corp.example.comsuffix that is not configured. - Host not in
/etc/hostsand not in DNS — common for internal service names and for the box’s own hostname. - Wrong or empty nameserver in
/etc/resolv.conf, so even valid names return nothing. nsswitch.confmisordered — thehosts:line does not includedns, or omitsfiles.- Unknown service name — e.g. passing a port name that is not in
/etc/services.
Quick Triage
# Is it just this one name? Try a name you know resolves.
getent hosts example.com
getent hosts <the-failing-name>
# Raw IP works? -> confirms connectivity is fine, it is name lookup.
ping -c1 1.1.1.1
Diagnostic Commands
# Resolution via the real NSS path (files + dns)
getent hosts db01.corp.example.com
# Direct DNS: does the authoritative/upstream server know it?
dig db01.corp.example.com
dig @1.1.1.1 example.com +short
nslookup db01.corp.example.com
# Search domains and nameservers actually in effect
cat /etc/resolv.conf # look at 'search' and 'nameserver' lines
resolvectl status # per-link DNS + search domains
# Is 'dns' even consulted, and in what order?
grep '^hosts:' /etc/nsswitch.conf
# Static overrides
getent ahosts <name> # shows files+dns merged result
cat /etc/hosts
# Stub resolver health
systemctl status systemd-resolved
ss -tulpn | grep ':53'
nc -vz 1.1.1.1 53
# Verbose client trace
curl -v http://db01.corp.example.com
journalctl -u systemd-resolved --since "10 min ago"
A bare short name that fails but resolves once you append the domain (dig db01.corp.example.com) points straight at a missing search domain.
Fix / Remediation
-
Verify the name is correct and exists. Test the FQDN directly:
dig db01.corp.example.com +shortIf DNS has no record, the fix is on the DNS side (add the record), not the client.
-
Add the search domain so short names resolve:
sudo resolvectl domain <IFACE> corp.example.com # systemd-resolved # NetworkManager (RHEL/Rocky): sudo nmcli con mod "<CONN>" ipv4.dns-search "corp.example.com" sudo nmcli con up "<CONN>" -
Add a static entry to
/etc/hostsfor internal names with no DNS record:echo '10.0.3.20 db01 db01.corp.example.com' | sudo tee -a /etc/hosts -
Fix
nsswitch.confifdnsis missing:grep '^hosts:' /etc/nsswitch.conf # should read: hosts: files dns (or ...resolve dns on systemd-resolved)Warning: Edit
/etc/nsswitch.confcarefully — a malformedhosts:line can break all name resolution, including forsudoand package tooling. Keep a root shell open until you confirm resolution still works. -
Point at a working nameserver if
/etc/resolv.confis empty or wrong (see the DNS management steps for your distro —resolvectl dnson Ubuntu,nmclion RHEL).
Validation
getent hosts <name> # now returns an IP
dig <name> +short # returns an answer (or matches /etc/hosts)
ssh -o BatchMode=yes <name> true 2>&1 | grep -v 'Name or service'
Prevention
- Prefer FQDNs in configs and scripts so you do not depend on
searchdomains. - Keep internal service names in DNS (or a managed
/etc/hosts) rather than ad hoc edits. - Standardise
nsswitch.confand resolv.conf via configuration management. - Add synthetic
getent hostschecks for critical internal names to catch DNS drift before deploys fail.
Related Errors
- Temporary failure in name resolution
- curl: (6) Could not resolve host
- sudo: unable to resolve host
- Connection refused
- Docker: Temporary failure in name resolution
Final Notes
Name or service not known is a definitive “this name does not resolve,” so start by proving whether the name exists in DNS at all with dig on the FQDN. If the FQDN resolves but the short name does not, it is a search-domain gap; if neither resolves, fix DNS or add an /etc/hosts entry.
Want faster Linux incident response? Use DevOps AI Toolkit to turn production errors into clear diagnostics, remediation steps, and reusable runbooks.
Download the Free 500-Prompt DevOps AI Toolkit
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.