2如果直接修改 /etc/hosts 文件,重启后会还原丢失修改,原因是腾讯云默认使用了 Cloud-Init 进行初始化操作。
官方说明参见:https://cloud.tencent.com/document/product/213/34698
官方描述中说 "2018年9月后使用公共镜像创建的实例不会出现 /etc/hosts 配置在重启后被覆盖的问题"。
然而实际还是会被覆盖。
所以就手动修改 Cloud-Init 模板:
1 2 3 4 5 |
cd /etc/cloud/templates ls |
为了防止小白问找不到自己发行版,所以这里解释一下:
Ubuntu 是基于 Debian 的衍生版,CentOS7 是 RedHat 源码遵循GPL再编译的版本,CentOS8 以后是 RedHat 的试验田版本。
所以这样就明白了吧。假设你是 CentOS,就修改 "hosts.redhat.tmpl"。
1 2 3 4 |
sudo vim hosts.redhat.tmpl 根据你的需要进行修改: |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## template:jinja {# This file /etc/cloud/templates/hosts.redhat.tmpl is only utilized if enabled in cloud-config. Specifically, in order to enable it you need to add the following to config: manage_etc_hosts: True -#} 127.0.0.1 {{fqdn}} {{hostname}} 127.0.0.1 localhost.localdomain localhost 127.0.0.1 localhost4.localdomain4 localhost4 ::1 {{fqdn}} {{hostname}} ::1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 |
1 2 3 4 |
sudo reboot |