I need to do the hostname changes on a centos server which will serve www.myserver.com

What do I need to do in /etc/hosts, /etc/sysconfig/network and with the hostname command?

In /etc/sysconfig/network do I need to write this :


or this :


In /etc/hosts do I need to write this :

XXX.XXX.XXX.XXX     myserver.com

or this :

XXX.XXX.XXX.XXX     wwww.myserver.com www

Do I need to do sudo hostname www.myserver.com or sudo hostname myserver.com?

Thanks a lot!


Is there something else that I need to setup about hostname?

  • i don't even get why anyone would bother to do this stuff. i see a mention below of using hostname -f so i guess you get a properly-functioning hostname command, but what else?
    – Randy L
    Jan 11, 2016 at 22:58

2 Answers 2


/etc/sysconfig/network file is source from which the startup scripts take the arguments for 'hostname' command. And this should be just the machine name, not fully qualified. The domain part is usually defined in the /etc/resolv.conf file.

Assuming the fully qualified host name is 'lemon.example.com' ('www' doesn't look like a good host name to me), then:

  • /etc/sysconfig/network: HOSTNAME=lemon
  • /etc/resolv.conf (along right 'nameserver' entires): search example.com
  • /etc/hosts (should not be needed if DNS works properly): XXX.XXX.XXX.XXX lemon.example.com lemon

If everything is properly configured, then hostname command will return "lemon" and hostname -f will return "lemon.example.com".

In your case it would seem the hostname is 'www' in domain 'myserver.com'… I don't think that is exactly what you want. 'www' may be an alias (DNS CNAME)… if you want it in your /etc/hosts file, then just append it to the line with your IP:

XXX.XXX.XXX.XXX lemon.example.com lemon www.myserver.com

The settings from /etc/sysconfig/network will be applied after network is restarted. You may set the hostname immediately with the hostname command. /etc/hosts and /etc/resolv.conf changes are active immediately (except for applications that keep old data cached).

  • 1
    /etc/sysconfig/network is actual for pre-systemd Linuxes like CentOS 6 (End of life November 30th, 2020) /etc/resolv.conf is managed by NetworkManager in modern distributions (don't edit this file manually) Dec 4, 2020 at 8:45

your settings should look like this:

  • /etc/sysconfig/network: HOSTNAME=www.myserver.com
  • /etc/hosts: XXX.XXX.XXX.XXX wwww.myserver.com www

you can also call hostname, but this will only change the hostname until you restart your server the next time.

as far as i can remember, these are all changes you have to make.

  • 3
    If you put the FQDN in /etc/sysconfig/network then the 'hostname' and 'hostname -f' commands will return the same FQDN value. This is not really correct. 'hostname' should ideally simply return the actual hostname. Apr 4, 2011 at 12:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .