linux設定好IP後,可以訪問內網,不能訪問外網

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   sp   

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不通

[[email protected] ~]# ping www.baidu.comping: unknown host www.baidu.com

設定網關路由

route add default  gw  網關ip



感覺default這一行對應的gateway必須是localhost才管用呢。第一次設定完,是ip
[[email protected] ~]# netstat -rKernel IP routing tableDestination     Gateway         Genmask         Flags   MSS Window  irtt Iface172.16.0.0      *               255.255.255.0   U         0 0          0 eth0link-local      *               255.255.0.0     U         0 0          0 eth0default         172.16.0.1      0.0.0.0         UG        0 0          0 eth0[[email protected] ~]# ping www.baidu.comping: unknown host www.baidu.com

 

然後設定了 /etc/resolv.conf 檔案

設定nameserver

nameserver 202.106.0.20nameserver 202.106.196.115

重啟網卡

再次查看

[[email protected] ~]# netstat -rKernel IP routing tableDestination     Gateway         Genmask         Flags   MSS Window  irtt Iface172.16.0.0      *               255.255.255.0   U         0 0          0 eth0link-local      *               255.255.0.0     U         0 0          0 eth0default         localhost       0.0.0.0         UG        0 0          0 eth0

樂意串連上外網了

[[email protected] ~]# ping www.baidu.comPING 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 ms64 bytes from 61.135.169.125: icmp_seq=2 ttl=56 time=1.57 ms

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

linux設定好IP後,可以訪問內網,不能訪問外網

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.