Resolve Hostname to FQHN

Latest response

In /etc/host the fully qualified name is there and but in /etc/sysconfig/network the short name is there instead of the fully qualified domain name. How would I get hostname -f to get it to display FQHN?

Responses

Hello Edward,

In my /etc/hosts is the localhost present only. My /etc/sysconfig/network is empty. On my RHEL 7 the hostname (test1) ist stored in /etc/hostname and the domain is configured in /etc/sysconfig/network-scripts/ifcfg-ens192 like: DOMAIN="example.com"

With this configuration "hostname -f" displays the fqdn test1.example.com.

Kind regards, Joerg

The hostname entries in /etc/hosts are for local name resolution and system read hostname from here when /etc/nsswitch.conf is set to read from here. Also, when there is no dns involved the name set in /etc/sysconfig/network doesn't have any significance.

hostname man page says this...

The function gethostname(2) is used to  get  the  hostname.   When  the
       hostname  -a,  -d,  -f or -i is called will gethostbyname(3) be called.
       The difference in gethostname(2) and gethostbyname(3) is that  gethost-
       byname(3)  is  network  aware,  so  it  consults /etc/nsswitch.conf and
       /etc/host.conf to decide whether to read  information  in  /etc/syscon-
       fig/network or /etc/hosts

Hello, there is also the Configure Host Names chapter in the Red Hat Enterprise Linux 7 Networking Guide.

For testing purpose, I changed the hostname entry (rhel 6 vm) in /etc/sysconfig/network with static IP configuration set, and there is no DNS involved and 'NetworkManager' turned off . I had put both fqdn and alias entries in /etc/hosts file and my /etc/nsswitch.conf file is set as :

<br />[root@server222 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.100   server1.example.com server1
192.168.1.200   server2.example.com server2


[root@server222 ~]# grep -i host /etc/nsswitch.conf 
#hosts:     db files nisplus nis dns
hosts:      files dns


[root@server222 ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=server222
NOZEROCONF=yes
ARPCHECK=no
GATEWAY=192.168.1.254


[root@server222 ~]# sysctl -a|grep hostname
kernel.hostname = server222

[root@server222 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE="eth0"
BOOTPROTO="static"
HWADDR="00:0c:29:43:98:f6"
ONBOOT="yes"
TYPE="Ethernet"
IPADDR="192.168.1.100"
NETMASK="255.255.255.0"

Strangely, hostname got changed to "server222" which says that it has red it from /etc/sysconfig/network file and when I run "hostname -f" it failed even after I changed hostname entry and restarted network service :

[root@server222 ~]# grep -i hostname /etc/sysconfig/network
HOSTNAME=server1

[root@server222 ~]# hostname -f -v
gethostname()=`server222'
Resolving `server222' ...
hostname: Host name lookup failure

So, this confirms that the function gethostname() is actually reading from kernel.hostname entry, hence, I changed kernel entry after which (hostname -f)it worked successful:

[root@server222 ~]# sysctl -n kernel.hostname
server222

[root@server222 ~]# sysctl -w kernel.hostname=server1
kernel.hostname = server1

[root@server222 ~]# sysctl -n kernel.hostname
server1

[root@server222 ~]# hostname -f -v
gethostname()=`server1'
Resolving `server1' ...
Result: h_name=`server1.example.com'
Result: h_aliases=`server1'
Result: h_addr_list=`192.168.1.100'
server1.example.com

The hostname entry in /etc/sysconfig/network should be the FQDN as per this Red Hat KB :

What all options are available in /etc/sysconfig/network file in Red Hat Enterprise Linux 6?

There is another KB which is marked as "unverified": Hostname is incorrect with hostname and hostname -f which has to be corrected by Red Hat, otherwise, remove this mis-leading KB.

Hello, I will get those KBase solutions reviewed. The Red Hat Enterprise Linux 6 Deployment Guide had this section added recently Setting the Host Name.