NAT(Network Address Translation,網絡地址轉換)是將IP 數據包頭中的IP 地址轉換為另一個IP 地址的過程。
(1)NAT簡介:
在實際應用中,NAT 主要用於實現私有網絡訪問公共網絡的功能。這種通過使用少量的公有IP 地址代表較多的私有IP 地址的方式,不僅完美地解決了公網lP地址不足的問題,而且還能夠有效地避免來自網絡外部的攻擊,隱藏並保護網絡內部的計算機。
在學習配置NAT之前,需要明確兩種概念:
內部本地地址:需要訪問Internet網絡的內網IP地址(私有IP地址),是計算機在內網中的身份標識。內網地址(池)往往使用ACL(Access List)技術指定。
內部全局地址:NAT轉換中對應的公網IP地址,是內網中計算機在Internet環境中的外部身份標識。
由於內網中的計算機IP地址是私有IP地址,不能直接用於Internet,故NAT服務相當於給予內網計算機能用於Internet的公網IP地址身份,實現內網計算機與Internet的通信。
NAT類型:
靜態NAT(Static Nat)
內部本地地址和內部全局地址是一對一的關系,公網可通過全局地址直接訪問對應的內網主機,這種方式主要用於配置內網服務器。
動態NAT(Dynamic Nat)
內部本地地址池對應一個內部全局地址池,本地地址和全局地址之間是多對多的關系(微觀上看是一對一的關系)。由於內網IP和公網IP之間的對應不固定,故外網不能訪問內網主機。
端口多路復用(Port address Translation,PAT)
也稱為NPAT(Network Port address Translation),所有內部本地地址共享同一個內部全局地址,即本地地址和全局地址之間是多對一的關系,內網主機依靠端口來區分,由於端口是隨機的,故外網不能直接訪問內網,這對內網起到一定保護作用,但可訪問內網中綁定固定端口的某台主機或服務器。
(2)靜態NAT的配置:
靜態NAT將一個特定的內部本地地址(Inside Local Address)靜態地映射到一個內部全局地址(Inside Global Address),這意味着靜態NAT中每一個被映射的內部本地地址,一定對應着一個固定而且唯一的內部全局地址。這種方式不但可以讓內網設備訪問外網,而且還可以讓外網直接訪問內網設備。
常用的靜態NAT配置命令:
示例一:配置靜態NAT
步驟一:配置各接口ip地址:
RA:
en conf t host RA int f0/0 ip add 192.168.0.254 255.255.255.0 no shut int s0/1/1 ip add 200.200.10.1 255.255.255.0 no shut bandwidth 96 end
RB:
en conf t host RB int s0/1/1 clock rate 9600 bandwidth 96 ip add 200.200.10.2 255.255.255.0 no shut int s0/1/0 ip add 200.200.20.2 255.255.255.0 bandwidth 9600 no shut end
RC:
en conf t host RC int f0/0 ip add 100.100.10.254 255.255.255.0 no shut int s0/1/0 ip add 200.200.20.1 255.255.255.0 clock rate 9600 bandwidth 96 no shut
步驟二:配置靜態路由:
RA:
ip route 200.200.10.0 255.255.255.0 200.200.20.2
ip route 100.100.10.0 255.255.255.0 200.200.10.2
RB:
ip route 100.100.10.0 255.255.255.0 200.200.20.1
ip route 192.168.0.0 255.255.255.0 200.200.10.1 RC:
ip route 200.200.20.0 255.255.255.0 200.200.10.2
ip route 192.168.0.0 255.255.255.0 200.200.10.2
步驟三:配置NAT:
RA:
en conf t ip nat inside source static 192.168.0.1 200.200.10.3 int f0/0 ip nat inside int s0/1/0 ip nat outside end
RC:
en conf t ip nat inside source static 100.100.10.1 200.200.20.3 int f0/0 ip nat inside int s0/1/0 ip nat outside end
測試:
(3)動態NAT的配置:
動態NAT又稱為動態地址翻譯(Dynamic Address Translation),它是將內部本地地址與內部全局地址作一對一的替換。
內部全局地址以地址池的形式供選擇,內部本地地址只要符合訪問外網的條件,在做地址轉換時,就從地址池中動態地選取最小的還沒分配的內部全局地址來替換,其轉換步驟如下:
(1)根據可用的內部全局地址范圍,建立地址池。 (2)利用ACL,定義允許訪問外網的內部地址范圍。 (3)定義內部地址與地址池之間的轉換關系。 (4)定義內網端口和外網端口。
常用的動態NAT配置命令:
示例二:配置動態NAT:
步驟一:配置ip地址和路由:
RA:
en conf t host RA int f0/0 ip add 192.168.1.254 255.255.255.0 no shut int s0/1/0 clock rate 9600 bandwidth 96 ip add 220.220.10.1 255.255.255.0 no shut exit ip route 172.16.1.0 255.255.255.0 220.220.10.11 end
RB:
en conf t host RB int f0/0 ip add 172.16.1.254 255.255.255.0 no shut int s0/1/0 bandwidth 96 ip add 220.220.10.11 255.255.255.0 no shut exit ip route 192.168.1.0 255.255.255.0 220.220.10.11 end
步驟二:配置動態NAT:
RA:
en conf t ip nat pool p1 220.220.10.2 220.220.10.10 netmask 225.255.255.0 access-list 120 permit ip 192.168.1.0 0.0.0.255 any int f0/0 ip nat inside int s0/1/0 ip nat outside end
RB:
en conf t ip nat pool p1 220.220.10.12 220.220.10.20 netmask 225.255.255.0 access-list 120 permit ip 172.16.1.0 0.0.0.255 any int f0/0 ip nat inside int s0/1/0 ip nat outside end
配置結果:
IP地址轉換結果:
問題:如果路由器兩邊同時配置NAT網絡地址轉換會出現在串行鏈路一直循環尋路,找不到目標IP地址。
(4)NPAT的配置:
NPAT可以用於地址偽裝(Address Masquerading),它可以讓多個內網設備使用一個IP地址同時訪問外網。
NPAT的轉換步驟如下:
(1)利用ACL,定義允許訪問外網的內部IP地址范圍。 (2)為外網IP地址定義一個地址池名。 (3)將地址池賦予在第(1)步中定義的內部IP地址范圍,並標明為overload(超載,即PAT)方式。 (4)定義內網端口和外網端口。
常用的NPAT配置命令:
示例三:配置NPAT超載:
步驟一:配置接口ip和路由(省略)
步驟二:配置NPAT
RA:(用地址池)
en conf t access-list 120 permit ip 192.168.1.0 0.0.0.255 any ip nat pool p1 200.200.10.2 200.200.10.2 netmask 255.255.255.0 ip nat inside source list 120 pool p1 overload int f0/1 ip nat inside int f0/0 ip nat outside end
RA:(用接口)
en conf t access-list 120 permit ip 192.168.1.0 0.0.0.255 any ip nat inside source list 120 interface f0/0 overload int f0/1 ip nat inside int f0/0 ip nat outside end
配置結果:
NAT轉換的詳細信息:
(5)驗證NAT和NPAT的配置:
配置完NAT和NPAT后,可以使用表 8.4中的命令來測試它是否正常工作:
實驗文檔下載地址:http://files.cnblogs.com/files/MenAngel/NetBlog10.zip