NAT - Network Address Translation 網絡地址轉換
顧名思義,是一種在數據包通過路由器或者防火牆時替換/重寫IP源地址或者目的地址的技術。
發明該技術的本意是:使用私有地址的設備或者主機通過這種方式可以用有限的公網地址在網絡上進行通信,從而減緩了IPv4地址緊缺的趨勢。
但是大家逐漸發現NAT可有更多用途:服務器負載均衡,網絡安全,網絡移植和融合等。
什麼時候使用NAT
1. 需要接入internet但是主機沒有足夠的公網地址
2. 更換ISP需要重新分配網絡地址時
3. 網絡融合但是有IP地址衝突時
4. 內網地址由於安全原因需要隱藏時
優點
1. 保存了有限的公有IP地址
2. 在網絡運營中減少了IP地址重新劃分
3. 增加了鏈接網絡靈活性和安全性
缺點
1. 地址轉換增加了數據交換的延時
2. 失去了端到端的IP可追蹤性
3. 有些程序不能與NAT兼容
種類
1. 靜態NAT - 一對一
2. 動態NAT - 多對多
3. 過載NAT - 多對一或多對少,PAT - port address translation
重要概念
角度 角度
位置 | Inside Local | Inside Global |
位置 | Outside Local | Outside Global |
Inside Local: 分配給內網主機的地址
Inside Global: 從外網看,內網主機的地址 (從裏往外發包時,IP地址轉換後內網主機的地址)
Outside Global:分配給外網主機的地址
Outside local: 從內網看,外網主機的地址 (從外往裏發包時,IP地址轉換後的外網主機地址)
基本配置
1. 選擇NAT種類,寫出需要轉換的IP地址, 最好用inside/outside標註好
2. 在config t 後啓動nat,用相應的keyword對應相應的種類, static-靜態; pool - 動態; overload - 過載
3. 在對應端口聲明對如NAT是inside還是outside
4. 對於靜態NAT前三步就夠了,Dynamic & PAT 需要再加一步添加access-list
5. 用sh ip nat tran ; sh ip nat statistics; debug ip nat 等驗證
配置實例 (注:例子來源於 Sybex CCNA第七版, Chap 14 NAT, Hands on Lab - 以下爲GNS3中的模擬)
Part 1 - Dynamic NAT
------------------------------------------------------
Lab_A - 相關NAT的配置
interface Serial0/0
ip address 192.168.20.1 255.255.255.0
ip nat inside
encapsulation ppp
interface Serial0/1
ip address 171.16.10.2 255.255.255.0
ip nat outside
encapsulation ppp
ip nat pool GlobalNet 171.16.10.50 171.16.10.55 netmask 255.255.255.0 》》》》 config NAT pool
ip nat inside source list 1 pool GlobalNet
ip classless
ip route 171.16.0.0 255.255.0.0 171.16.10.1
no ip http server
!
access-list 1 permit 192.168.20.0 0.0.0.255
--------------------------------------------------------------
Lab B & Lab C telnet 171.16.10.1 and output from Lab A, 開啓 debug ip nat
==========Before Telnet===========
Lab_A#sh ip nat trans
Lab_A#
==========After Telnet============
Lab_A#
01:39:40: NAT: s=192.168.20.2->171.16.10.50, d=171.16.10.1 [0] >>>>>>> telnet from Lab B
01:39:40: NAT: s=171.16.10.1, d=171.16.10.50->192.168.20.2 [0]
### output omitted#####
Lab_A#
01:42:44: NAT: s=192.168.30.2->171.16.10.51, d=171.16.10.1 [0] >>>>>>> telnet from Lab C
01:42:44: NAT: s=171.16.10.1, d=171.16.10.51->192.168.30.2 [0]
### output omitted#####
Lab_A#sh ip nat tran
Pro Inside global Inside local Outside local Outside global
--- 171.16.10.50 192.168.20.2 --- ---
--- 171.16.10.51 192.168.30.2 --- ---
Lab_A#sh ip nat statistics
Total active translations: 2 (0 static, 2 dynamic; 0 extended)
Outside interfaces:
Serial0/1
Inside interfaces:
Serial0/0
Hits: 47 Misses: 2
Expired translations: 0
Dynamic mappings:
-- Inside Source
access-list 1 pool GlobalNet refcount 2
pool GlobalNet: netmask 255.255.255.0
start 171.16.10.50 end 171.16.10.55
type generic, total addresses 6, allocated 2 (33%), misses 0 >>>>>>> used 1/3 of the 6 IP addresses
Part 2 - PAT
------------------------------------------------------
先移除 Dynamic NAT相關配置
Lab_A#clear ip nat tran * >>>>>>>>> only dynamic NAT entries will be removed
Lab_A#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Lab_A(config)#no ip nat inside source list 1 pool GlobalNet
Lab_A(config)#$ool GlobalNet 171.16.10.50 171.16.10.55 netmask 255.255.255.0
Lab_A(config)#
Lab_A - 相關PAT的配置
interface Serial0/0
ip address 192.168.20.1 255.255.255.0
ip nat inside
encapsulation ppp
interface Serial0/1
ip address 171.16.10.2 255.255.255.0
ip nat outside
encapsulation ppp
ip nat pool Adam-PAT 171.16.10.100 171.16.10.100 netmask 255.255.255.0
ip nat inside source list 2 pool Adam-PAT overload >>>>>>>>>>>>>>>>>>>>>> overload keyword
ip classless
ip route 171.16.0.0 255.255.0.0 171.16.10.1
no ip http server
!
access-list 1 permit 192.168.20.0 0.0.0.255
access-list 1 permit 192.168.30.0 0.0.0.255
access-list 2 permit 192.168.20.0 0.0.0.255
access-list 2 permit 192.168.30.0 0.0.0.255
----------------------------------------------------------------
Lab B & Lab C telnet 171.16.10.1 and output from Lab A, 開啓 debug ip nat
==========Before Telnet===========
Lab_A#sh ip nat trans
Lab_A#
==========After Telnet============
Lab_A#
02:07:17: NAT: s=192.168.20.2->171.16.10.100, d=171.16.10.1 [0] >>>>>>>>>> Telnet from Lab B
02:07:17: NAT: s=171.16.10.1, d=171.16.10.100->192.168.20.2 [0]
02:08:14: NAT: s=192.168.20.2->171.16.10.100, d=171.16.10.1 [17] >>>>>>>>> Telnet from Lab C
02:08:14: NAT: s=171.16.10.1, d=171.16.10.100->192.168.20.2 [8]
Lab_A#sh ip nat trans
Pro Inside global Inside local Outside local Outside global
tcp 171.16.10.100:11002 192.168.20.2:11002 171.16.10.1:23 171.16.10.1:23
tcp 171.16.10.100:1024 192.168.30.2:11002 171.16.10.1:23 171.16.10.1:23
-------------------------------------------------
篇後語
通過寫這個文章加上在GNS3實現這個lab發現我這個動手能力簡直爛到不行,文藝點叫眼高手低。雖然經常看相關的文章,平時工作trouble-shooting也會用到相關的命令和知識。今天在GNS3上試着一模擬問題全出來了,一會串口起不來(需要改下封裝協議 - encapsulation ppp), 一會兒rip 水平分割忘了。整整在這捯飭了一下午 (中間偷懶去看了一集 Game of Thrones)。 所以,結論是,需要 在以後總結知識的時候儘量用GNS3或者IOU模擬下實驗,以免出現紙上談兵的情況。