host 命令使用

host hostname [server]

[server]:使用不是由 /etc/resolv.conf 文件定义的 DNS 服务器来查询某台主机的 IP。

应用举例

第一种方法:使用 /etc/resolv.conf 中定义的DNS服务器查出百度主机的IP。

$ host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 180.101.49.14
www.a.shifen.com has address 180.101.49.13

第二种方法:是用谷歌的DNS(8.8.8.8)来查百度主机的IP。

$ host www.baidu.com 8.8.8.8
Using domain server:
Name: 8.8.8.8
Address: 8.8.8.8#53
Aliases: 

www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 180.101.49.13
www.a.shifen.com has address 180.101.49.14
www.a.shifen.com is an alias for www.wshifen.com.

/etc/resolv.conf

$ cat /etc/resolv.conf 
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0 trust-ad

执行 host、ping 命令,会默认从这个文件中获取 DNS Server 的地址,如果这个文件被删了,那么 host、ping 命令就会执行失败。

$ sudo mv /etc/resolv.conf /etc/resolv.conf.bak

host 命令卡住

$ host www.baidu.com



ping 命令执行失败

$ ping www.baidu.com
ping: www.baidu.com: 域名解析暂时失败

把文件还原回来,

$ sudo mv /etc/resolv.conf.bak /etc/resolv.conf

host、ping 命令就正常了

$ host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 180.101.49.13
www.a.shifen.com has address 180.101.49.14
$ ping www.baidu.com
PING www.a.shifen.com (180.101.49.13) 56(84) bytes of data.
64 字节,来自 180.101.49.13 (180.101.49.13): icmp_seq=1 ttl=53 时间=9.77 毫秒
64 字节,来自 180.101.49.13 (180.101.49.13): icmp_seq=2 ttl=53 时间=9.88 毫秒
^C
--- www.a.shifen.com ping 统计 ---
已发送 2 个包, 已接收 2 个包, 0% 包丢失, 耗时 1003 毫秒
rtt min/avg/max/mdev = 9.767/9.821/9.875/0.054 ms

参考

Linux中的host命令应用实例详解