一、前提条件
腾讯云针对 Cloud-Init 的覆盖操作已经做了优化,2018年9月后使用公共镜像创建的实例不会出现 /etc/hosts 配置在重启后被覆盖的问题。
若你的实例创建于2018年9月前,请通过下面的解决方案进行修改。
二、方案一
1. 登录 Linux 服务器。
2. 执行以下命令,将 /etc/cloud/cloud.cfg 配置文件中的 – update_etc_hosts 修改为 – ['update-etc-hosts', 'once-per-instance']。
sed -i "/update_etc_hosts/c \\ – ['update_etc_hosts', 'once-per-instance']" /etc/cloud/cloud.cfg
3. 执行以下命令,在 /var/lib/cloud/instance/sem/ 路径下创建 config_update_etc_hosts 文件。
touch /var/lib/cloud/instance/sem/config_update_etc_hosts
三、方案二
注意:说明:
注意:此方案以 CentOS 7.2 操作系统为例。
四、获取 hosts 模板文件路径
1. 登录 Linux 服务器。
2. 执行以下命令,查看系统 hosts 模板文件。
cat /etc/hosts
hosts 模板文件如下:
五、修改 hosts 模板文件
注意:说明:
注意:以添加 127.0.0.1 test test 为例,你可按需修改 hosts 模板文件与 /etc/hosts 文件。
1. 执行以下命令,修改 hosts 模板文件。
vim /etc/cloud/templates/hosts.redhat.tmpl
2. 按 i 切换至编辑模式。
3. 在文件末尾输入以下内容。
127.0.0.1 test test
4. 输入完成后,按 Esc,输入 :wq,保存文件并返回。
六、修改 /etc/hosts 文件
1. 执行以下命令,修改 /etc/hosts 文件。
vim /etc/hosts
2. 按 i 切换至编辑模式。
3. 在文件末尾输入以下内容。
127.0.0.1 test test
4. 输入完成后,按 Esc,输入 :wq,保存文件并返回。