The file is overwritten by systemd-resolved.service
, among other files:
systemd-resolved synthesizes DNS resource records (RRs) for the
following cases:
The local, configured hostname is resolved to all locally configured IP addresses ordered by their scope, or — if none are configured — the
IPv4 address 127.0.0.2 (which is on the local loopback) and the IPv6
address ::1 (which is the local host).
The hostnames "localhost" and "localhost.localdomain" (as well as any hostname ending in ".localhost" or ".localhost.localdomain") are
resolved to the IP addresses 127.0.0.1 and ::1.
The hostname "_gateway" is resolved to all current default routing gateway addresses, ordered by their metric. This assigns a stable
hostname to the current gateway, useful for referencing it
independently of the current network configuration state.
The mappings defined in /etc/hosts are resolved to their configured addresses and back, but they will not affect lookups for non-address
types (like MX).
According to documentation for /etc/systemd/resolved.conf
and the related post, you can edit /etc/systemd/resolved.conf
to have specific domain resolved by your local DNS sever (on Ubuntu you have dnsmasq, example), or add ReadEtcHosts=
to let the service actually use the file.
You can also disable the service. See How to disable systemd-resolved in Ubuntu?
According to a Fedora forum thread the issue could also relate to Network Manager service. For Ubuntu servers, cloud-init could also be the cause of the issue according to the bug report on launchpad
/etc/hosts
file as sudo?/etc/hosts
is not a link file nor is it controlled by any daemons that I am aware of, so there shouldn't be any reason why it is changing between reboots. What is the output ofls -al /etc/hosts
?ls -al /etc/hosts
outputs. It should be a standalone file with a 644 permissions owned by root:root.nano
might be creating a temp file that is not getting erased before reboot, then when a reboot happens, it takes the tmp file and overrides the one that was just changed? I don't know for sure, but can you try adding a line to the bottom of your hosts file by tryingsudo bash -c 'echo "127.0.1.1 $(hostname)" >> /etc/hosts'
from a terminal window then reboot and see if that sticks?