1. 程式人生 > >Linux CentOS 6 NAT 轉發 完整教程

Linux CentOS 6 NAT 轉發 完整教程

linux centos 6 nat 轉發 完整教程


網絡拓補圖:

技術分享

環境介紹:

一臺CentOS6 主機,雙網卡,兩網段

網口設定如下:

[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=dhcp
[[email protected]
/* */ ~]# [[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 DEVICE=eth1 TYPE=Ethernet ONBOOT=yes BOOTPROTO=static IPADDR=192.168.66.1 NETMASK=255.255.255.0 [[email protected] ~]# service network restart [[email protected] ~]# ifconfig -a eth0 Link encap:Ethernet HWaddr 00:0C:29:37:00:93 inet addr:172.16.20.183 Bcast:172.16.255.255 Mask:255.255.0.0 eth1 Link encap:Ethernet HWaddr 00:0C:29:37:00:9D inet addr:192.168.66.1 Bcast:192.168.66.255 Mask:255.255.255.0


打開網卡轉發

[[email protected] ~]# vim /etc/sysctl.conf  
net.ipv4.ip_forward = 1


立即生效

[[email protected] ~]# sysctl -p


轉發規則:

[[email protected] ~]# iptables -t nat -A POSTROUTING -s 192.168.66.0/24 -j SNAT --to 172.16.20.183 #出口為靜態IP選這個
[[email protected] ~]# iptables -t nat -A POSTROUTING -s 192.168.66.0/24 -o eth0 -j MASQUERADE #出口為動態IP選這個


查看/保存轉發規則

[[email protected] ~]# iptables -t nat -nL
[[email protected] ~]# service iptables save


===================驗證NAT===============



在192.168.66/24網段內,一臺linux主機設置為:

[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static

IPADDR=192.168.66.2
NETMASK=255.255.255.0
GATEWAY=192.168.66.1
DNS1=180.76.76.76.76
DNS2=8.8.8.8

[[email protected] ~]#


重啟網絡服務
[[email protected] ~]# service network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
														   	



開始測試

												   
ping 自己測試
[[email protected] ~]# ping 192.168.66.2
PING 192.168.66.2 (192.168.66.2) 56(84) bytes of data.
64 bytes from 192.168.66.2: icmp_seq=1 ttl=64 time=0.042 ms

ping網關測試
[[email protected] ~]# ping 192.168.66.1
PING 192.168.66.1 (192.168.66.1) 56(84) bytes of data.
64 bytes from 192.168.66.1: icmp_seq=1 ttl=64 time=0.261 ms

ping nat出口測試
[[email protected] ~]# ping 172.16.20.183
PING 172.16.20.183 (172.16.20.183) 56(84) bytes of data.
64 bytes from 172.16.20.183: icmp_seq=1 ttl=64 time=0.280 ms

ping nat外的工作機
[[email protected] ~]# ping 172.16.20.245
PING 172.16.20.245 (172.16.20.245) 56(84) bytes of data.
64 bytes from 172.16.20.245: icmp_seq=1 ttl=63 time=2.39 ms

ping 百度DNS
[[email protected] ~]# ping 180.76.76.76
PING 180.76.76.76 (180.76.76.76) 56(84) bytes of data.
64 bytes from 180.76.76.76: icmp_seq=1 ttl=52 time=10.2


ping 網易163
[[email protected] ~]# ping www.163.com
PING 1stcncloud163.xdwscache.ourwebpic.com (114.80.143.193) 56(84) bytes of data.
64 bytes from 114.80.143.193: icmp_seq=1 ttl=55 time=9.58 ms

ping 淘寶網
[[email protected] ~]# ping www.taobao.com
PING www.taobao.com.danuoyi.tbcache.com (114.80.174.46) 56(84) bytes of data.
64 bytes from 114.80.174.46: icmp_seq=1 ttl=48 time=8.83 ms


完。。

本文出自 “李春利” 博客,請務必保留此出處http://990487026.blog.51cto.com/10133282/1939701

Linux CentOS 6 NAT 轉發 完整教程