“ntpq -pn” command returns with error “Name or service not known”

The Problem

Executing the command “ntpq -pn” returns with the error:

# ntpq -p
Name or service not known

In some cases, you may also encounter an error shown below:

# ntpq -pn
Servname not supported for ai_socktype

The Solution

The “Name or service not known” error will be seen if the system cannot resolve “localhost” for the ntpq command. Make sure we have pointer to “127.0.0.1 localhost” in /etc/hosts file.

1. Fix the /etc/hosts file to map 127.0.0.1 to localhost, i.e., there must be a line in /etc/hosts that begins with “127.0.0.1” and contains “localhost”.

Note that it is perfectly fine if this line contains additional host aliases in addition to localhost. Example:

# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain myserver myserver.mydomain
Note that there are countless other programs that will also error-out or behave erratically if the 127.0.0.1 –> localhost mapping is removed from /etc/hosts.

That said, in RHEL6, the name “localhost” maps to both 127.0.0.1 and the IPv6 loopback ::1, so while removing the 127.0.0.1 line in RHEL6 would be a bad idea, it would likely not cause ntpq to fail.

2. Also make sure we have pointer to ntp in /etc/services file.

# vi /etc/services
ntp             123/tcp
ntp             123/udp                         # Network Time Protocol
Related Post