RedHat Linux

Hostname Changing Back after Reboot

On OpenVZ VPS running Centos 7 with cPanel installed one of my friends had problem, he couldn’t keep his FQDN (Fully Qualified Domain Name) as his hostname.
Instead, after every reboot his hostname: changed just to scorpio.
This made him a lot of trouble because if hostname is not FQDN many mails go to spam.

To fix this I open crontab

crontab -e

added following line:

@reboot /usr/local/cpanel/bin/set_hostname

Now on every boot his hostname is just the way it is supposed to be.
If you are not running cPanel and have the same problem you can add this instead:

@reboot hostnamectl set-hostname

Another solution, and my least favorite is to execute this after changing hostname:

chattr +i /etc/hosts && chattr +i /etc/sysconfig/network

As far I figured it out this issue is probably related to CentOS 7 and/or CPanel.

About the author

Luka Paunovic

I am Linux System Administrator and student of Business economics at John Naisbitt University.
Love to blog and read blogs.