2

Title says it all really - I want to change the hostname of a Google Compute Engine Cent OS instance.

I have tried the normal method...

  • in /etc/sysconfig/network set HOSTNAME=my.new.hostname
  • in /etc/hosts set 127.0.1.1 my.new.hostname
  • reboot

I have tried setting a hostname key/value pair in the Custom metadata of the Compute Engine VM Instance admin page.

Nothing sticks. Seems to use the instance name as hostname.

4

4 Answers 4

2

By default there is a start up script which sets the hostname according to the metadata of that instance.
See: https://github.com/GoogleCloudPlatform/compute-image-packages/tree/master/google-startup-scripts

Sets the hostname from the metadata server via DHCP exit hooks.
1
  • I'm getting 404 page. And is there a way to change that? I'd like that the hostname to be a FQDN hostname.
    – el.severo
    Jul 2, 2016 at 16:43
1

Have you tried hostname(1) ?

Hostname is the program that is used to either set or display the current host, domain or node name of the system. These names are used by many of the networking programs to identify the machine.

hostname
xyzzy.example.com

hostname plugh.example.com

hostname
plugh.example.com
1
  • I did try that, and it works until the machine is rebooted :(
    – Billy Moon
    Apr 13, 2014 at 14:34
0

Change hostname in Google Compute Engine (Cent OS)

Step 1

# nano /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=my.new.hostname

Step 2: Create crontab auto set Hostname

# crontab -e
@reboot hostname="my.new.hostname"; sed -i "s/.*Google.*//" /etc/hosts; hostname "$hostname"

Step 3

# reboot

Step 4: Add below

# nano /etc/hosts
127.0.0.1    my.new.hostname
0

For those who want to have a permanent hostname (for CentOS 6 & 7):

# cd /etc/dhcp/dhclient-exit-hooks.d/
# ls
azure-cloud.sh
# touch update-hostname.sh
# nano update-hostname.sh # or you can use vi
# cat update-hostname.sh 
hostname my.fqdndomain.com
# chmod a+x update-hostname.sh

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .