/etc/hosts and the DNS don't work together. They provide independent resolutions of names (network names).
The glue that links them is inside
/etc/nsswitch.conf for linux systems. In
/etc/netsvc.conf for AIX servers, in the system for Windows and could be listed with
lookupd -configuration (search for LookupOrder, similar to:
Cache FF DNS NI DS) in MacOS systems.
The actual order becomes complex and usually convoluted as each name resolution service could (and many times do) look inside other levels of resolution. Like
dnsmasq (a light DNS server generally at
::1:53 (or both)) usually reads and includes the
/etc/hosts file contents. Or like
systemd.resolver (a basic resolver that should only resolve un-dotted names like
mycomputer) calls directly DNS resolutions for dotted names (
mycomputer.here.dev.) under some conditions.
In general, services are called in order and the first one that doesn't fail wins and is accepted as the correct address. The general basic order is:
/etc/hosts (file), mDNS (un-dotted names), DNS, NIS, NIS+, LDAP. In some linux systems there is a last resort resolution for the computer
hostname in the service
For example, in this system (from
hosts: files mdns4_minimal [NOTFOUND=return] dns myhostname
Note that the very old (glibc 2.4 and earlier)
order entry set in
Only apply to the files (file
/etc/hosts) name service.
The effects on this (linux) client computer related to NIS and LDAP are (usually) controlled by the DNS server used (bind, unbound, etc.).
- If a hostname can be resolved in /etc/hosts, does DNS apply after /etc/hosts to resolve the hostname or treat the resolved IP address by /etc/hosts as a "hostname" to resolve recursively?
If a hostname can be resolved in
DNS doesn't apply (if files is before DNS).
nor is the resolved IP address treated as a "hostname".
It simply is: the resolved address.
A browser could use any method to resolve a name (after it has checked its cache of resolved names). Only if it uses a system provided method the order given above apply. The browser, as any program, could choose to contact any DNS server directly.
If the system order has
DNS, it means that an entry in that file will take precedence to
DNS resolution service.
- ... Does it mean that /etc/hosts overrides DNS for resolving hostnames?
Yes (if the browser use the system provided resolution).
/etc/hosts apply again, so that I can't connect to the website?
Only until the browser internal cache is cleared (or it times out) for that specific name is that name searched outside of the browser again.
If the browser has a name resolved in its cache, the browser uses it again.
Use this to clear the cache.
Or simply close (wait a while) and re-start the browser.