linux設置好IP后,可以訪問內網,不能訪問外網


1,設置網卡,ip

vi /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0 #描述網卡對應的設備別名,例如ifcfg-eth0的文件中它為eth0   
BOOTPROTO=static #設置網卡獲得ip地址的方式,可能的選項為static,dhcp或bootp,分別對應靜態指定的 ip地址,通過dhcp協議獲得的ip地址,通過bootp協議獲得的ip地址   
BROADCAST=192.168.0.255 #對應的子網廣播地址   
HWADDR=00:07:E9:05:E8:B4 #對應的網卡物理地址   
IPADDR=192.168.0.2 #如果設置網卡獲得 ip地址的方式為靜態指定,此字段就指定了網卡對應的ip地址   
IPV6INIT=no   
IPV6_AUTOCONF=no   
NETMASK=255.255.255.0 #網卡對應的網絡掩碼   
NETWORK=192.168.0.0 #網卡對應的網絡地址   
ONBOOT=yes #系統啟動時是否設置此網絡接口,設置為yes時,系統啟動時激活此設備

重啟網卡
service network restart  

測試網絡:

ping www.baidu.com 

很多時候用百度測試網絡連通性,^_^

如果可以ping通,那當就ok了。如果ping不通

[root@uat ~]# ping www.baidu.com
ping: unknown host www.baidu.com

設置網關路由

route add default  gw  網關ip

如圖:

感覺default這一行對應的gateway必須是localhost才管用呢。第一次設置完,是ip
[root@uat ~]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
172.16.0.0      *               255.255.255.0   U         0 0          0 eth0
link-local      *               255.255.0.0     U         0 0          0 eth0
default         172.16.0.1      0.0.0.0         UG        0 0          0 eth0
[root@uat ~]# ping www.baidu.com
ping: unknown host www.baidu.com

 設置DNS。如果做此設置,無法解析域名

ping www.baidu.com unknow host....

然后設置了 /etc/resolv.conf 文件

設置nameserver

nameserver 202.106.0.20
nameserver 202.106.196.115

重啟網卡

再次查看

[root@uat ~]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
172.16.0.0      *               255.255.255.0   U         0 0          0 eth0
link-local      *               255.255.0.0     U         0 0          0 eth0
default         localhost       0.0.0.0         UG        0 0          0 eth0

樂意連接上外網了

[root@uat ~]# ping www.baidu.com
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125: icmp_seq=1 ttl=56 time=1.40 ms
64 bytes from 61.135.169.125: icmp_seq=2 ttl=56 time=1.57 ms

 原理還不是很清楚。先mark一下。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM