如果在linux系统中ping某一台电脑的ip地址可以ping 通:
~ >: ping 192.168.1.3
PING 192.168.1.3 (192.168.1.3): 56 data bytes
64 bytes from 192.168.1.3: seq=0 ttl=64 time=2.511 ms
64 bytes from 192.168.1.3: seq=1 ttl=64 time=0.903 ms
64 bytes from 192.168.1.3: seq=2 ttl=64 time=0.916 ms
64 bytes from 192.168.1.3: seq=3 ttl=64 time=0.830 ms
64 bytes from 192.168.1.3: seq=4 ttl=64 time=0.912 ms
64 bytes from 192.168.1.3: seq=5 ttl=64 time=0.885 ms
--- 192.168.1.3 ping statistics ---
6 packets transmitted, 6 packets received, 0% packet loss
round-trip min/avg/max = 0.830/1.159/2.511 ms
~ >:
即,可以ping通内外。
如果ping外网出现如下现象
~>:ping 4.2.2.2
ping:bad address '4.2.2.2'
和
~ >: ping www.baidu.com
ping: bad address 'www.baidu.com'
出现时,很可能是有以下两个原因引起的
一、很可能是因为没有/etc/resolv
此时,需要手动修改配置文件:
/etc/resolv.conf
将域名解析服务器手动添加到配置文件中,该文件是DNS域名解析的配置文件,它的格式很简单,每行以一个关键字开头,后接配置参数。resolv.conf的关键字主要有四个,分别是:
nameserver #定义DNS服务器的IP地址
domain #定义本地域名
search #定义域名的搜索列表
sortlist #对返回的域名进行排序
/etc/resolv.conf的一个示例:
domain ringkee.com
search www.ringkee.com ringkee.com
nameserver 4.2.2.2
nameserver 8.8.8.8
最主要是nameserver关键字,如果没指定nameserver就找不到DNS服务器,其它关键字是可选的。
~ >: vim /etc/resolv.conf
nameserver 4.2.2.2(DNS地址可以在主机上通过ipconfig /all 查看)
nameserver 8.8.8.8
~
二、再有就是网关没有配置正确,
例如:
如果是eth0网,
~ >: route add default gw 192.168.1.1 dev eth0
如果是wlan0网,
~ >: route add default gw 192.168.1.1 dev wlan0
正确后的结果:
~ >: ping www.baidu.com
PING www.baidu.com (220.181.111.148): 56 data bytes
64 bytes from 220.181.111.148: seq=1 ttl=55 time=1310.653 ms
64 bytes from 220.181.111.148: seq=2 ttl=55 time=690.384 ms
64 bytes from 220.181.111.148: seq=3 ttl=55 time=454.612 ms
64 bytes from 220.181.111.148: seq=4 ttl=55 time=422.831 ms
64 bytes from 220.181.111.148: seq=5 ttl=55 time=1358.609 ms
64 bytes from 220.181.111.148: seq=6 ttl=55 time=490.365 ms
64 bytes from 220.181.111.148: seq=7 ttl=55 time=301.805 ms
64 bytes from 220.181.111.148: seq=8 ttl=55 time=809.492 ms
--- www.baidu.com ping statistics ---
10 packets transmitted, 8 packets received, 20% packet loss
round-trip min/avg/max = 301.805/729.843/1358.609 ms
~ >:
~ >: ping 4.2.2.2
PING 4.2.2.2 (4.2.2.2): 56 data bytes
64 bytes from 4.2.2.2: seq=0 ttl=53 time=1497.458 ms
64 bytes from 4.2.2.2: seq=1 ttl=53 time=948.759 ms
64 bytes from 4.2.2.2: seq=2 ttl=53 time=767.602 ms
64 bytes from 4.2.2.2: seq=3 ttl=53 time=848.248 ms
64 bytes from 4.2.2.2: seq=4 ttl=53 time=940.327 ms
--- 4.2.2.2 ping statistics ---
7 packets transmitted, 5 packets received, 28% packet loss
round-trip min/avg/max = 767.602/1000.478/1497.458 ms
~ >: