skip to Main Content

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


  1. Chosen as BEST ANSWER

    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"
    

  2. 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.

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search