VMware ESXi change /etc/hosts by using the ESXCLI
Because of changing the IP addresses of my ESXi Hosts and the vCenter appliance, I also wanted to adjust the local /etc/hosts file on all of them to be sure they can resolve the FQDNs of my vSphere lab environment without a DNS server.
When opening the /etc/hosts file as usual for Linux you will get notified that you should not directly modify this file and instead using the esxcli.
Do not modify this file directly, please use esxcli.
The reason for is that from ESXi Host version 7.0 U3 the content of /etc/hosts and /etc/resolv.conf are moved to the configstore.
They are no longer sticky bit file. Direct update using text editor will not be Persistent across reboot. User should use public cli or API to update them.
Source: https://knowledge.broadcom.com/external/article/318036/changes-to-esxi-70-u3-hosts-etchosts-or.html
To add mapping to the /etc/hosts file on ESXi Hosts we can use the esxcli network ip hosts add command.
In case we want to add FQDNs + hostname we need to use quotes as shown below.
[root@ESXi-01:~] esxcli network ip hosts add --hostname "ESXi-02.matrixpost-lab.net ESXi-02" --ip 10.0.0.51 [root@ESXi-01:~] esxcli network ip hosts add --hostname "vCenter.matrixpost-lab.net vCenter" --ip 10.0.0.52
To remove mapping we can use esxcli network ip hosts remove.
[root@ESXi-01:~] esxcli network ip hosts remove --hostname "ESXi-02.matrixpost-lab.net ESXi-02" --ip 10.0.0.51
To check the current IP configuration on the ESXi Host we can use the following command.
[root@ESXi-01:~] esxcli network ip interface ipv4 get
You can also use the ESXCLI to configure DNS. As mentioned also /etc/resolv.conf is since version 7.0 U3 moved to the configstore and direct update using a text editor will not be persistent across reboot.
List current configured DNS servers on the ESXi Host [root@ESXi-01:~] esxcli network ip dns server list Remove all configured DNS server on the ESXi Host [root@ESXi-01:~] esxcli network ip dns server remove --all Remove specific configured DNS server on the ESXi Host [root@ESXi-01:~] esxcli network ip dns server remove --server <ip address> Add new DNS server [root@ESXi-01:~] esxcli network ip dns server add --server 10.0.0.70
The /etc/hosts file on the vCenter appliance we can still directly modify.
Links
Configure the DNS Hosts File by Using ESXCLI Commands
https://docs.vmware.com/en/VMware-vSphere/7.0/com.vmware.vsphere.networking.doc/GUID-543D13BB-C5B8-4DE5-85F7-0BEFBE4BE151.htmlChanges to ESXi 7.0 U3 hosts /etc/hosts or /etc/resolv.conf revert after reboot
https://knowledge.broadcom.com/external/article/318036/changes-to-esxi-70-u3-hosts-etchosts-or.htmlesxcli network Commands
https://vdc-repo.vmware.com/vmwb-repository/dcr-public/26334f54-ee84-47c2-b2f3-901f51cbc98a/d3f55719-4d3f-47c4-a3c5-fe9c7e5a67f6/doc/esxcli_network.htmlSet Up a DNS Server with ESXCLI
https://vdc-repo.vmware.com/vmwb-repository/dcr-public/0a40d9c5-4d4b-490d-8efa-e373a0ff2109/43a3c005-3878-4e05-8b60-35aca804d61d/doc/GUID-33939200-1D76-4C67-9607-77C586ADDFBA.html