I’ve created a Google instance in Google Compute Engine with CentOS operating system, then I installed Cpanel. My problem is with WHM/Cpanel, it needs a hostname to be FQDN hostname, specifically for updating Cpanel or it will fail.
My problem is that after changing the hostname the instance reverts back to the old hostname after rebooting the operating system or resetting/stopping/starting the instance.
I’ve checked most questions before and I’ve tried most of the solutions with no luck. It keeps changing after reboot, I’ve try all the methods below and more:
create sh script in:
/etc/dhcp/dhclient-exit-hooks.d/
change hostname in
/etc/hostname
edit file
/etc/dhclient.conf
then add inside it, for my network interface:
supersede host-name "host.domain.com"
in crontab
add to the end:
@reboot hostname="host.domain.com"; sed -i "s/.*Google.*//" /etc/hosts; hostname "$hostname"
But after reboot, the hostname changes back to the instance name.
Is there any other workaround to permanently change my hostname even after reboot.?
Thanks
4
Answers
Problem of automatic change hostname without restart solve it by create an ".sh" executable file in "/etc/dhcp/dhclient-exit-hooks.d/", ex: below we create file "set_my_hostname.sh", you can create an sh file with any name:
then inside the file put:
save the file and make it executable:
and to fix, hostname automatic change after reboot, create a cronjob to start at reboot with delay
(thanks neilH for his help):
then add this line:
You could create a similar crontab entry, but instead of using the line in your post, you could use
hostnamectl
to set the hostname on start-up.I’ve tested this with Google’s Centos7 and Debian9 images and it works for both. However, I found that with Centos, I had to add a delay before the commands execution (see below).
So for example, open crontab:
Then enter this line for Centos:
For Debian this worked:
I didn’t experiment too much with the crontab Centos timings (you may be able to use a lower figure than 15 seconds), but from my experience, using @reboot alone didn’t seem to initiate the change on start-up.
This worked for me, I wanted my hostname to be a subdomain ie: server1.example.com:
1: Change
/etc/hosts file
add:2: Change
etc/hostname
file (if doesn’t exist create it):add just the sub-domain part ie:
server1
3: Change
/etc/dhcp/dhclient.conf
add:4: Create a
cron job
:run sudo crontab -e
then add:5:
sudo reboot
This worked for me in a GCE instance running Ubuntu 16.04:
1: Open
/etc/hostname
(sudo nano /etc/hostname
) and change the hostname to the new one.2: Open
/etc/hosts
(sudo nano /etc/hosts
). The first line will probably be:Add your new hostname to the end of the line, so it should look like this:
3: Open
/etc/rc.local
(sudo nano /etc/rc.local
). Before the lineexit 0
, add another line:4: That’s it! The hostname has been changed permanently. You can either open a new bash shell by running
bash
or reboot the instance.