What Does “Name or Service Not Known” Mean and How To Fix It

In this post, we will explore what this message means, what could cause this message, and different solutions to those issues.

On this page
  1. Understanding The Error Message
  2. Identifying The Root Cause
  3. Cause 1: DNS Issues
  4. Cause 2: Service Name Resolution
  5. Did You Know?
  6. Additional Links

Have you encountered the following error message “Name or Service Not Known” while working on your Linux system? If so you have come to the right place. In this post, we will explore what this message means, what could cause this message, and different solutions to those issues.

#Understanding The Error Message

This error occurs because a hostname or service name is unable to be resolved to an IP or port respectively.

For example, if you try to ping domain names that don’t exist such as:

Code
cj@example:~$ ping nonexistentdomain.example
ping: nonexistentdomain.example: Name or service not known

Typically, with this error message, you will see the application, the hostname, or the service, followed by the error message.

#Identifying The Root Cause

Context is important in narrowing down the root cause:

  1. The error occurs when making a network connection and is being used to represent the hostname or domain of a specific host then this would be considered a DNS issue.
  2. The error occurs when being used to reference a port rather than a host then it would be considered an issue related to service name resolution.

#Cause 1: DNS Issues

The most common cause of this error message is DNS (domain name resolution) related issues. DNS records are how a domain name is translated to IP addresses.

Before we jump in ask yourself do you expect this hostname to be valid, could it be that it is misspelled, or invalid? For external domains do you have a working internet connection, do you have working DNS servers configured, have you checked the domain name using an online DNS checker, does it resolve for the rest of the internet?

Excluding the above this should leave DNS configuration issues or local DNS records issues.

First, confirm that your machine’s DNS configured is set up as foreseen. To do that run the command below to check /etc/resolv.conf and systemd-resolved:

Code
{ echo "=== /etc/resolv.conf ==="; cat /etc/resolv.conf | grep '^nameserver'; echo "=== NetworkManager ==="; nmcli device show | grep 'IP4.DNS'; echo "=== systemd-resolved ==="; resolvectl status | grep "DNS Servers" -A2; } 2>/dev/null

For local issues where DNS is set up on the host itself, you can add the record to your systems host file which is located at /etc/hosts using the following syntax:

IP Address + space + the domain

Code
 203.0.113.119 hostfileexample.local

Note: be aware that to edit the /etc/hosts file you will need root or sudo permissions.

#Cause 2: Service Name Resolution

If the name being mentioned is referring to a service name these are specified in /etc/services and map specific service names to port number and protocol.

To resolve issues with service names either add a new entry in that file or directly specify the port and protocol directly.

#Did You Know?

Have you ever wondered how many files are checked every time a DNS lookup is made on your system? Even if you haven’t, it is intriguing to think about, isn’t it? The below strace command which counts the files opened for both the ping command and dig commands:

Code
strace -e trace=file ping -c 4 namehero.com 2>&1 | grep open | wc -l
Code
strace -e trace=file dig namehero.com 2>&1 | grep open | wc -l

Done reading and looking for more, why not check these out?

Illustrated avatar of CJ Saathoff
Written by

CJ Saathoff

CJ Saathoff writes for the NameHero blog.

More from CJ →