Updated: Jan 28
You will need the following items to complete this tutorial:
SSH (Secure Shell Access).
Hostname Metadata Key and Value
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.).
Go to the Create an instance page in the Google Cloud Console.
To begin, go to Create an instance.
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.)
Expand the section Networking, disks, security, management, and then do the following:
Expand the section on networking.
Enter the custom hostname in the Hostname field and create the instance.
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 "subdomain.yourdomain.com" represents your desired hostname
hostnamectl set-hostname subdomain.yourdomain.com
hostnamectl set-hostname vpn.incapio.org
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 "subdomain.yourdomain.com" with the hostname of your server:
mkdir -p /etc/dhcp/dhclient-exit-hooks.d/ && echo -ne '#!/bin/sh\nhostname subdomain.yourdomain.com\n/scripts/fixetchosts\n' > /etc/dhcp/dhclient-exit-hooks.d/zzz-set-hostname.sh && chmod +x /etc/dhcp/dhclient-exit-hooks.d/zzz-set-hostname.sh
mkdir -p /etc/dhcp/dhclient-exit-hooks.d/ && echo -ne '#!/bin/sh\nhostname vpn.incapio.org\n/scripts/fixetchosts\n' > /etc/dhcp/dhclient-exit-hooks.d/zzz-set-hostname.sh && chmod +x /etc/dhcp/dhclient-exit-hooks.d/zzz-set-hostname.sh
Close the terminal session and return to the Google Compute Engine interface when you're done.
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.
In the Key input field, type hostname.
In the Value input field, type your hostname(uscentral.incapio.org.in)
In the Automation section, Startup-Script field, paste the following code.
hostname $(curl --silent "http://metadata.google.internal/computeMetadata/v1/instance/attributes/hostname" -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.