能ping通ip但无法ping通域名和localhost //ping: bad address 'www.baidu.com'

错误描述:

~ # ping localhost
ping: bad address 'localhost'

原因,在/etc目录下缺少hosts文件,将linux中的/etc hosts文件拷入即可

~ # ping localhost
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.506 ms
64 bytes from 127.0.0.1: seq=1 ttl=64 time=0.324 ms
64 bytes from 127.0.0.1: seq=2 ttl=64 time=0.293 ms
[12]+  Stopped                   ping localhost

无法ping通域名,但是可以ping通ip

~ # ping 202.168.22.5
PING 202.168.22.5 (202.168.22.5): 56 data bytes
64 bytes from 202.168.22.5: seq=0 ttl=50 time=332.618 ms
64 bytes from 202.168.22.5: seq=1 ttl=50 time=326.861 ms
[11]+  Stopped                   ping 202.168.22.5
~ # route add -net 127.0.0.1 netmask 255.255.255.0 lo
route: netmask and route address conflict
~ # ping www.baidu.com
ping: bad address 'www.baidu.com'

原因,缺少相关配置文件,分别为 :/etc/resolv.conf 、/etc/nsswitch.conf、/etc/hosts
将这几个文件考入开发板发现,依旧存在问题,此时我们修改配置文件中的dns配置

/etc # vi resolv.conf 
#!/bin/bash

domain=`(. /boot.conf; echo "$DNSDOMAINNAME") 2>/dev/null`

echo "# This file is automagically regenerated with each boot"
echo
test "$domain" && echo "domain $domain"
test "$domain" && echo "search $domain"
echo
echo "# Note that nslookup can choke on DNS server which itself"
echo "# does NOT have domain name. Other things can work fine."
echo
# # If we run DNS cache:
# echo "nameserver xxx.xxx.xxx.xxx"//此处写服务器网关名称
# exit

prio=0
i=0; while test "${if[$i]}"; do
        test x"${dns_prio[$i]}" != x"" \
        && test "${dns_prio[$i]}" -gt "$prio" \
        && prio="${dns_prio[$i]}"
let i++; done

i=0; while test "${if[$i]}"; do
        for d in ${dns[$i]}; do
                p="${dns_prio[$i]}"
                test x"$p" == x"" && p=0
                test x"$p" == x"$prio" || continue
                echo "nameserver $d"
        done
let i++; done
nameserver xxx.xxx.xxx.xxx//此处与上文的服务器网关IP相同 

之后我们再试下

/etc # ping www.baidu.com
PING www.baidu.com (14.215.177.38): 56 data bytes
64 bytes from 14.215.177.38: seq=0 ttl=55 time=3.708 ms
64 bytes from 14.215.177.38: seq=1 ttl=55 time=3.303 ms
[18]+  Stopped                   ping www.baidu.com
/etc # ping localhost
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.517 ms
64 bytes from 127.0.0.1: seq=1 ttl=64 time=0.381 ms
[19]+  Stopped                   ping localhost

如果你本地局域网可以ping通但是外网ping不通IP,那么你可以看看网关有没有设置好

~ # route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.9.0     *               255.255.255.0   U     0      0        0 eth0
//此处网关没有设置,为空
~ # route add default gw 192.168.9.1 //设置默认网关
~ # route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.9.1     0.0.0.0         UG    0      0        0 eth0
192.168.9.0     *               255.255.255.0   U     0      0        0 eth0

你可能感兴趣的:(能ping通ip但无法ping通域名和localhost //ping: bad address 'www.baidu.com')