目錄
- NAT概述
- 什麼是NAT
- 爲什麼要有NAT
- 回顧:IP地址分類
- NAT分類
- NAT配置命令
- 靜態NAT
- 動態NAT
- 端口複用NAT( PAT )
<一> NAT概述
1> 什麼是NAT?
NAT:Network Address Translation 網絡地址轉換
將IP數據包頭中的IP地址轉換爲另一個IP的過程,配置在路由器上。
2> 爲什麼要有NAT?
在實際中,內網通過邊界路由器無法直接訪問外網,需要在邊界路由器上配置NAT規則,將內網訪問外網的數據包中源IP轉換爲公網IP;
這種方式不僅可以減少IP地址的使用,還可以保護局域網內部主機的安全。
<二> IP地址分類
A:1.0.0.0 ~ 126.255.255.255
netmask:255.0.0.0
B:128.0.0.0 ~ 191.255.255.255
netmask:255.255.0.0
C:192.0.0.0 ~ 223.255.255.255
netmask:255.255.255.0
D:224.0.0.0 ~ 239.255.255.255(組播)
E:240.0.0.0 ~ 255.255.255.255(實驗)
☞ 局域網IP:
10.0.0.0 ~ 10.255.255.255
172.16.0.0 ~ 172.31.255.255
192.168.0.0 ~ 192.168.255.255
<三> NAT分類
- 靜態轉換 Static Nat
- 動態轉換 Dynamic Nat
- 端口多路複用 Overload
<四> NAT配置命令
1> 靜態NAT配置
- 指定邊界路由器的inside接口和outside接口
Router>en
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#int f0/0
Router(config-if)#ip nat inside
Router(config-if)#exit
Router(config)#int s2/0
Router(config-if)#ip nat outside
Router(config-if)#exit
Router(config)#
- 定義靜態NAT規則(全局配置模式下)
Router>en
Router#
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#ip nat inside source static 192.168.1.2 12.1.1.102
Router(config)#ip nat inside source static 192.168.1.3 12.1.1.103
Router(config)#end
Router#
%SYS-5-CONFIG_I: Configured from console by console
Router#
Router#show ip nat t
Router#show ip nat translations
Pro Inside global Inside local Outside local Outside global
--- 12.1.1.102 192.168.1.2 --- ---
--- 12.1.1.103 192.168.1.3 --- ---
Router#
2> 動態NAT(NAT POOL)
靜態NAT將每一個內網IP與一個外網IP手動綁定,很麻煩且分配公網IP不是很方便;
於是出現了動態NAT,可以指定公網的一批網絡主機映射到一批公網地址(有空閒公網IP時自動分配);
注意:指定的一批內網地址,需要使用ACL(access control list)配置
- 指定邊界路由器的inside接口和outside接口
- 配置ACL
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#access-list 1 permit 192.168.1.0 0.0.0.255
Router(config)#
- 配置公網IP池
Router(config)#
Router(config)#ip nat pool nat1 12.1.1.100 12.1.1.200 netmask 255.255.255.0
Router(config)#
- 配置動態NAT規則(結合上述ACL和Nat Pool)
Router(config)#
Router(config)#ip nat inside source list 1 pool nat1
Router(config)#
3> 端口複用NAT(PAT:Port Address Translation)
- 指定邊界路由器的inside接口和outside接口
- 配置ACL
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#access-list 1 permit 192.168.1.0 0.0.0.255
Router(config)#
- 配置公網IP池(一個公網IP)
Router(config)#
Router(config)#ip nat pool nat1 12.1.1.100 12.1.1.100 netmask 255.255.255.0
Router(config)#
- 配置PAT(overload)
Router(config)#
Router(config)#ip nat inside source list 1 pool nat1 overload
Router(config)#