How to effectively modify the etc/hosts configuration of Linux C

Last Updated At: 2025-11-26 11:31:02

Overview

Converge Cloud are pre-installed with the purely open-source Cloud-Init tool, which enables the initialization operations of all instances and increases the transparency of the internal processes of the instances.
Cloud-Init generates a new /etc/hosts file based on the template located at /etc/cloud/templates/hosts.${os_type}.tmplevery time the instance is started, which overwrites the original /etc/hosts file. This means that if you manually modify the /etc/hosts configuration inside the instance and restart the instance, the /etc/hosts configuration will revert to its default state.

Directions

Option I

  1. Log in to the Linux server.

  2. Execute the following command to modify the /etc/cloud/cloud.cfg configuration file by changing - update_etc_hosts 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 the config_update_etc_hosts file under the path /var/lib/cloud/instance/sem/.

    touch /var/lib/cloud/instance/sem/config_update_etc_hosts
    

Option II

Note:

This solution takes the CentOS7.2 operating system as an example.

Retrieve the hosts template file path

  1. Log in to the Linux server.

  2. Execute the following command to view the system's hosts template file.

    cat /etc/hosts
    

    The hosts template file path will be displayed:

Modifying Hosts Template File

Note:

Take adding 127.0.0.1 test test as an example. You can modify the hosts template file 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 edit mode.

  3. Add the following content at the end of the file.

    127.0.0.1 test test
    
  4. After entering, press "Esc " , input :wq to save the file and exit.

Modifying /etc/hosts File

  1. Execute the following command to modify /etc/hosts file.

    vim /etc/hosts
    
  2. Press "i ” to switch to edit mode.

  3. Add the following content at the end of the file.

    127.0.0.1 test test
    
  4. After entering, press "Esc " , input :wq to save the file and exit.