前提:/etc/hosts文件里面未添加任何主机名记录,/etc/resolv.conf文件也没有配置正确(或不配置)。
现象:在CentOS7上,只修改了本机主机名,ping一下结果通了,很奇怪。
解释:原来在/etc/nsswitch.conf文件中hosts多了一个项:
#hosts: db files nisplus nis dns
hosts: files dns myhostname
就是那个myhostname项,/etc/hosts和dns都解析不到IP时,myhostname就起作用了,它可以把hostname,解析成自己范围内的IP地址,因此就可以ping通了。
可以通过下面命令查看hostname对应的IP:
# getent ahosts `hostname`
fe80::f816:3eff:fe26:6d42 STREAM centos7-bind-1
fe80::f816:3eff:fe26:6d42 DGRAM
fe80::f816:3eff:fe26:6d42 RAW
192.168.122.84 STREAM
192.168.122.84 DGRAM
192.168.122.84 RAW
# ping `hostname`
PING centos7-bind-1 (192.168.122.84) 56(84) bytes of data.
64 bytes from centos7-bind-1 (192.168.122.84): icmp_seq=1 ttl=64 time=0.120 ms
64 bytes from centos7-bind-1 (192.168.122.84): icmp_seq=2 ttl=64 time=0.043 ms
nss-myhostname, libnss_myhostname.so.2英文描述:
nss-myhostname, libnss_myhostname.so.2 — Provide hostname resolution for the locally configured system hostname.
nss-myhostname is a plugin for the GNU Name Service Switch (NSS) functionality of the GNU C Library (glibc) primarily providing hostname resolution for the locally configured system hostname as returned by gethostname(2). The precise hostnames resolved by this module are:
The local, configured hostname is resolved to all locally configured IP addresses ordered by their scope, or — if none are configured — the IPv4 address 127.0.0.2 (which is on the local loopback) and the IPv6 address ::1 (which is the local host).
The hostname “localhost” is resolved to the IP addresses 127.0.0.1 and ::1.
The hostname “gateway” is resolved to all current default routing gateway addresses, ordered by their metric. This assigns a stable hostname to the current gateway, useful for referencing it independently of the current network configuration state.
Linux服务器禁ping与开启ping:http://www.linuxdiyf.com/linux/15375.html
ubuntu无法 ping www.baidu.com:http://www.linuxdiyf.com/linux/15301.html
windows平台下安装linux虚拟机,宿主机与虚拟机ping不通:http://www.linuxdiyf.com/linux/14038.html
主机+虚拟机Ubuntu+开发板互相ping通:http://www.linuxdiyf.com/linux/6768.html
Linux和Windows下ping:http://www.linuxdiyf.com/linux/15288.html