【小記】騰訊雲 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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM