hostname options not working

On a CentOS 7 Linux system, I could issue the command hostname and see the name for the system displayed.
# hostname
moonpoint

Yet, if I tried entering any option for the command, I received the error message "Name or service not known".

# hostname -a
hostname: Name or service not known
# hostname -i
hostname: Name or service not known
# hostname -f
hostname: Name or service not known
# hostname --fqdn
hostname: Name or service not known

The hostname command existed and when I checked the man page for the command by issuing the command man hostname, I saw all of those options listed as supported arguments to the command - see hostname man page.

# which hostname
/bin/hostname
#

I attempted to use strace, a "system call tracer" tool, to check the command, but found it wasn't present on the system, so I installed it with yum install strace. I then tried strace hostname --fqdn , but didn't see the cause of the problem in its output - I stored the results in hostname-fqdn_trace.txt with strace -o hostname-fqdn_trace.txt hostname --fqdn. I saw an IP address in the output; when I checked that IP address I realized it was a Domain Name System (DNS) server for the system, so thought the system may have been attempting to look up a fully qualified domain name (FQDN) for the system, but perhaps could not.

I had specified the hostname for the system a week ago using the hostname command, but hadn't specified a domain name then. I examined the /etc/hosts file, but there wasn't one specified there.

# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

The etc/hosts file on another CentOS system was the same as the one above, but I could issue the hostname command there with the above arguments to the command without the error message appearing, though whenever I added an option to the command there was a delay in the output appearing.

$ hostname saturn
$ hostname -i
fe80::21b:fcff:fe2f:66fd%enp1s4 192.168.0.5
$ hostname -f
saturn
$ hostname --fqdn
saturn$

But I added a line at the end of /etc/hosts in the form:

serverIPaddress myhostname.mydomain.tld myhostname

Note: "tld" stands for "top-level domain", e.g. .com, .net, .org, etc.

E.g., since I had the domain moonpoint.org pointed to the server I added a line similar to the following one:

192.168.8.5 moonpoint.org moonpoint

Immediately after I did so, all of the options that hadn't worked with the hostname command previously now worked.

# hostname -f
moonpoint.org
# hostname --fqdn
moonpoint.org
# hostname -a
moonpoint
# hostname -i
192.168.8.5
# hostname
moonpoint

References:

  1. Using strace to debug application errors
    Posted by: Steve
    Date: February 15, 2006
    Debian Administration
  2. Setting the Hostname & Fully Qualified Domain Name (FQDN) on Ubuntu 12.04 or CentOS 6.4
    By: Pabllo Carranza, aka vDevices
    Date: November 12, 2013
    GitHub

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px