网络地址转换(Network Address Translation, NAT) 是一种在IP数据包传输过程中修改源或目标IP地址及端口的技术,主要用于解决IPv4地址短缺问题。随着互联网设备激增,NAT通过允许多个设备共享单一公网IP地址,显著延缓了IPv4地址耗尽的速度。
NAT的核心是通过中间设备(如路由器、防火墙)建立内网私有地址与外网公有地址的映射关系,主要流程如下:
优点:
缺点:
1.配置 IP 地址部分结果
[R1]display ip interface brief
*down: administratively down
(s): spoofing (l): loopback
Interface Physical Protocol IP address/Mask VPN instance Description
GE0/0 up up -- -- --
GE0/1 up up 100.1.1.1/24 -- --
[R2]display ip interface brief
*down: administratively down
(s): spoofing (l): loopback
Interface Physical Protocol IP address/Mask VPN instance Description
GE0/0 up up 100.1.1.2/24 -- --
GE0/1 up up 100.2.2.2/24 -- --
[R3]display ip interface brief
*down: administratively down
(s): spoofing (l): loopback
Interface Physical Protocol IP address/Mask VPN instance Description
GE0/0 up up 100.2.2.3/24 -- --
GE0/1 up up 192.168.1.254/24 -- --
[PCB]display ip interface brief
*down: administratively down
(s): spoofing (l): loopback
Interface Physical Protocol IP address/Mask VPN instance Description
GE0/0 up up 192.168.1.4/24 -- --
[pcA]display ip interface brief
*down: administratively down
(s): spoofing (l): loopback
Interface Physical Protocol IP address/Mask VPN instance Description
GE0/0 up up 192.168.2.1/24 -- --
[FTP-A]display ip interface brief
*down: administratively down
(s): spoofing (l): loopback
Interface Physical Protocol IP address/Mask VPN instance Description
GE0/0 up up 192.168.1.1/24 -- --
2.R1 和 R3 上配置默认路由指向公网,配置步骤
分别配置R1、R3、PCB、FTP-A、PCA的静态路由:
[R1]ip route-static 0.0.0.0 0 100.1.1.2
[R3]ip route-static 0.0.0.0 0 100.2.2.2
[PCB]ip route-static 0.0.0.0 0 192.168.1.254
[FTP-A]ip route-static 0.0.0.0 0 192.168.1.254
[PCA]ip route-static 0.0.0.0 0 192.168.2.254
3.私网 A 内部单臂路由配置部分
[SWA]vlan 10
[SWA-vlan10]port g1/0/1
[SWA-vlan10]vlan 20
[SWA-vlan20]port g1/0/2
[SWA-vlan20]int g1/0/3
[SWA-GigabitEthernet1/0/3]port link-type trunk
[SWA-GigabitEthernet1/0/3]port trunk permit vlan 10 20
[R1]int g0/0.1
[R1-GigabitEthernet0/0.1]vlan-type dot1q vid 10
[R1-GigabitEthernet0/0.1]ip add 192.168.1.254 24
[R1-GigabitEthernet0/0.1]quit
[R1]int g0/0.2
[R1-GigabitEthernet0/0.2]vlan-type dot1q vid 20
[R1-GigabitEthernet0/0.2]ip address 192.168.2.254 24
4.私网 A 通过 NAPT 使 Vlan10 和 Vlan20 都能够使用 R1 的公网地址访问互联网
步骤 1:R1 上创建基本 ACL,允许 192.168.1.0/24 和 192.168.2.0/24 网段
[R1]acl basic 2000
[R1-acl-ipv4-basic-2000]rule permit source 192.168.1.0 0.0.0.255
[R1-acl-ipv4-basic-2000]rule permit source 192.168.2.0 0.0.0.255
步骤 2:R1 上创建 NAT 地址池,设置公网地址
[R1]nat address-group 1
[R1-address-group-1]address 100.1.1.1 100.1.1.1
步骤 3:在 R1 的公网接口上配置 NAPT
[R1]interface g0/1
[R1-GigabitEthernet0/1]nat outbound 2000 address-group 1
步骤 4:在 PCA 上 Ping R3 的公网地址,测试是否可以访问互联网
ping 100.2.2.3
Ping 100.2.2.3 (100.2.2.3): 56 data bytes, press CTRL+C to break
56 bytes from 100.2.2.3: icmp_seq=0 ttl=253 time=1.889 ms
56 bytes from 100.2.2.3: icmp_seq=1 ttl=253 time=4.122 ms
56 bytes from 100.2.2.3: icmp_seq=2 ttl=253 time=2.798 ms
56 bytes from 100.2.2.3: icmp_seq=3 ttl=253 time=1.617 ms
56 bytes from 100.2.2.3: icmp_seq=4 ttl=253 time=1.591 ms
私网 B 通过在 R3 上配置 EASY IP 访问互联网
步骤 1:R3 上创建基本 ACL,允许 192.168.1.0/24 网段
[R3]acl basic 2000
[R3-acl-ipv4-basic-2000]rule permit source 192.168.1.0 0.0.0.255
步骤 2:在 R3 的公网接口上配置 EASY IP
[R3]interface g0/0
[R3-GigabitEthernet0/0]nat outbound 2000
步骤 4:在 PCB 上 Ping R1 的公网地址,测试是否可以访问互联网
ping 100.1.1.1
Ping 100.1.1.1 (100.1.1.1): 56 data bytes, press CTRL+C to break
56 bytes from 100.1.1.1: icmp_seq=0 ttl=253 time=1.797 ms
56 bytes from 100.1.1.1: icmp_seq=1 ttl=253 time=2.180 ms
56 bytes from 100.1.1.1: icmp_seq=2 ttl=253 time=2.153 ms
56 bytes from 100.1.1.1: icmp_seq=3 ttl=253 time=1.030 ms
56 bytes from 100.1.1.1: icmp_seq=4 ttl=253 time=1.132 ms
私网 A 配置 NAT SERVER 把 FTPA 的 FTP 服务发布到公网,使 PCB 可以访问
步骤1:配置FTP服务
[FTP-A]ftp server enable
[FTP-A]local-user root class manage
New local user added.
[FTP-A-luser-manage-root]password simple 123456.com
[FTP-A-luser-manage-root]service-type ftp
[FTP-A-luser-manage-root]authorization-attribute user-role level-15
步骤 2:在 R1 的公网接口上配置 NAT SERVER,映射端口 20 和 21
[R1-GigabitEthernet0/1]nat server protocol tcp global current-interface 20 21 inside 192.168.1.10 20 21
步骤 3:在 PCB 上测试是否能够通过 R1 的公网地址访问 FTPA 的 FTP 服务
ftp 100.1.1.1
Press CTRL+C to abort.
Connected to 100.1.1.1 (100.1.1.1).
220 FTP service ready.
User (100.1.1.1:(none)):
本次实验中需注意ACL 需要配置允许 192.168.1.0/24 和 192.168.2.0/24 网段;私网 A 只有 1 个公网地址可用,意味着创建的 NAT 地址池起始和结束地址就都是 100.1.1.1,使用 EASY IP,就无需配置 NAT 地址池,直接在公网接口上配置即可,EASY IP 会自动识别公网接口的 IP 地址。
220 FTP service ready.
User (100.1.1.1:(none)):
### **实验总结**
本次实验中需注意ACL 需要配置允许 192.168.1.0/24 和 192.168.2.0/24 网段;私网 A 只有 1 个公网地址可用,意味着创建的 NAT 地址池起始和结束地址就都是 100.1.1.1,使用 EASY IP,就无需配置 NAT 地址池,直接在公网接口上配置即可,EASY IP 会自动识别公网接口的 IP 地址。
NAT作为IPv4时代的救星,平衡了地址短缺与网络扩展需求。尽管IPv6逐步推广,NAT在过渡期和特定场景中仍不可或缺。理解其原理与限制,有助于优化网络设计并应对新兴技术挑战。