How does /etc/hosts match work?
It's depend on
/etc/host.conf to decide whether to read information in
As @msw said processed by libc, a user-space library, it's read nsswitch.conf and read Tag "hosts: files dns" , then file means read
/etc/hosts and dns means
Now suppose you hit the http://www.google.com in firefox,
- then firefox first going to resolve google.com with help of local resolver ( libresolve.so ), then
- it's first check the priority of file and dns ,
- in default case it will search in file means "/etc/hosts",
- in case if google.com is not match then it will refer "/etc/resolv.conf", in this file it's check the
nameserver tag if it's not configure then resolver send dns Query to localhost on port 53,
- if nameserver define suppose
nameserver 220.127.116.11, then it will send query to eg.
dig google.com @18.104.22.168. of course now we can get ans of the query from pubic dns.
Also users can maintain it's own hosts file using "HOSTALIASES" variable, so it will first check this file, before reading /etc/hosts.
echo "fb www.fb.com" >> ~/my_hosts
echo "export HOSTALIASES=~/my_hosts" >> ~/.bashrc