@goldilocks answer is completely correct AFAIK; this answer is posted to hopefully augment that answer with some additional information:
The best explanation and guidance I've seen for /etc/hosts
, and the address 127.0.1.1
is in the Debian Reference, Chapter 5 - Network Reference,Para 5.1.1 hostname resolution. It contains some specific guidance on the /etc/hosts
file including the following:
-
The host_name matches the hostname defined in the "/etc/hostname
".
This may be misleading. As I read this, there's an implication that the entry in /etc/hostname
updates the entry in /etc/hosts
. However there is no such automatic update feature, nor does it happen even after reboot - at least on my RPis.
Rather, read this as an instruction: you (the administrator) must edit the 127.0.1.1 entry in /etc/hosts
to "match" the /etc/hostname
entry.
The take-away should be that these two hostname entries should match - under most normal circumstances. You can easily verify that there is a match:
$ hostnamectl | grep hostname && cat /etc/hosts | grep '127.0.1.1'
Static hostname: raspberrypi4b
Transient hostname: raspberrypi4bxyz
127.0.1.1 raspberrypi4b
# NOTE the appearance of a 'Transient hostname' implies
# the old name is being cached; reboot will flush it out
-
For a system with a permanent IP address, that permanent IP address should be used here instead of 127.0.1.1
.
Users (mis-informed ones mostly) of the static <value>
option in /etc/dhcpcd.conf
should take note.
The package resolvconf
(typically installed in /sbin
) populates the file /etc/resolv.conf
. This file will contain a domain name if you've defined one in your router/gateway, and the address of your internal nameserver
- typically your router/gateway.
The file /etc/nsswitch.conf
should contain a line like the following to enable zero conf networking, and resolution of host names ending in .local
:
hosts: files mdns4_minimal [NOTFOUND=return] dns
Vintage Windows users may rejoice in the explanation provided for integrating systems that still use NetBIOS!
And so informed, it should be clear that deleting the line with 127.0.1.1
in your /etc/hosts <hostname from /etc/hostname>
file will cripple mDNS which is (by default) how at least your RPi systems find each other, and make themselves find-able from other mDNS/zero-conf systems.
This "bookmark-worthy" document should be retained for future reference, especially since the RPi Organization has announced their intention to deprecate some of the "common" Linux documentation currently contained in their repo.
Wrt the other anomalies noted in the question:
sudo: unable to resolve host <xxxxx>: Name or service not known
I can't add a thing to @goldilocks answer, except to wonder why the sudo
command executes even if the information in /etc/hosts
is missing or incorrect?
df -h
==> 100% usage of /dev/root
I was unable to replicate this error on either my buster or bullseye RPi. I did find it curious that df -h
lists /dev/root
- which AFAICS isn't defined on RPi. This reference shed little light - except that this related blog post was interesting. The fact that RPi's /proc/cmdline
contains root=PARTUUID=6c586e13-02
may suggest a system-level disconnect between /dev/root
and /
under some circumstances. A guess is that this is unique to RPi.
/etc/hosts
at all (but you could put it in/etc/hostname
). – goldilocks Jan 12 '23 at 17:22