Bog'liq Mastering Ubuntu Server Gain expertise in the art of deploying, configuring, managing, and troubleshooting Ubuntu Server by Jay LaCroix (z-lib.org)
[ 259 ] The
/etc/hosts
file, which we briefly discussed while working with our hostname,
tells our server how to resolve itself (it has a hostname mapping to the localhost IP
of
127.0.0.1
), but you are also able to create additional names to IP mappings here
as well. For example, if I had a server (
myserver.mydomain.org
) at IP
10.10.96.124
, I
could add the following line to
/etc/hosts
to make my machine resolve the server to
that IP each time, without it needing to consult a DNS server at all:
10.10.96.124 myserver.mydomain.org
In practice though, this is usually not a very convenient method by which to
configure name resolution. Don't get me wrong, you can certainly list your servers
in this file along with their IP addresses, and your server would be able to resolve
those names just fine. The problem stems from the fact that this method is difficult to
maintain. The name mappings apply only to the server you've made the
/etc/hosts
changes on; other servers wouldn't benefit since they would only check their own
/etc/hosts
file. You could add a list of servers to the hosts file on each of your
servers, but that would be a pain to manage. This is the main reason why having a
central DNS server is a benefit to any network, especially for resolving the names of
local resources.
However, the
/etc/hosts
file is used every now and again in the enterprise as a
quick one-off workaround, and you'll probably eventually end up needing to use
this method for one reason or another. One very common reason to use such a
manual method of resolving names is in the case where you're testing a replacement
server. In that case, you can configure the
/etc/hosts
file to have the same name as
the original server, but with the IP address of the new server. Once you finish testing
and confirm that the new server is operating properly, you can then replace the
network-wide DNS name to point to the new IP address.
On legacy Ubuntu servers, the
/etc/resolv.conf
file included the IP addresses for
DNS servers the system would use to resolve names. If you wanted to override the
DNS servers for your server, you would alter that file. Although this file still exists
in Ubuntu 20.04, it's not actually used anymore for this purpose. Name resolution
is now handled by
systemd-resolved
, which is a systemd unit that runs in the
background and applies DNS settings based on what the system receives via DHCP
or what you may have configured in Netplan. For the sake of completeness though,
here is a brief overview of the syntax of this file in older releases, in case you end up
working on such a server. An example of this file is as follows:
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by
resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 10.10.96.1
nameserver 10.10.96.2