tencent cloud

Cloud Virtual Machine

Release Notes and Announcements
Release Notes
Public Image Release Notes
Official End of Support Plan for the Operating System
Announcements
Product Introduction
CVM Overview
Strengths
Basic Concepts
Regions and Zones
Tutorial
Service Regions and Service Providers
Billing
Billing Overview
Billing Modes
Billing Items
Billing Mode Conversion Overview
Purchasing Instances
Configuration Adjustment Billing Guide
Overdue Payments
Getting Started
Purchasing a Customized Linux Instance
Purchasing a Customized Windows Instance
User Guide
Operation Guide Overview
Use Limits
Instances
Spot Instances
Reserved Instances
Images
Storage
Backup and Restoration
Network
Security
Passwords/Keys
Monitoring and Alarms
Ops Management
Convenience Features
Migrating a Server
Online Migration
Migration Consultation
Troubleshooting
CVM Login Failures
Windows Instance Login Failures
Linux Instance Login Failures
Other Login Failures
Instance Running Failures
Linux Instance Memory Failures
Network Failures
Use Cases
Suggestions on CVM Model Selection
Environment Building
Website Building
Application Building
Visual GUI Building
Uploading Local Files to CVM
Network Performance Test
Other Practical Tutorials
API Documentation
History
Introduction
API Category
Making API Requests
Region APIs
Instance APIs
Cloud Hosting Cluster APIs
Image APIs
Instance Launch Template APIs
Placement Group APIs
Key APIs
Security Group APIs
Network APIs
Data Types
Error Codes
Security and Compliance
CAM
Network
FAQs
Regions and Zones
Billing
Instances
Storage
Images
Server Migration
Network
Security
Operating Systems
Ops and Monitoring
CAM
NTP Service
Scenarios
Agreements
CVM Service Level Agreements
Red Hat Enterprise Linux Image Service Agreement
Public IP Service Level Agreement
Glossary
DocumentationCloud Virtual MachineUser GuideOps ManagementSystem OpsModifying the etc/hosts File Configurations of a Linux Instance

Modifying the etc/hosts File Configurations of a Linux Instance

PDF
Focus Mode
Font Size
Last updated: 2024-01-06 17:38:11

Overview

From March 1, 2018, the Linux public image provided by Tencent Cloud has the open-source tool Cloud-Init pre-installed, and all initialization operations on an instance are done via Cloud-Init, making operations inside the instance more transparent. For more information, see Cloud-Init and Cloudbase-Init. In each launch, Cloud-Init generates a new /etc/hosts file according to the /etc/cloud/templates/hosts.${os_type}.tmpl template and overwrites the original /etc/hosts file of the instance involved. Hence, after you manually modify the internal /etc/hosts configuration of the instance and restart it, the /etc/hosts configuration goes back to the original default configuration.

Precautions

Tencent Cloud has fixed this problem for instances created by using a public image after September 2018, and the /etc/hosts configuration will not be overwritten. For instance created before September 2018, follow the steps below for modification.

Directions

Solution 1

1. Log in to the Linux CVM.
2. Execute the following command to change the - update_etc_hosts in the /etc/cloud/cloud.cfg configuration file to - ['update-etc-hosts', 'once-per-instance']:
sed -i "/update_etc_hosts/c \\ - ['update_etc_hosts', 'once-per-instance']" /etc/cloud/cloud.cfg
3. Execute the following command to create a config_update_etc_hosts file under the /var/lib/cloud/instance/sem/ path:
touch /var/lib/cloud/instance/sem/config_update_etc_hosts

Solution 2

Note:
This solution takes the CentOS 7.2 operating system as an example.

Obtaining the hosts template file path

1. Log in to the Linux CVM.
2. Execute the following command to view the system hosts template file:
cat /etc/hosts
The hosts template file is as shown in the following figure:



Modifying the hosts template file

Note:
Taking adding 127.0.0.1 test test as an example, you can modify the hosts template and /etc/hosts file as needed.
1. Execute the following command to modify the hosts template file:
vim /etc/cloud/templates/hosts.redhat.tmpl
2. Press i to switch to the editing mode.
3. Add the following content to the end of the file:
127.0.0.1 test test
4. Press Esc and enter :wq to save and close the file.

Modifying the /etc/hosts file

1. Execute the following command to modify the /etc/hosts file:
vim /etc/hosts
2. Press i to switch to the editing mode.
3. Add the following content to the end of the file:
127.0.0.1 test test
4. Press Esc and enter :wq to save and close the file.

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback