8

On CentOS (5.x) I have a VPS that has hostname set to "olddomain.com", at least this is the domain I am getting when typing "hostname" command.

I want to change it to 'localhost.localdomain' because I think it is more generic and reliable.

localhost.localdomain already exists in these files: /etc/sysconfig/network AND /etc/hosts

NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=localhost.localdomain
GATEWAY=95.xx.xx.xx

AND

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

So next I tried these commands:

hostname localhost.localdomain

AND

hostname -v localhost.localdomain

"hostname" command now returns this new value. But after restart it changes back to olddomain.com or at least this is the "hostname" command returns.

Anything else to edit?

adrianTNT
  • 1,252

4 Answers4

10

Add the following lines into your /etc/sysctl.conf configuration file.

kernel.hostname = hostname
kernel.domainname = domain.com

And execute the command sysctl -p /etc/sysctl.conf to read and apply the changes of your /etc/sysctl.conf configuration file.

bintut
  • 339
2

Did you edit the /etc/sysconfig/network file? Or are you just showing it's contents? The hostname command does not actually edit these files. When the system starts the init scripts read the host name from the configuration files and this setting is then passed to the hostname command. In additoion as mentioned by other posts be sure to edit your hosts file so that the mapping for your hostname <-> ip is correct on the local host.

More information is available in the Red Hat documentation

Also are you using Network Manager to manage your network interfaces? If so you will need to go into NM and make sure the settings jive there as well.

Red Tux
  • 2,084
  • 13
  • 14
2

I didn't edit the files /etc/sysconfig/network and /etc/hosts because these already contained the hostname I wanted (localhost.localdomain), so just by command hostname localhost.localdomain it didn't remain saved after reboot.

But then my host edited these two files to a different host name, rebooted the server (without any other command, just editing) and the new hostname applied and remained changed. Then I edited files again and set them to localhost.localdomain as they are on some other of my VPS and now they updated and remained to localhost as I wanted.

Edit (11 years later): while working on an older OS (CentOS 7.x) I was unable to change hostname by any method, the only thing that eventually worked was @bintut's answer below, editing /etc/sysctl.conf and adding:

kernel.hostname = hostname
kernel.domainname = domain.com
adrianTNT
  • 1,252
0

I'm pretty sure that /etc/sysconfig/network is deprecated. Try /etc/sysconfig/networking/profiles/default/hosts and /etc/sysconfig/networking/profiles/default/network.

Tim
  • 3,087
  • 19
  • 15