hostname command doesn't update /etc/hosts

Issues related to configuring your network
Post Reply
BluesRenegade
Posts: 8
Joined: 2007/01/25 11:47:17

hostname command doesn't update /etc/hosts

Post by BluesRenegade » 2007/10/10 11:47:59

For example:

[root@localhost ~]# hostname
localhost
[root@localhost ~]# hostname HomePC-1
[root@localhost ~]# hostname
HomePC-1
[root@localhost ~]# hostname -a
hostname: Unknown host
[root@localhost ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost

The hostname command appeared to change the hostname from 'localhost' to 'HomePC-1'.
A couple of strange things going on here:

1) The hostname command with no paramaters returns the new hostname, yet with -a it says, 'Unknown host'.

2) /etc/hosts is not updated, so where did the hostname command write the new hostname (HomePC-1), which it was able to recall without any parameters?

It appears that after changing the hostname, the machine has 2 diff host names, stored in 2 different locations, and depending on where they are read from or what command is used to retrieve the host name is which name you will be given.

Is this an accurate assessment? If so, what is going on here?

John

User avatar
toracat
Forum Moderator
Posts: 7386
Joined: 2006/09/03 16:37:24
Location: California, US
Contact:

Re: hostname command doesn't update /etc/hosts

Post by toracat » 2007/10/10 13:35:23

Reading man page for hostname will help. And check out /etc/sysconfig/network.

BluesRenegade
Posts: 8
Joined: 2007/01/25 11:47:17

Re: hostname command doesn't update /etc/hosts

Post by BluesRenegade » 2007/10/10 19:13:53

[quote]
toracat wrote:
Reading man page for hostname will help. And check out /etc/sysconfig/network.[/quote]

You are referring to the NOTE at the end of the manpage that says hostname change is temporary?

I _DID_ read man page for hostname, but that doesn't explain what happened within the same session, which is what I described above. There was no reboot in there!

After reboot, the Linksys router reports the hostname for that MAC address as: Host1
(the Linksys router always correctly identified the hostname before... where it's getting Host1 from I have no idea)

However, /etc/hosts reads:

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost

and /etc/sysconfig/network reads:

NETWORKING=yes
HOSTNAME=localhost.localdomain

Any ideas?

Q. Since hostname is temporary, is system-config-network the recommended way to permanently change the hostname?

Thanks for your help.

gerald_clark
Posts: 10642
Joined: 2005/08/05 15:19:54
Location: Northern Illinois, USA

hostname command doesn't update /etc/hosts

Post by gerald_clark » 2007/10/10 19:53:08

Edit /etc/sysconfig/network. That is where hostname gets its info.
Edit /etc/hosts and add a line for your IP and hostname.

Your router is getting the name from your nameserver.

User avatar
toracat
Forum Moderator
Posts: 7386
Joined: 2006/09/03 16:37:24
Location: California, US
Contact:

Re: hostname command doesn't update /etc/hosts

Post by toracat » 2007/10/10 19:53:16

Run system-config-network and go to the DNS tab and change the hostname there. On the HOSTS tab you can edit /etc/hosts file if you wish.

Or, simply edit /etc/sysconfig/network and enter the hostname in there. The result should be the same.

You can check the current hostname by:

cat /proc/sys/kernel/hostname

Post Reply

Return to “CentOS 4 - Networking Support”