如何有效的修改 Linux 云服务器的 etc/hosts 配置
操作场景
在2018年3月1号之后,站狐云官网提供的 Linux 公共镜像预安装了纯开源的工具 Cloud-Init,并通过 Cloud-Init 实现了实例的所有初始化操作,使得整个实例内部的操作更加的透明,详情请参见 Cloud-Init。
Cloud-Init 在 每次启动 时会根据 /etc/cloud/templates/hosts.${os_type}.tmpl
模板生成一份新的 /etc/hosts
文件覆盖实例原有的 /etc/hosts
文件,导致用户在实例内部手动修改 /etc/hosts
配置并重启实例后, /etc/hosts
配置又变为原始默认配置。
前提条件
站狐云针对 Cloud-Init 的覆盖操作已经做了优化, 2018年9月后使用公共镜像 创建的实例不会出现 /etc/hosts
配置在重启后被覆盖的问题。
若您的实例创建于 2018年9月前,请通过下面的解决方案进行修改。
操作步骤
方案一
-
登录 Linux 服务器。
-
执行以下命令,将
/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
- 执行以下命令,在
/var/lib/cloud/instance/sem/
路径下创建config_update_etc_hosts
文件。
touch /var/lib/cloud/instance/sem/config_update_etc_hosts
方案二
说明: 此方案以 CentOS7.2 操作系统为例。
获取 hosts 模版文件路径
-
登录 Linux 服务器。
-
执行以下命令,查看系统 hosts 模版文件。
cat /etc/hosts
hosts 模版文件如下图所示:
修改 hosts 模版文件
说明:以添加 127.0.0.1 test test 为例,您可按需修改 hosts 模版文件与 /etc/hosts 文件。
- 执行以下命令,修改 hosts 模版文件。
vim /etc/cloud/templates/hosts.redhat.tmpl
-
按 “ i” 切换至编辑模式。
-
在文件末尾输入以下内容。
127.0.0.1 test test
- 输入完成后,按 “ Esc” ,输入 :wq,保存文件并返回。
修改 /etc/hosts 文件
- 执行以下命令,修改
/etc/hosts
文件。
vim /etc/hosts
-
按 “ i” 切换至编辑模式。
-
在文件末尾输入以下内容。
127.0.0.1 test test
- 输入完成后,按 “ Esc” ,输入 :wq,保存文件并返回。