能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

 

 

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值