2

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

neilH
  • 3,320
  • 1
  • 17
  • 38

4 Answers4

0

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:

sudo crontab -e

Then enter this line for Centos:

@reboot sleep 15 && hostnamectl set-hostname YOUR_HOSTNAME

For Debian this worked:

@reboot hostnamectl set-hostname YOUR_HOSTNAME

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.

neilH
  • 3,320
  • 1
  • 17
  • 38
  • i will check it with delay.. since with no delay it did not work. Thanks – Bilal SmartClick Apr 15 '18 at 16:19
  • i test it, it auto change now even without reboot operating system. – Bilal SmartClick Apr 17 '18 at 05:38
  • Okay, so just to clarify, it changes to your desired hostname? Has is solved your issue? – neilH Apr 18 '18 at 07:50
  • no it changed automatic after time -without restart- to the old non FQDN hostname [google instance]. – Bilal SmartClick Apr 19 '18 at 07:34
  • Maybe try the accepted answer here, but with Centos experiment with the delay in the crontab if it doesn't work initially https://stackoverflow.com/questions/25408612/google-compute-engine-how-to-set-hostname-permanently?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – neilH Apr 19 '18 at 10:30
  • i check the link, it just say to add to crontab @reboot hostname host.domain.com, my problem is also differ than after reboot it auto change. now it also auto change without restart. Thanks.. – Bilal SmartClick Apr 19 '18 at 11:43
  • I'm out of ideas. The steps in my answer works with my VM running Centos7. Mines is a new VM. If yours is behaving differently, maybe you have configured something different. You could try this on a brand new Centos7 machine as a troubleshooting step. – neilH Apr 27 '18 at 15:09
0

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:

cd /etc/dhcp/dhclient-exit-hooks.d/
nano set_my_hostname.sh

then inside the file put:

hostname hosting.domain.com

save the file and make it executable:

chmod +x set_my_hostname.sh

and to fix, hostname automatic change after reboot, create a cronjob to start at reboot with delay
(thanks neilH for his help):

sudo env EDITOR=nano crontab -e

then add this line:

@reboot sleep 20 && hostnamectl set-hostname "hosting.domain.com"
0

This worked for me, I wanted my hostname to be a subdomain ie: server1.example.com:

1: Change /etc/hosts file add:

127.0.0.1       localhost.localdomain   localhost
192.168.1.100   server1.example.com     server1

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:

supersede host-name "server1.example.com";

4: Create a cron job: run sudo crontab -e then add:

@reboot hostnamectl set-hostname server1.example.com

5: sudo reboot

Tian Reagon
  • 118
  • 2
  • 11
0

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:

127.0.0.1 localhost

Add your new hostname to the end of the line, so it should look like this:

127.0.0.1 localhost <new_hostname>

3: Open /etc/rc.local (sudo nano /etc/rc.local). Before the line exit 0, add another line:

hostname <new_hostname>

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.

Adithya
  • 25
  • 3