第七部分,也是本次更新的最後一部分,NAT(Network Address Translation),即地址解析協議。通俗理解,地址解析協議就是當一個單位只擁有一個公網ip地址,當內網中的主機想要訪問因特網時,可以通過在NAT路由器上將其本地地址轉換成全球IP地址的方式實現,從而實現內網中主機都可以訪問因特網。
網絡拓撲圖如下:
在還沒有配置nat的情況下,pc0和pc1是不能訪問pc2的,因爲R0右邊接口使用的是202.116.66.1,而下面端口使用192.168.1.1,從pc0發出的icmp可以到達pc2,因爲路由表中有到202.116.78.0/24的路由信息,但是從pc2回來的數據包找不到pc0,因爲三臺路由器上沒有到192.168.1.0/24的路由信息,所以需要把192.168.1.2轉化成和R0右邊接口的地址,就可以實現訪問pc2。右邊同理,只是變成了多個內網ip對應一個外網ip,隨機動態分配。
左邊部分使用靜態地址轉換協議,即一個內網ip只能對應一個公網ip,右邊部分使用動態轉換協議,即一段內網ip對應一個公網IP。通過配置,使pc0和pc1都能訪問pc2.
基礎配置
#r0
en conf t int f0/0 ip address 192.168.1.1 255.255.255.0 no shut int s0/0/0 ip address 202.116.66.1 255.255.255.0 no shut exit router ospf 1 network 192.168.1.0 0.0.0.255 area 1 network 202.116.66.0 0.0.0.255 area 1
#r1
en conf t int s0/0/0 ip add 202.116.66.2 255.255.255.0 no shut int s0/0/1 ip add 202.116.64.1 255.255.255.0 no shut int f0/0 ip add 202.116.78.1 255.255.255.0 no shut exit router ospf 1 network 202.116.66.0 0.0.0.255 area 1 network 202.116.64.0 0.0.0.255 area 1 network 202.116.78.0 0.0.0.255 area 1
#r2
en conf t int f0/0 ip address 10.0.1.1 255.255.255.0 no shut int s0/0/0 ip address 202.116.64.2 255.255.255.0 no shut exit router ospf 1 network 10.0.1.0 0.0.0.255 area 1 network 202.116.64.0 0.0.0.255 area 1
靜態NAT
#r0
exit ip nat inside source static 192.168.1.2 202.116.67.1 int f0/0 ip nat inside int s0/0/0 ip nat outside #r1 exit ip route 202.116.67.0 255.255.255.0 202.116.66.1
##動態NAT
#r2
exit ip nat pool abin 202.116.65.1 202.116.65.10 netmask 255.255.255.0 access-list 1 permit 10.0.1.2 0.0.0.255 ip nat inside source list 1 pool abin int f0/0 ip nat inside int s0/0/0 ip nat outside
#r1
ip route 202.116.65.0 255.255.255.0 202.116.64.2
結果檢測:
使用pc0 ping 通 pc2:
使用pc1 ping 通 pc2: