天天看點

網絡位址轉換 NAT 配置

了解 NAT 網絡位址轉換的原理及功能

掌握靜态 NAT 的配置,實作區域網路通路網際網路

實驗背景

你是某公司的網絡管理者,欲釋出公司的 WWW 服務。現要求将内網 Web 伺服器 IP位址映射為全局 IP 位址,實作外部網絡可以通路公司内部 Web 伺服器。

技術原理

網絡位址轉換 NAT(Network Address Translation),被廣泛應用于各種類型 Internet 接入方式和各種類型的網絡中。原因很簡單,NAT 不僅完美地解決了 IP 位址不足的問題,而且還能夠有效地避免來自網絡外部的攻擊,隐藏并保護網絡内部的計算機。預設情況下,内部 IP 位址是無法被路由到外網的,内部主機 10.1.1.1 要與外部 Internet通信,IP 包到達 NAT 路由器時,IP 標頭的源位址 10.1.1.1 被替換成一個合法的外網 IP,并在 NAT 轉發表中儲存這條記錄。當外部主機發送一個應答到内網時,NAT 路由器收到後,檢視目前 NAT 轉換表,用 10.1.1.1 替換掉這個外網位址。NAT 将網絡劃分為内部網絡和外部網絡兩部分,區域網路主機利用 NAT 通路網絡時,是将區域網路内部的本地位址轉換為全局位址(網際網路合法的 IP 位址)後轉發資料包;

NAT 分為兩種類型:NAT(網絡位址轉換)和 NAPT(網絡端口位址轉換 IP 位址對應一個全局位址)。

靜态 NAT:實作内部位址與外部位址一對一的映射。現實中,一般都用于伺服器;

動态 NAT:定義一個位址池,自動映射,也是一對一的。現實中,用得比較少;

NAPT:使用不同的端口來映射多個内網 IP 位址到一個指定的外網 IP 位址,多對一。

實驗步驟

建立 Packet Tracer 拓撲圖

網絡位址轉換 NAT 配置網絡位址轉換 NAT 配置

(1)R1 為公司出口路由器,其與外部路由器之間通過 V.35 電纜序列槽連接配接,DCE 端連接配接在 R1 上,配置其時鐘頻率 64000;

(2)配置 PC 機、伺服器及路由器接口 IP 位址;

(3)在各路由器上配置靜态路由協定,讓 PC 間能互相 Ping 通;

(4)在 R1 上配置靜态 NAT。

(5)在 R1 上定義内外網絡接口。

(6)驗證主機之間的互通性。

實驗裝置

PC 1 台;Server-PT 1 台;Switch_2950-24 1 台;Router-PT 2 台;直連線;交叉線;DCE序列槽線

Server-PT

192.168.1.2

255.255.255.0

192.168.1.1

PC0

222.0.2.2

222.0.2.1

R1

en

conf t

host R1

int fa 0/0

ip address 192.168.1.1 255.255.255.0

no shutdown

int s 2/0

ip address 222.0.1.1 255.255.255.0

clock rate 64000

R2

host R2

ip address 222.0.1.2 255.255.255.0

no shut

ip address 222.0.2.1 255.255.255.0

exit;

ip route 222.0.2.0 255.255.255.0 222.0.1.2

exit

ip route 192.168.1.0 255.255.255.0 222.0.1.1

end

show ip route

CMD

ping 192.168.1.2 (success)

Web 浏覽器

http://192.168.1.2

(success)

ip nat inside

ip nat outside

ip nat inside source static 192.168.1.2 222.0.1.3

show ip nat translations

http://222.0.1.3

實戰演練

Continue with configuration dialog? [yes/no]: n

Press RETURN to get started!

Router>en

Router#conf t

Enter configuration commands, one per line. End with CNTL/Z.

Router(config)#hostname R1

R1(config)#int fa 0/0

R1(config-if)#ip add 192.168.1.1 255.255.255.0

R1(config-if)#no shut

R1(config-if)#

%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up

R1(config-if)#int s 2/0

R1(config-if)#ip add 222.0.1.1 255.255.255.0

%LINK-5-CHANGED: Interface Serial2/0, changed state to down

R1(config-if)#clock rate 64000

Router(config)#host R2

R2(config)#int s 2/0

R2(config-if)#ip add 222.0.1.2 255.255.255.0

R2(config-if)#no shut

%LINK-5-CHANGED: Interface Serial2/0, changed state to up

R2(config-if)#int fa 0/0

R2(config-if)#ip add 222.0.2.1 255.255.255.0

%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up

R2(config-if)#

R1(config-if)#exit

R1(config)#

R1(config)#ip route 222.0.2.0 255.255.255.0 222.0.1.2

R2(config-if)#exit

R2(config)#

R2(config)#ip route 192.168.1.0 255.255.255.0 222.0.1.1

R2(config)#end

R2#

%SYS-5-CONFIG_I: Configured from console by console

R2#show ip route

Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP

D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
   N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
   E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
   i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
   * - candidate default, U - per-user static route, o - ODR
   P - periodic downloaded static route
           

Gateway of last resort is not set

S 192.168.1.0/24 [1/0] via 222.0.1.1

C 222.0.1.0/24 is directly connected, Serial2/0

C 222.0.2.0/24 is directly connected, FastEthernet0/0

CMD ping 192.168.1.2 (success)

PC>ipconfig

IP Address......................: 222.0.2.2

Subnet Mask.....................: 255.255.255.0

Default Gateway.................: 222.0.2.1

PC>ping 192.168.1.2

Pinging 192.168.1.2 with 32 bytes of data:

Request timed out.

Reply from 192.168.1.2: bytes=32 time=19ms TTL=126

Reply from 192.168.1.2: bytes=32 time=18ms TTL=126

Reply from 192.168.1.2: bytes=32 time=23ms TTL=126

Ping statistics for 192.168.1.2:

Packets: Sent = 4, Received = 3, Lost = 1 (25% loss),           

Approximate round trip times in milli-seconds:

Minimum = 18ms, Maximum = 23ms, Average = 20ms
           

Reply from 192.168.1.2: bytes=32 time=26ms TTL=126

Reply from 192.168.1.2: bytes=32 time=22ms TTL=126

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),           
Minimum = 22ms, Maximum = 26ms, Average = 24ms
           

PC>

R1>en

R1#conf t

R1(config-if)#ip nat inside

R1(config-if)#ip nat outside

R1(config)#ip nat inside source static 192.168.1.2 222.0.1.3

R1(config)#end

R1#

R1#show ip nat translations

Pro Inside global Inside local Outside local Outside global

--- 222.0.1.3 192.168.1.2 --- ---

CMD ping 222.0.1.3

PC>ping 222.0.1.3

Pinging 222.0.1.3 with 32 bytes of data:

Reply from 222.0.1.3: bytes=32 time=21ms TTL=126

Reply from 222.0.1.3: bytes=32 time=22ms TTL=126

Reply from 222.0.1.3: bytes=32 time=23ms TTL=126

Ping statistics for 222.0.1.3:

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),           
Minimum = 21ms, Maximum = 23ms, Average = 22ms           

繼續閱讀