Introduction

Many cloud hosting providers, including Google Cloud Platform™, use the dhclient script to configure VPS deployments.

However, this script may not preserve the locally-configured hostname, leading to conflicts with WHM's Change Hostname feature and causing a locked cPanel & WHM license.

 

The Problem

The dhclient script's inability to preserve the hostname can lead to interference with WHM's Change Hostname feature, resulting in hostname configuration issues.

 

The Solution

To resolve this issue, you must create a dhclient exit hook script to set the hostname properly.

Here's a step-by-step guide:

1. Create the Required Directories

Command: mkdir -p /etc/dhcp/dhclient-exit-hooks.d/

2. Create and Write to the Exit Hook Script

Command: echo -ne '#!/bin/sh\nhostname hostname.example.com\n/scripts/fixetchosts\n' > /etc/dhcp/dhclient-exit-hooks.d/zzz-set-hostname.sh

3. Make the Script Executable

Command: chmod +x /etc/dhcp/dhclient-exit-hooks.d/zzz-set-hostname.sh

This solution ensures proper hostname configuration and avoids conflicts with WHM's Change Hostname feature, thus unlocking the cPanel & WHM license.

But it is not enough to know how to make this happen, so let's break this down further.

 

Command Breakdown

The command you provided is a composite of three commands, tied together using the logical AND (&&) operator.

Here's the breakdown:

mkdir -p /etc/dhcp/dhclient-exit-hooks.d/

  • mkdir: This command is used to create directories.
  • -p: This option allows the creation of parent directories as needed. If the directory already exists, no error is produced. It ensures that the entire path /etc/dhcp/dhclient-exit-hooks.d/ is created, even if some of the parent directories do not exist.
  • /etc/dhcp/dhclient-exit-hooks.d/: This is the directory path that will be created.

echo -ne '#!/bin/sh\nhostname hostname.example.com\n/scripts/fixetchosts\n' > /etc/dhcp/dhclient-exit-hooks.d/zzz-set-hostname.sh

  • echo: This command is used to print text or variables.
  • -n: This option suppresses the newline at the end of the output, ensuring the string is printed exactly as given.
  • -e: This option enables the interpretation of escape sequences like \n, which represents a newline.
  • #!/bin/sh\nhostname hostname.example.com\n/scripts/fixetchosts\n': This is the string being printed. It's a shell script that sets the hostname and then calls another script.
  • >: This symbol is used to redirect the output, overwriting the file if it exists.
  • /etc/dhcp/dhclient-exit-hooks.d/zzz-set-hostname.sh: This is the file path where the output string will be saved.

chmod +x /etc/dhcp/dhclient-exit-hooks.d/zzz-set-hostname.sh

  • chmod: This command is used to change file permissions.
  • +x: This option adds executable permissions to the file, allowing it to be run as a script.
  • /etc/dhcp/dhclient-exit-hooks.d/zzz-set-hostname.sh: This is the file path to which the executable permissions are being applied.

Summary:

  1. Create the directory /etc/dhcp/dhclient-exit-hooks.d/, including any necessary parent directories.
  2. Create or overwrite a shell script file in that directory with specific content that sets the hostname and calls another script.
  3. Make the newly created shell script file executable.

These commands are linked with the && operator, meaning that each subsequent command will only be executed if the previous command succeeds (returns a zero exit status). If any command fails, the remaining commands will not be executed.

 

Amazon EC2 instance running RHEL 7 or CentOS 7

To make a hostname persist when you reboot or stop and start an EC2 instance, add the hostname to the appropriate configuration files on the instance. Please note that the following steps apply to RHEL 7 or CentOS 7.

Run the hostnamectl command from the shell to update the hostname of your RHEL 7 or CentOS 7 Linux instance. Replace persistent-hostname with the name you want the instance to use:

sudo hostnamectl set-hostname --static persistent-hostname

Note: Changing the hostname by using the hostnamectl command doesn't update the /etc/hosts file. However, if necessary, you can manually edit this file to add the new hostname.

Use the vi text editor to update the /etc/cloud/cloud.cfg file on your RHEL 7 or CentOS 7 Linux instance. For general information about using the vi editor, run the command info vi from the command shell:

Note: For instances running RHEL 7.4 and above, skip this step.

sudo vi /etc/cloud/cloud.cfg

Add the following string to the bottom of the /etc/cloud/cloud.cfg file to be sure that the new hostname is preserved between restarts and reboots:

preserve_hostname: true

Save and exit the vi editor.

Run the reboot command to reboot the EC2 instance and associate the new hostname with the instance:

sudo reboot

Run the Linux hostname command without any parameters to verify that the hostname change persisted:

hostname

Or send ICMP ECHO_REQUEST to network using the ping command to verify the new hostname.

 

Amazon EC2 instance running Ubuntu Linux

Use vim to open the /etc/hosts file.

sudo vim /etc/hosts

Update the /etc/hosts file to include your persistent hostname for localhost, similar to the following:

127.0.0.1 localhost $myserver-hostname

Note: You might have to create an entry for localhost if the /etc/hosts file on your EC2 instance doesn't have an entry for it.

For more information about the hosts file on Ubuntu, check the Ubuntu hosts file manpage.

If your EC2 instance uses IPv6, add the following configuration data.

::1 ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts

Save and exit the vim editor.

Note: After making this change, press SHIFT+:[colon] to open a new command entry box in the vim editor.

Type wq, and then press Enter to save changes and exit vim.

Or use SHIFT + ZZ to save and close the file.

Run the hostnamectl command and specify the new hostname. Replace the persistent-hostname with the new hostname.

sudo hostnamectl set-hostname persistent-hostname

After you start or reboot the EC2 instance, run the Linux hostname command without any parameters to verify that the hostname change persisted.

hostname

Conclusion

By understanding and implementing this solution, you can enhance the stability and functionality of your VPS deployments, fostering a more reliable hosting environment.

If you need help, contact our support team for help.

Răspunsul a fost util? 0 utilizatori au considerat informația utilă (0 Voturi)