动态nat实验配置
如图1所示,某公司A区和B区的私网用户和互联网相连,路由器上接口GigabitEthernet3/0/0的公网地址为2.2.2.1/24,对端运营商侧地址为2.2.2.2/24。A区用户希望使用公网地址池中的地址(2.2.2.100~2.2.2.200)采用NAT方式替换A区内部的主机地址(网段为192.168.20.0/24),访问因特网。B区用户希望结合B区的公网IP地址比较少的情况,使用公网地址池(2.2.2.80~2.2.2.83)采用IP地址和端口的替换方式替换B区内部的主机地址(网段为10.0.0.0/24),访问因特网。
图1 配置动态地址转换为组网图
配置动态地址转换的思路如下:配置接口IP地址、缺省路由和在WAN侧接口下配置NAT Outbound,实现内部主机访问外网服务功能。
1、在Router上配置接口IP地址
[Huawei] sysname Router
[Router] vlan 100
[Router-vlan100] quit
[Router] interface vlanif 100
[Router-Vlanif100] ip address 192.168.20.1 24
[Router-Vlanif100] quit
[Router] interface ethernet 2/0/0
[Router-Ethernet2/0/0] port link-type access
[Router-Ethernet2/0/0] port default vlan 100
[Router-Ethernet2/0/0] quit
[Router] vlan 200
[Router-vlan200] quit
[Router] interface vlanif 200
[Router-Vlanif200] ip address 10.0.0.1 24
[Router-Vlanif200] quit
[Router] interface ethernet 2/0/1
[Router-Ethernet2/0/1] port link-type access
[Router-Ethernet2/0/1] port default vlan 200
[Router-Ethernet2/0/1] quit
[Router] interface gigabitethernet 3/0/0
[Router-GigabitEthernet3/0/0] ip address 2.2.2.1 24
[Router-GigabitEthernet3/0/0] quit
[Router] ip route-static 0.0.0.0 0.0.0.0 2.2.2.2
3、在Router上配置NAT Outbound
[Router] nat address-group 1 2.2.2.100 2.2.2.200
[Router] nat address-group 2 2.2.2.80 2.2.2.83
[Router] acl 2000
[Router-acl-basic-2000] rule 5 permit source 192.168.20.0 0.0.0.255
[Router-acl-basic-2000] quit
[Router] acl 2001
[Router-acl-basic-2001] rule 5 permit source 10.0.0.0 0.0.0.255
[Router-acl-basic-2001] quit
[Router] interface gigabitethernet 3/0/0
[Router-GigabitEthernet3/0/0] nat outbound 2000 address-group 1 no-pat
[Router-GigabitEthernet3/0/0] nat outbound 2001 address-group 2
[Router-GigabitEthernet3/0/0] quit
说明:如果需要在Router上执行ping -a source-ip-address命令通过指定发送ICMP ECHO-REQUEST报文的源IP地址来验证内网用户可以访问因特网,需要配置命令ip soft-forward enhance enable使能设备产生的控制报文的增强转发功能,这样,私网的源地址才能通过NAT转换为公网地址。缺省情况下,设备产生的控制报文的增强转发功能处于使能状态。如果之前已经执行命令undo ip soft-forward enhance enable去使能增强转发功能,需要重新在系统视图下执行命令ip soft-forward enhance enable。
4、验证配置结果
# 在Router上执行命令display nat outbound,查看地址转换结果。
NAT Outbound Information:
-----------------------------------------------------------------
Interface Acl Address-group/IP/Interface Type
-----------------------------------------------------------------
GigabitEthernet3/0/0 2000 1 no-pat
GigabitEthernet3/0/0 2001 2 pat
-----------------------------------------------------------------
Total : 2
# 在Router上执行命令ping,验证内网可以访问因特网。
PING 2.2.2.2: 56 data bytes, press CTRL_C to break
Reply from 2.2.2.2: bytes=56 Sequence=1 ttl=255 time=1 ms
Reply from 2.2.2.2: bytes=56 Sequence=2 ttl=255 time=1 ms
Reply from 2.2.2.2: bytes=56 Sequence=3 ttl=255 time=1 ms
Reply from 2.2.2.2: bytes=56 Sequence=4 ttl=255 time=1 ms
Reply from 2.2.2.2: bytes=56 Sequence=5 ttl=255 time=1 ms
-- 2.2.2.2 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 1/1/2 ms
PING 2.2.2.2: 56 data bytes, press CTRL_C to break
Reply from 2.2.2.2: bytes=56 Sequence=1 ttl=255 time=1 ms
Reply from 2.2.2.2: bytes=56 Sequence=2 ttl=255 time=1 ms
Reply from 2.2.2.2: bytes=56 Sequence=3 ttl=255 time=1 ms
Reply from 2.2.2.2: bytes=56 Sequence=4 ttl=255 time=1 ms
Reply from 2.2.2.2: bytes=56 Sequence=5 ttl=255 time=1 ms
-- 2.2.2.2 ping statistics ---
5 packet(s) transmitted
5 packet(s) received
0.00% packet loss
round-trip min/avg/max = 1/1/2 ms
Router的配置文件
#
sysname Router
#
vlan batch 100 200
#
acl number 2000
rule 5 permit source 192.168.20.0 0.0.0.255
#
acl number 2001
rule 5 permit source 10.0.0.0 0.0.0.255
#
nat address-group 1 2.2.2.100 2.2.2.200
nat address-group 2 2.2.2.80 2.2.2.83
#
interface Vlanif100
ip address 192.168.20.1 255.255.255.0
#
interface Vlanif200
ip address 10.0.0.1 255.255.255.0
#
interface Ethernet2/0/0
port link-type access
port default vlan 100
#
interface Ethernet2/0/1
port link-type access
port default vlan 200
#
interface GigabitEthernet3/0/0
ip address 2.2.2.1 255.255.255.0
nat outbound 2000 address-group 1 no-pat
nat outbound 2001 address-group 2
#
ip route-static 0.0.0.0 0.0.0.0 2.2.2.2
#
return
NAPT实验配置
除了一对一的NAT转换方式外,网络地址端口转换NAPT(Network Address Port Translation)可以实现并发的地址转换。它允许多个内部地址映射到同一个公有地址上,因此也可以称为“多对一地址转换”或地址复用。NAPT方式属于多对一的地址转换,它通过使用“IP地址+端口号”的形式进行转换,使多个私网用户可共用一个公网IP地址访问外网。
图1 NAPT示意图
图1描述了NAPT的基本原理,实现过程如下:
1、Router收到内网侧Host发送的访问公网侧Server的报文。比如收到Host A报文的源地址是10.1.1.100,端口号1025。
2、Router从地址池中选取一对空闲的“公网IP地址+端口号”,建立与内网侧报文“源IP地址+源端口号”间的NAPT转换表项(正反向),并依据查找正向NAPT表项的结果将报文转换后向公网侧发送。比如Host A的报文经Router转换后的报文源地址为1.1.1.1,端口号16384。
3、Router收到公网侧的回应报文后,根据其“目的IP地址+目的端口号”查找反向NAPT表项,并依据查表结果将报文转换后向私网侧发送。比如Server回应Host A的报文经Router转换后,目的地址为10.1.1.100,端口号1025。