一、基礎
Cisco路由器配置中NAT的主要命令:
靜態NAT:
1、指定NAT內部接口
在內網相應接口的接口配置模式下執行:ip nat inside
2、指定NAT外部接口
在外網相應接口的接口配置模式下執行:ip nat outside
3、在內部本地地址與內部全局地址之間建立靜態地址轉換關系:
ip nat inside source static 內部本地地址 內部全局地址
4、在外部全局地址與外部本地地址之間建立靜態地址轉換關系:
ip nat outside source static 外部全局地址 外部本地地址
5、
show ip nat translations:顯示當前存在的NAT轉換信息。
show ip nat statics:查看NAT的統計信息。
show ip nat translations verbose:顯示當前存在的NAT轉換的詳細信息。
debug ip nat:跟蹤NAT操作,顯示出每個被轉換的數據包。
Clear ip nat translations *:刪除NAT映射表中的所有內容.
動態地址NAT:
1、指定NAT內部接口
在內網相應接口的接口配置模式下執行:ip nat inside
2、指定NAT外部接口
在外網相應接口的接口配置模式下執行:ip nat outside
3、在全局配置模式下,定義一個標准的access-list規則,聲明允許哪些內部本地地址可以進行動態地址轉換:
access-list list-number permit 源地址 通配符
其中,list-number為1-99之間的一個任意整數。
4、在全局配置模式下,定義內部全局地址池:
ip nat pool 地址池名 起始IP地址 終止IP地址 netmask 子網掩碼
其中,地址池名可以任意設定,但最好有一定的說明意義。
5、在全局配置模式下,定義符合先前定義的access-list規則的IP數據包與先前定義的地址池中的IP地址進行轉換:
ip nat inside source list list-number pool 內部全局地址池名
6、no ip nat pool name 刪除全局地址池
no access-list access-list-number 刪除訪問列表
no ip nat inside source 刪除動態源地址轉換
網絡地址端口轉換NAPT:
1、指定NAT內部接口
在內網相應接口的接口配置模式下執行:ip nat inside
2、指定NAT外部接口
在外網相應接口的接口配置模式下執行:ip nat outside
3、在全局配置模式下,定義內部全局地址池:
ip nat pool 地址池名 起始IP地址 終止IP地址 netmask 子網掩碼
其中,地址池名可以任意設定,但最好有一定的說明意義。
4、在全局配置模式下,定義一個標准的access-list規則,聲明允許哪些內部本地地址可以進行復用地址轉換:
access-list list-number permit 源地址 通配符
其中,list-number為1-99之間的一個任意整數。
5、在全局配置模式下,定義符合先前定義的access-list規則的IP數據包與先前定義的地址池中的IP地址進行復用地址轉換:
ip nat inside source list list-number pool 內部全局地址池名 overload
注:
內部本地地址、內部全局地址、外部本地地址、外部全局地址
ip nat inside/outside針對的是不同方向的流量/數據包。
ip nat inside是指從內網發送的數據包,提取包頭的源IP地址,轉換為外網地址;ip nat outside是指從外網返回的數據包,提取包頭的目的ip地址,轉換為內網地址。
inside/outside不是用來區分NAT路由器的內外部接口,而是用來區分需要NAT地址轉換的不同方向的流量。inside指內部網絡;outside指內部網絡需要連接的網絡,一般指互聯網
二、實例
1、靜態nat
基本命令
ip nat inside source static 192.168.1.10 13.0.1.3
ip nat inside
ip nat outside
這里先使用靜態nat使pc1能與R2連接通信
先配置pc1和pc2的ip
再配置路由器R1和R2
R1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#int f0/0
R1(config-if)#ip address 192.168.1.1 255.255.255.0
R1(config-if)#no sh
R1(config-if)#int
*Mar 1 00:02:12.327: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:02:13.327: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
R1(config-if)#int f1/0
R1(config-if)#ip address 13.0.1.1 255.255.255.0
R1(config-if)#no sh
R1(config-if)#
*Mar 1 00:02:41.819: %LINK-3-UPDOWN: Interface FastEthernet1/0, changed state to up
*Mar 1 00:02:42.819: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet1/0, changed state to up
R1(config-if)#exit
R1(config)#ip nat inside source static 192.168.1.10 13.0.1.3
R1(config-if)#int f0/0
R1(config-if)#ip nat inside
R1(config-if)#
*Mar 1 00:03:29.095: %LINEPROTO-5-UPDOWN: Line protocol on Interface NVI0, changed state to up
R1(config-if)#int f1/0
R1(config-if)#ip nat outside
R2#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R2(config)#int f0/0
R2(config-if)#ip address 13.0.1.2 255.255.255.0
R2(config-if)#no sh
測試
2、動態NAT
使用動態nat使pc2與R2通信
R1(config)#ip nat pool test 13.0.1.10 13.0.1.20 netmask 255.255.255.0
!定義內部全局地址池
R1(config)#access-list 10 permit 192.168.1.0 0.0.0.255
!定義訪問控制列表,內部本地地址范圍
R1(config)#ip nat inside source list 10 pool test
!建立映射關系
R1(config)#int f0/0
R1(config-if)#ip nat inside !定義內部網絡接口
R1(config)#int f1/0
R1(config-if)#ip nat outside !定義外部網絡接口
將192.168.1.0/24網段的內部主機動態轉換到全局地址13.0.1.10-13.0.1.20之間的ip,內部網絡的其他網段(非192.168.1)的主機不允許做NAT
測試
3、PAT
1)靜態PAT
R1(config)#ip nat inside source static {UDP|TCP} local-address port global-address port
例:
這里我們將C1的telnet服務映射到13.0.1.3的999端口,來測試一下靜態PAT
R3模擬為主機C1,並且開啟telnet服務
c1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
c1(config)#no ip routing
c1(config)#int f0/0
c1(config-if)#ip address 192.168.1.2 255.255.255.0
c1(config-if)#no sh
c1(config-if)#
*Mar 1 00:03:16.475: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:03:17.475: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
c1(config-if)#exit
c1(config)#ip def
c1(config)#ip default-g
c1(config)#ip default-gateway 192.168.1.1
c1(config)#end
c1#sho
*Mar 1 00:04:06.223: %SYS-5-CONFIG_I: Configured from console by console
c1#show ip route
Default gateway is 192.168.1.1
Host Gateway Last Use Total Uses Interface
ICMP redirect cache is empty
c1#
!開啟telnet
c1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
c1(config)#line vty 0 4
c1(config-line)#login
% Login disabled on line 130, until 'password' is set
% Login disabled on line 131, until 'password' is set
% Login disabled on line 132, until 'password' is set
% Login disabled on line 133, until 'password' is set
% Login disabled on line 134, until 'password' is set
c1(config-line)#password zwish
c1(config-line)#access-list 101 permit ip any any
c1(config-line)#end
R2
R2#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R2(config)#int f0/0
R2(config-if)#ip address 13.0.1.2 255.255.255.0
R2(config-if)#no sh
R2(config-if)#
*Mar 1 00:06:58.331: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:06:59.331: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
R2(config-if)#
R1配置靜態PAT
R1#en
R1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#int f0/0
R1(config-if)#ip address 192.168.1.1 255.255.255.0
R1(config-if)#no sh
R1(config-if)#int
*Mar 1 00:06:06.983: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:06:07.983: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
R1(config-if)#int f1/0
R1(config-if)#ip address 13.0.1.1 255.255.255.0
R1(config-if)#no sh
R1(config-if)#
*Mar 1 00:06:27.363: %LINK-3-UPDOWN: Interface FastEthernet1/0, changed state to up
*Mar 1 00:06:28.363: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet1/0, changed state to up
R1(config-if)#exit
R1(config)#ip nat inside source static tcp 192.168.1.2 23 13.0.1.3 999
R1(config)#int f0/0
R1(config-if)#ip nat inside
R1(config-if)#int f1/0
R1(config-if)#ip nat outside
R1(config-if)#exit
這里最重要異步就是將192.168.1.2的telnet服務映射到了13.0.1.3的999端口
最后配置好了,用R2來telenet登陸一下C1,telnet 13.0.1.3 999
成功!
抓個包看一下,源地址和目的地址分別為13.0.1.2和192.168.1.2
2)動態PAT
R1配置
R1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#int f0/0
R1(config-if)#ip address 192.168.1.1 255.255.255.0
R1(config-if)#no sh
R1(config-if)#int f1/0
R1(config-if)#
*Mar 1 00:01:11.115: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:01:12.115: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
R1(config-if)#int f1/0
R1(config-if)#ip address 192.168.1.1 255.255.255.0
% 192.168.1.0 overlaps with FastEthernet0/0
R1(config-if)#ip address 13.0.1.1 255.255.255.0
R1(config-if)#no sh
R1(config-if)#
*Mar 1 00:05:50.503: %LINK-3-UPDOWN: Interface FastEthernet1/0, changed state to up
*Mar 1 00:05:51.503: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet1/0, changed state to up
R1(config-if)#exit
R1(config)#ip nat pool test 13.0.1.3 13.0.1.3 netmask 255.255.255.0
R1(config)#ac
*Mar 1 00:07:20.143: %LINEPROTO-5-UPDOWN: Line protocol on Interface NVI0, changed state to up
R1(config)#access-list 10 permit 192.168.1.0 0.0.0.255
R1(config)#ip nat inside source list 10 pool test overload
R1(config)#int f0/0
R1(config-if)#ip nat inside
R1(config-if)#int f1/0
R1(config-if)#ip nat outside
R2
R2#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R2(config)#int f0/0
R2(config-if)#ip address 13.0.1.2 255.255.255.0
R2(config-if)#no sh
R2(config-if)#
*Mar 1 00:06:05.027: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar 1 00:06:06.027: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
R2(config-if)#
pc1和pc2
VPCS[1]> ip 192.168.1.2 192.168.1.1 24
Checking for duplicate address...
PC1 : 192.168.1.2 255.255.255.0 gateway 192.168.1.1
VPCS[1]> 2
VPCS[2]> ip 192.168.1.3 192.168.1.1 24
Checking for duplicate address...
PC2 : 192.168.1.3 255.255.255.0 gateway 192.168.1.1
VPCS[2]> show
NAME IP/MASK GATEWAY MAC LPORT RHOST:PORT
VPCS1 192.168.1.2/24 192.168.1.1 00:50:79:66:68:00 20000 127.0.0.1:30000
fe80::250:79ff:fe66:6800/64
VPCS2 192.168.1.3/24 192.168.1.1 00:50:79:66:68:01 20001 127.0.0.1:30001
fe80::250:79ff:fe66:6801/64
pc1和pc2分別ping R2,發現源地址都是13.0.1.3