【小记】腾讯云 Linux 虚拟机如何正确修改 hosts 文件
如果直接修改 /etc/hosts 文件,重启后设置会丢失还原,原因是腾讯云虚拟机默认使用了 Cloud-Init 进行初始化操作。
参见:https://cloud.tencent.com/document/product/213/34698
官方描述中说 "2018年9月后使用公共镜像创建的实例不会出现 /etc/hosts 配置在重启后被覆盖的问题"。
然而实际还是会被覆盖。
所以就手动修改 Cloud-Init 模板呗。
直接:
cd /etc/cloud/templates
ls
会看到很多 hosts.***.tmpl 文件,根据你的Linux发行版进行修改。
为了防止小白问找不到自己发行版,所以这里解释一下:Ubuntu 是基于 Debian 的衍生版,CentOS7 是 RedHat 源码遵循GPL再编译的版本,CentOS8以后是 RedHat 的试验田版本。
所以这样就明白了吧。假设你是 CentOS,就修改 "hosts.redhat.tmpl"。
sudo vim hosts.redhat.tmpl
根据你的需要进行修改:
## 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
完成后重启服务器就会根据模板重新加载 Hosts。
sudo reboot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具