If your running CentOS/Linux VPS or server, you may encounter your custom hostname changing after restarting your VPS/server. Hosting providers usually assign your server with a default hostname, usually a unique subdomain with a bunch of numbers.
Most of us however, prefer to set our server hostname to something more personal and professional such as “server1.domain.com”, or “host.domain.com” especially if running a mailserver.
You may have noticed that after setting your custom hostname over SSH in /etc/hosts and /etc/sysconfig/network these settings are overwritten on a server restart.
The quick explanation as to why this happens is that on a server restart, Virtualization will overwrite /etc/hosts and /etc/sysconfig/network to the default hostname that your hosting provider issued.
There are 2 Methods to Solve this Problem:
1) Contact Your Hosting Provider
Find out if they are willing to change your hostname at the parent node. Some hosting providers are willing to do this (usually the smaller ones) and others will not make this change. This is the “best” way to customize your hostname, but is not the only way.
2) Lock Host & Network Files
If you don’t want to call your host, or they won’t agree to change it, follow this simple workaround:
1) Edit your /etc/hosts and /etc/sysconfig/network files over SSH changing them to reflect your preferred hostname
2) Run the following commands:
# chattr +i /etc/hosts
# chattr +i /etc/sysconfig/network
This will “lock” the files as read-only and therefore prevent any attempts to change them. Need to unlock the files later? Simply run the commands again to “un-lock” them.
Mike is the managing member at MSLtek and is an avid web consultant and web developer. He enjoys solving complex problems and building awesome stuff.