Blog·Tanky WooABOUTRSS

host localhost?

01 Jun 2016

之前有时看下localhost,要么ping, 要么host(或者dig)。

昨天在回答一个Issue后,我本地测试了下,发现host localhost没有结果,但是在线上机器是可以返回:

$ host localhost
localhost has address
localhost has IPv6 address ::1



The host program uses libresolv to perform a DNS query directly, i.e., does not use gethostbyname.

Most programs, when attempting to connect to another host, invoke the gethostbyname system call or a similar function. This function obeys the configuration of /etc/nsswitch.conf. This file has a line which in Ubuntu 12.04 defaults to the following:

hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns4

which means that it will first use /etc/hosts, then fall back to DNS queries.

If you want to perform a host lookup this way, you can do this with getent hosts. For example:

$ getent hosts

I hope this helps.




$ cat named.conf
zone "localhost" in{
  type master;
  file "master.localhost";

$ cat master.localhost
$TTL    86400 ; 24 hours could have been written as 24h or 1D
$ORIGIN localhost.
; line below expands to: localhost 1D IN SOA localhost root.localhost
@  1D  IN    SOA @  root (
                  2002022401 ; serial
                  3H ; refresh
                  15 ; retry
                  1w ; expire
                  3h ; minimum
@  1D  IN  NS @ 
   1D  IN  A

This zone allows resolution of the name localhost to the loopback address when using the DNS server. Any query for 'localhost' from any host using the name server will return

参考named.conf - localhost

所以线上的这个结果,是根据配置的nameserver, 进而查询获取的结果。