Este conteúdo foi traduzido por máquina para sua conveniência e a Huawei Cloud não pode garantir que o conteúdo foi traduzido com precisão. Para exibir o conteúdo original, use o link no canto superior direito para mudar para a página em inglês.
Central de ajuda/ Elastic Cloud Server/ Perguntas frequentes/ ECS Management/ Hostnames/ Como um nome do host estático alterado pode ter efeito permanente?
Atualizado em 2025-01-23 GMT+08:00

Como um nome do host estático alterado pode ter efeito permanente?

Sintoma

O nome do host estático de um ECS de Linux é definido pelo usuário e injetado usando Cloud-Init durante a criação do ECS. Embora o nome do host possa ser alterado executando o comando hostname, o nome do host alterado é restaurado depois que o ECS é reiniciado.

Alteração do nome do host no ECS

Para que o nome do host alterado ainda tenha efeito mesmo depois que o ECS for interrompido ou reiniciado, salve o nome do host alterado nos arquivos de configuração.

O nome do host alterado é assumido como new_hostname.

  1. Modifique o arquivo de configuração /etc/hostname.
    1. Execute o seguinte comando para editar o arquivo de configuração:

      sudo vim /etc/hostname

    2. Altere o nome do host para o novo.
    3. Execute o seguinte comando para salvar e sair do arquivo de configuração:

      :wq

  2. Modifique o arquivo de configuração /etc/sysconfig/network.
    1. Execute o seguinte comando para editar o arquivo de configuração:

      sudo vim /etc/sysconfig/network

    2. Altere o valor de HOSTNAME para o novo nome do host.

      HOSTNAME=Changed hostname

      Se não houver nenhum HOSTNAME no arquivo de configuração, adicione manualmente esse parâmetro e defina-o como o nome de host alterado.

      Por exemplo:

      HOSTNAME=new_hostname
    3. Execute o seguinte comando para salvar e sair do arquivo de configuração:

      :wq

  3. Modifique o arquivo de configuração /etc/cloud/cloud.cfg.
    1. Execute o seguinte comando para editar o arquivo de configuração:

      sudo vim /etc/cloud/cloud.cfg

    2. Use um dos seguintes métodos para modificar o arquivo de configuração:
      • Método 1: altere o valor do parâmetro preserve_hostname ou adicione o parâmetro preserve_hostname ao arquivo de configuração.

        Se preserve_hostname: false já estiver disponível no arquivo de configuração /etc/cloud/cloud.cfg, altere-o para preserve_hostname: true. Se preserve_hostname não estiver disponível no arquivo de configuração /etc/cloud/cloud.cfg, adicione preserve_hostname: true antes de cloud_init_modules.

        Se você usar o método 1, o nome do host alterado ainda terá efeito depois que o ECS for interrompido ou reiniciado. No entanto, se o ECS for usado para criar uma imagem privada e a imagem for usada para criar um novo ECS, o nome do host do novo ECS será o nome do host (new_hostname) usado pela imagem privada, e os nomes de host definidos pelo usuário não poderão ser injetados usando Cloud-Init.

      • Método 2 (recomendado): exclua ou comente - update_hostname.

        Se você usar o método 2, o nome do host alterado ainda terá efeito depois que o ECS for interrompido ou reiniciado. Se o ECS for usado para criar uma imagem privada e a imagem for usada para criar um novo ECS, o nome do host alterado entrará em vigor permanentemente, e nomes de host definidos pelo usuário (como new_new_hostname) poderão ser injetados usando Cloud-Init.

  1. Execute o seguinte comando para reiniciar o ECS:

    sudo reboot

  2. Execute o seguinte comando para verificar se o nome do host foi alterado:

    sudo hostname

    Se o nome do host alterado for exibido na saída do comando, o nome do host foi alterado e o novo nome entrará em vigor permanentemente.

Modifying the Mapping Between the ECS Hostname and IP Address (Modifying the hosts File)

If you want to use the changed hostname as the preferred localhost and localhost.localdomain, update the mapping between the hostname and IP address after the hostname is changed and then save the configuration to the corresponding Cloud-Init configuration file so that the new hostname takes effect permanently.

The changed hostname is assumed to be new_hostname.

  1. Modify the /etc/hostname configuration file.
    1. Run the following command to edit the configuration file:

      sudo vim /etc/hostname

    2. Change the hostname to the new one.
    3. Run the following command to save and exit the configuration file:

      :wq

  2. Modify the /etc/sysconfig/network configuration file.
    1. Run the following command to edit the configuration file:

      sudo vim /etc/sysconfig/network

    2. Altere o valor de HOSTNAME para o novo nome do host.

      HOSTNAME=Changed hostname

      If there is no HOSTNAME in the configuration file, manually add this parameter and set it to the changed hostname.

      For example:

      HOSTNAME=new_hostname
    3. Run the following command to save and exit the configuration file:

      :wq

  3. Modify the /etc/cloud/cloud.cfg configuration file.
    1. Run the following command to edit the configuration file:

      sudo vim /etc/cloud/cloud.cfg

    2. Use either of the following methods to modify the configuration file:
      • Method 1: Change the preserve_hostname parameter value or add the preserve_hostname parameter to the configuration file.

        If preserve_hostname: false is already available in the /etc/cloud/cloud.cfg configuration file, change it to preserve_hostname: true. If preserve_hostname is unavailable in the /etc/cloud/cloud.cfg configuration file, add preserve_hostname: true before cloud_init_modules.

        If you use method 1, the changed hostname still takes effect after the ECS is stopped or restarted. However, if the ECS is used to create a private image and the image is used to create a new ECS, the hostname of the new ECS is the hostname (new_hostname) used by the private image, and user-defined hostnames cannot be injected using Cloud-Init.

      • Method 2 (recommended): Delete or comment out - update_hostname.

        If you use method 2, the changed hostname still takes effect after the ECS is stopped or restarted. If the ECS is used to create a private image and the image is used to create a new ECS, the changed hostname permanently takes effect, and user-defined hostnames (such as new_new_hostname) can be injected using Cloud-Init.

  1. Update the mapping between the hostname and IP address in /etc/hosts to an entry starting with 127.0.0.1. Use new_hostname as your preferred localhost and localhost.localdomain.
    1. Run the following command to edit /etc/hosts:

      sudo vim /etc/hosts

    2. Modify the entry starting with 127.0.0.1 and replace localhost and localhost.localdomain with new_hostname.
      ::1     localhost       localhost.localdomain   localhost6      localhost6.localdomain6
      127.0.0.1       localhost       localhost.localdomain   localhost4      localhost4.localdomain4
      127.0.0.1       new_hostname    new_hostname
    3. Run the following command to save and exit the configuration file:

      :wq

  2. Modify the /etc/cloud/cloud.cfg configuration file.
    1. Run the following command to edit the configuration file:

      sudo vim /etc/cloud/cloud.cfg

    2. Set manage_etc_hosts to manage_etc_hosts: false.
      manage_etc_hosts: false
    3. Run the following command to save and exit the configuration file:

      :wq

  1. Run the following command to restart the ECS:

    sudo reboot

  2. Run the following commands to check whether the changes to hostname and hosts take effect permanently:

    sudo hostname

    sudo cat /etc/hosts

    If the changed hostname (new_hostname) and hosts are displayed in the command output, the changes take effect permanently.