Changing the ECS Hostname Using a Script
What Is a Hostname?
A hostname is used to identify a computer or server on a network. It helps you and network systems identify and access a specific device.
Hostnames are used in the following scenarios:
- Identifying ECSs: Hostnames help distinguish different devices. You can plan hostnames based on the location, usage, and owner of ECSs to easily identify different ECSs.
- Identifying the source of logs and monitoring data: Hostnames can be used to identify the source of incidents in logs and monitoring data and accurately locate the ECSs where incidents occur. This reduces the time required for system analysis or troubleshooting and improves O&M efficiency.
- Simplifying configuration management: You can use hostnames instead of IP addresses when configuring network services, applications, and system settings. For example, Configuring Mapping Between Hostnames and IP Addresses in the Same VPC.
Background
In the Huawei Cloud ECS O&M scenario, the hostname is the core field for server identification, log search, alarm monitoring, cluster management, and automated O&M. When an ECS is created, the hostname is injected by Cloud-Init. To meet the requirements of service expansion, environment normalization, cluster migration, and compliance audit, you need to change the static hostnames one by one or in batches. Ensure that no hostname is lost after the restart, the console is the same as that in the system, and service stability is not affected.
Traditional manual modification (using hostnamectl or editing /etc/hostname) has the following pain points:
- After the restart, the new hostname is restored to original name by Cloud-Init and cannot be persisted.
- The console is inconsistent with that in the OS, and O&M identification is disordered.
- The batch operation efficiency is low and error-prone.
- The modification fails due to missing permissions and dependencies.
Solutions
The COC and batch script execution are used to modify ECS hostnames in batches online.
The solution utilizes COC as a centralized O&M entry point. By deploying pre-configured or custom standardized Shell scripts, it executes batch operations on target ECS instances to handle tasks such as hostname modification, configuration persistence, and reboot protection. The entire process requires no individual server login, achieving batch processing with controllable processes, traceable results, and preventable risks.
The core advantages of the solution are as follows:
- Batch Execution: Select multiple ECS instances at once and dispatch scripts uniformly, significantly boosting O&M efficiency.
- Standardization: Scripts solidify the modification logic, eliminating anomalies caused by discrepancies in manual commands.
- Auditability: COC records execution logs, results, and targets, fully satisfying audit requirements.
Constraints
- UniAgent must be installed. UniAgent is a unified data collection agent that supports script delivery and execution.
If UniAgent is not installed on the ECS, the hostname cannot be changed. To install UniAgent, see Installing UniAgent on an ECS.
- You can only change the hostname of a Linux ECS.
Step 1: Creating a Custom Script
- Log in to COC.
- In the navigation pane, choose Resource O&M > Automated O&M.
- In the Routine O&M area, click Scripts.
- On the Custom Scripts tab page, click Create Script.
- Set basic script information by referring to Table 1.
Table 1 Basic information parameters Parameter
Example Value
Description
Script Name
Changing the VM Hostname
Specify the script name based on the naming rules.
The value can contain 3 to 100 characters, including letters, digits, hyphens (-), and underscores (_).
Enterprise Project
default
Select the enterprise project to which the script belongs from the drop-down list.
After the script is created, the enterprise project cannot be modified.
Version
1.0.0
The preset version number is 1.0.0. You can customize the script version number. The version number format is x.x.x.
Risk Level
High
Select High, Medium, or Low as required.
Description
Change the hostname of the VM.
Enter a description about the script.
The value can contain 1 to 256 characters.
- Set the script type to Shell. The script content is as follows:
#!/bin/bash # Obtain the hostname from the input parameter. set +x hostname=${hostname} # Change the hostname. hostnamectl set-hostname "$hostname" retCode=$? if [ $retCode -eq 0 ]; then echo "Modify hostname to ${hostname} successfully." else echo "Modify hostname to ${hostname} failed." exit 1 fi - Set the script input parameter. Set the input parameter name to hostname, and the input parameter value to ECS-host-01. The hostname must only contain letters, numbers, periods (.), and hyphens (-). It cannot start or end with a period or hyphen, and consecutive periods or hyphens are not allowed.Figure 1 Setting script input parameters
- Disable Manual Review.
This example only demonstrates the basic functions of scripts. For routine O&M, you are advised to configure an approval process to evaluate operation risks.
- Click OK.
After the script is created, you can view the basic information about the script in the script list.
Figure 2 Viewing script information
Step 2: Executing Scripts
- On the Custom Script tab page, locate the script created in Step 1: Creating a Custom Script and click Execute in the Operation column.
- Click Add and select the instance whose host account needs to be modified.
Retain the default values for other parameters.
- Click OK. In the confirmation drawer, confirm the execution information, and click OK to execute the script service ticket.
Wait until the execution progress reaches 100%.
Figure 3 Viewing the script execution service ticket
Step 3: Checking Whether the Hostname Is Changed
- In the navigation pane, choose Resource O&M > Batch Resource Operations.
- On the displayed page, choose the Elastic Cloud Server (ECS) tab. Click Execute Commands.
- Click Add and select the instance whose host account is modified in 2.
- Set the command content.
- Select Linux for OS.
- Select Shell for command type.
- Run the following command:
hostname
Figure 4 Setting the command content
- Click OK to run the command.
After the command is executed, view the command output.
Figure 5 Checking the hostname
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot