0

How can a Linux server lose its hostname? This might be a loaded question, but there might be several answers. I just need to diagnose the problem I'm having, but I don't know where to start.

Perhaps you can help me figure out how Linux determines the output for this command? I'll accept that as an answer as well.

[root@xyz ~]# hostname -f
hostname: Unknown host

This issue was what allowed me to determine the hostname was missing:

https://stackoverflow.com/questions/6484275/what-causes-the-error-java-net-unknownhostexception

EDIT:

Per this comment, I've pasted the /etc/hosts file below. It doesn't look like the hostname is found in there. This is CentOS. D34DM347, what OS are you looking at the man pages in?

The man page for hostname indicates the -f option returns the FQDN, usually defined in /etc/hosts linux.die.net/man/1/hostname – D34DM347

/etc/hosts:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  • The man page for hostname indicates the -f option returns the FQDN, usually defined in /etc/hosts linux.die.net/man/1/hostname – D34DM347 Dec 10 '15 at 17:04
  • D34DM347, please see edit. Do you have a server where the hostname is found in your /etc/hosts? Also, what distribution are you looking at? – MacGyver Dec 10 '15 at 17:40
  • It really shouldn't matter which distort. Typically there would be another entry in /etc/hosts which lists your hostname and IP address. Have you tried setting the hostname with hostname servername? – D34DM347 Dec 10 '15 at 17:52
  • Perhaps you can help me figure out how Linux determines the output for this command? - see unix.stackexchange.com/questions/164089/… Distro specific, depends on systemd or not systemd. Might be /etc/hosts or /etc/hostname or /etc/sysconfig/network or /etc/cloud/cloud.cfg or none of the above. – TessellatingHeckler Dec 10 '15 at 18:13
  • You forgot to mention which distribution version you are using. – Michael Hampton Dec 10 '15 at 18:14
2

I'm running CentOS7 and my hostname is written under all the ipv4 / ipv6 entries. Try adding a line at the bottom of /etc/hosts with

[your IP address] [your hostname]

Also you can update (although I didn't have to for my servers)

/etc/sysconfig/network

 NETWORKING="yes"
 GATEWAY="10.1.1.1"
 HOSTNAME="www.example.com"

You'll have to restart networking at least, generally I do a full reboot though.

systemctl restart network

or

shutdown -r now
  • What am I adding a line to? /etc/hosts? – MacGyver Dec 10 '15 at 18:15
  • 1
    Yes, /etc/hosts – red_shift Dec 10 '15 at 18:20
  • This works, but I'm just wondering if it's the best answer. Because doesn't the kernal also store it? – MacGyver Dec 10 '15 at 18:21
  • 1
    It should be, try updating /etc/sysconfig/network too based on my edit. – red_shift Dec 10 '15 at 18:22
  • 1
    /etc/hosts survives a restart so it MAY be the kernel value. I don't want to lead you down the wrong path there so you should investigate more if you need to know down to that level. – red_shift Dec 10 '15 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.