Incapio - Blog Posts © 2022 by Incapio is licensed under CC BY-SA 4.0 

How to change the hostname of a Compute Engine (VM) Instance in Google Cloud (GCP) ?

Updated: Jan 28


You will need the following items to complete this tutorial:

A Compute Engine's hostname can be changed or updated in several ways:

  • Creating a VM instance with a custom hostname.

  • Create a hook to fix the hostname.

  • Assign a hostname with Metadata.

What is a hostname?

On a network, a device is referred to by its hostname. This can also be referred to as a computer name or a site name. The hostname is used to differentiate devices on a local network. Furthermore, computers can be found by others using the hostname, allowing for data transmission within a network, for example. On the internet, hostnames are used as part of a fully qualified domain name.

Method - 1. Creating a VM instance with a custom hostname.

Google Cloud generates an internal DNS name based on the virtual machine name when creating a virtual machine instance. Unless you give a different hostname, Google Cloud utilizes the automatically generated internal DNS name as the hostname it assigns to the VM.

You can build a virtual machine with a custom hostname by specifying any fully qualified DNS name. Custom hostnames are important for maintaining customs or meeting the needs of applications that demand a specific hostname.

Even if you choose a custom hostname, you must still create a corresponding DNS record in the relevant zone, for example, by utilizing Cloud DNS or where the domain is currently registered (Relevant DNS Providers, Such as GoDaddy, and so forth.).

Compute Engine hostname, GCE hostname, hostnames, networking, GCE change hostname, Google Compute Engine change hostname
Google Compute Engine VM instance Interface
  1. Go to the Create an instance page in the Google Cloud Console.

  2. To begin, go to Create an instance.

  3. Give your virtual machine a name. (The name must begin with a lowercase letter and contain up to 62 lowercase letters, numerals, or hyphens. It cannot finish with a hyphen.)

  4. Expand the section Networking, disks, security, management, and then do the following:

  5. Expand the section on networking.

  6. Enter the custom hostname in the Hostname field and create the instance.

  7. Verify the custom hostname

Method - 2: Create a hook to fix the hostname.

  • Open Secure Shell access (SSH) on the VM instance

  • Run the following command to assign a hostname to the server, where "" represents your desired hostname


hostnamectl set-hostname


hostnamectl set-hostname
  • Create dhclient script to configure virtual machine deployments. The configuration of the instance's hostname is included in this script. However, the locally specified hostname may not be preserved by the client script. As a result, Google Cloud (GCP) utilizes scripts to get around this problem. The google set-hostname script, for example, is used by Google Cloud Platform services.

  • To make the hook, use the following command, replacing "" with the hostname of your server:


mkdir -p /etc/dhcp/dhclient-exit-hooks.d/ && echo -ne '#!/bin/sh\nhostname\n/scripts/fixetchosts\n' > /etc/dhcp/dhclient-exit-hooks.d/ && chmod +x /etc/dhcp/dhclient-exit-hooks.d/


mkdir -p /etc/dhcp/dhclient-exit-hooks.d/ && echo -ne '#!/bin/sh\nhostname\n/scripts/fixetchosts\n' > /etc/dhcp/dhclient-exit-hooks.d/ && chmod +x /etc/dhcp/dhclient-exit-hooks.d/

Method - 3: Assign a hostname to New or Existing VM instance with Metadata and Startup-Script.

  • Log in to the Google Cloud Console, navigate to Compute Engine VM instance, and click on the "Create Instance" option to create a new VM instance.

  • Now, scroll down until your notice the "Networking, Disks, Security, Management and Sole-Tenancy" option and expand it.

  • Click on the "Add Item" option in the Management Section under the Metadata.

GCE hostname, Compute Engine Hostname, metadata, startup-script
Google Compute Engine Management Interface
  • In the Key input field, type hostname.

  • In the Value input field, type your hostname(

  • Syntax:
  • Example:
  • In the Automation section, Startup-Script field, paste the following code.

hostname $(curl --silent "" -H "Metadata-Flavor: Google")
  • Finally, Click on the "Create" option to deploy a new VM instance.

  • Once the VM deployment is done, open secure shell access(SSH) on the VM instance and type hostname in the terminal to verify the hostname.

1,316 views0 comments

Do you need assistance? Visit Forum to join the conversation.