Linux主机名称(hostname)查询

Linux的域名服务器地址放在/etc/resolv.conf文件中。除了域名服务器,还可以使用本地/etc/hosts文件来定义名字与IP解析。通过/etc/nsswitch.conf来约定解析过程的一些选项.

在新版本的Ubuntu中,如 14.04, 16.0x等,/etc/resolv.conf是自动维护的。 如果要修改,则必须在源头处进行,比如其gateway,或在/etc/network/interfaces 中定义dns属性 (dns-nameserver, dns-nameservers, dns-search)

hostname and node name

What is the source of the hostname? It is from the value of uname -n. In redhat, it is from /etc/sysconfig/network.

$ cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=samplehost
GATEWAY=16.10.248.1

名字与地址对应关系查询

有多个简单的工具可以用来根据名字查询地址,比如nslookup, dig, host等。

nslookup

比如我们查一下baidu

$ nslookup baidu.com
Server:             10.143.22.118
Address:    10.143.22.118#53

Non-authoritative answer:
Name:       baidu.com
Address: 220.181.57.217
Name:       baidu.com
Address: 111.13.101.208
Name:       baidu.com
Address: 123.125.114.144
Name:       baidu.com
Address: 180.149.132.47

从这个结果来看,baidu.com的名字对应了四个地址。我们ping一下,看会返回哪个地址

# 从一个位于华北区的阿里云虚拟机来执行命令
$ ping baidu.com
PING baidu.com (180.149.132.47) 56(84) bytes of data.
64 bytes from 180.149.132.47: icmp_seq=1 ttl=50 time=52.8 ms
64 bytes from 180.149.132.47: icmp_seq=2 ttl=50 time=53.0 ms

# 从本地执行
$ ping baidu.com
PING baidu.com (220.181.57.217): 56 data bytes
64 bytes from 220.181.57.217: icmp_seq=0 ttl=55 time=29.754 ms
64 bytes from 220.181.57.217: icmp_seq=1 ttl=55 time=20.374 ms
64 bytes from 220.181.57.217: icmp_seq=2 ttl=55 time=23.065 ms

dig

看看dig baidu.com的输出, 比nslookup要更详细.

$ dig baidu.com

; <<>> DiG 9.9.5-3ubuntu0.2-Ubuntu <<>> baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34423
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 5, ADDITIONAL: 6

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;baidu.com.                 IN      A

;; ANSWER SECTION:
baidu.com.          422     IN      A       220.181.57.217
baidu.com.          422     IN      A       180.149.132.47
baidu.com.          422     IN      A       111.13.101.208
baidu.com.          422     IN      A       123.125.114.144

;; AUTHORITY SECTION:
baidu.com.          81437   IN      NS      ns2.baidu.com.
baidu.com.          81437   IN      NS      dns.baidu.com.
baidu.com.          81437   IN      NS      ns3.baidu.com.
baidu.com.          81437   IN      NS      ns4.baidu.com.
baidu.com.          81437   IN      NS      ns7.baidu.com.

;; ADDITIONAL SECTION:
dns.baidu.com.              68894   IN      A       202.108.22.220
ns2.baidu.com.              85356   IN      A       61.135.165.235
ns3.baidu.com.              6547    IN      A       220.181.37.10
ns4.baidu.com.              78716   IN      A       220.181.38.10
ns7.baidu.com.              76579   IN      A       119.75.219.82

;; Query time: 3 msec
;; SERVER: 10.143.22.118#53(10.143.22.118)
;; WHEN: Mon Aug 01 20:37:44 CST 2016
;; MSG SIZE  rcvd: 272

查询结果中的A类记录是IP地址,NS类记录是名字服务器的名称。NS记录是非常关键的,NS服务器存放了域名和IP地址的记录,本地DNS服务器会向NS服务器查询域名的IP地址,并缓存查询结果,缓存时间可通过TTL设置。为了利用DNS进行负载均衡和故障IP排除,在保证解析速度的前提下,TTL值应该尽量小。

host

看看host baidu.com的输出,

$ host baidu.com
baidu.com has address 111.13.101.208
baidu.com has address 220.181.57.217
baidu.com has address 180.149.132.47
baidu.com has address 123.125.114.144
baidu.com mail is handled by 10 mx.n.shifen.com.
baidu.com mail is handled by 20 mx50.baidu.com.
baidu.com mail is handled by 20 jpmx.baidu.com.
baidu.com mail is handled by 20 mx1.baidu.com.

host的默认输出更简洁。也可以输出更全面的信息,

$ host -a baidu.com
Trying "baidu.com"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 40283
;; flags: qr rd ra; QUERY: 1, ANSWER: 10, AUTHORITY: 5, ADDITIONAL: 5

;; QUESTION SECTION:
;baidu.com.                 IN      ANY

;; ANSWER SECTION:
baidu.com.          6302    IN      SOA     dns.baidu.com. sa.baidu.com. 2012132418 300 300 2592000 7200
baidu.com.          51      IN      A       123.125.114.144
baidu.com.          51      IN      A       111.13.101.208
baidu.com.          51      IN      A       180.149.132.47
baidu.com.          51      IN      A       220.181.57.217
baidu.com.          81066   IN      NS      ns4.baidu.com.
baidu.com.          81066   IN      NS      ns7.baidu.com.
baidu.com.          81066   IN      NS      dns.baidu.com.
baidu.com.          81066   IN      NS      ns2.baidu.com.
baidu.com.          81066   IN      NS      ns3.baidu.com.

;; AUTHORITY SECTION:
baidu.com.          81066   IN      NS      dns.baidu.com.
baidu.com.          81066   IN      NS      ns2.baidu.com.
baidu.com.          81066   IN      NS      ns7.baidu.com.
baidu.com.          81066   IN      NS      ns3.baidu.com.
baidu.com.          81066   IN      NS      ns4.baidu.com.

;; ADDITIONAL SECTION:
dns.baidu.com.              68523   IN      A       202.108.22.220
ns2.baidu.com.              84985   IN      A       61.135.165.235
ns3.baidu.com.              6176    IN      A       220.181.37.10
ns4.baidu.com.              78345   IN      A       220.181.38.10
ns7.baidu.com.              76208   IN      A       119.75.219.82

Received 370 bytes from 10.143.22.118#53 in 4 ms

更好的DNS原理介绍

最近找到了阮一峰写的一篇介绍DNS原理的文章,清晰易懂 http://www.ruanyifeng.com/blog/2016/06/dns.html