NAT概述

随着Internet的发展和网络应用的增多,有限的IPv4共有地址已经称为制约网络发展的瓶颈。为了解决这个问题,NAT(Network Address Translation,网络地址转换)技术应需而生。

NAT技术主要用于实现内部网络的主机访问外部网络。一方面NAT缓解了IPv4地址短缺的问题,另一方面NAT技术让外部网无法直接与使用私有地址的内网进行通信,提升了内网的安全性。

私网地址

公有地址:由专门的机构管理、分配,可以在Internet上直接通信的IP地址。

私有地址:组织和个人可以任意使用,无法在Internet上直接通信,只能在内网使用的地址。

A、B、C类地址中各预留了一些地址专门作为私有IP地址:

A类:10.0.0.0~10.255.255.255

B类:172.16.0.0~172.31255.255

C类:192.168.0.0~192.168.255.255

NAT技术原理

NAT:对IP数据报文中IP地址进行转换,是一种在现网中被广泛部署的技术,一般部署在网络出口设备,例如路由器或防火墙上。

NAT的典型应用场景:在私有网络内部(如园区、家庭)使用私有地址,出口设备部署NAT,对于“从内到外”的流量,网络设备通过NAT将数据包的源地址进行转换(转换为特定的公有地址),而对于“从外到内的”流量,则对数据包的目的地址进行转换。

通过私有地址的使用结合NAT技术,可以有效地节约公网IPv4地址。

image-1

静态NAT

静态NAT原理

静态NAT:每个私有地址都有一个与之对应并且固定的共有地址,即私有地址和公有地址之间的关系是一对一映射。

支持双向互访,私有地址访问Internet经过出口设备NAT转换时,会被转换成对应的公有地址。同时,外部网络访问内部网络时,其报文中携带的公有地址(目的地址)也会被NAT设备转换成对印的个私有地址。

image-2

静态NAT转换示例

image-3

静态NAT实验

image-4

基本配置

SW1配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sys
sysname SW1
undo info-center enable
vlan batch 10 20 2
int vlanif 10
ip add 10.0.10.254 24
int vlanif 20
ip add 10.0.20.254 24
int g0/0/1
port link-type access
port default vlan 10
int g0/0/02
port link-type access
port default vlan 20
int vlanif 2
ip add 10.0.12.1 24
int g0/0/3
port link-type access
port default vlan 2
quit

AR2配置

1
2
3
4
5
6
7
sys
sysname AR2
int g0/0/0
ip add 10.0.12.2 24
int g0/0/1
ip add 100.1.1.2 29
quit

AR1配置

1
2
3
4
5
6
7
sys
sysname AR3
int g0/0/0
ip add 100.1.1.1 29
int g0/0/1
ip add 200.1.1.254 24
quit

配置静态路由

AR2配置

1
2
3
4
ip route-static 10.0.10.0 24 10.0.12.1
ip route-static 10.0.20.0 24 10.0.12.1
#配置缺省路由确保AR2能访问公网
ip route-static 0.0.0.0 0 100.1.1.1

SW1配置

1
ip route-static 0.0.0.0 0 10.0.12.2

配置NAT

NAT配置都在出口设备的出接口进行配置。

AR2配置

1
2
3
int g0/0/1
nat static global 100.1.1.3 inside 10.0.10.1
nat static global 100.1.1.4 inside 10.0.20.1

联通性测试

Client1访问公网中的server1

image-5

PC1访问公网中的server1

image-6

动态NAT

动态NAT原理

动态NAT:静态NAT严格地一对一进行地址映射,这就导致即便内网主机长时间离线或者不发送数据时,与之对应地共有地址也处于使用状态。为了避免地址浪费,动态NAT提出了地址池地概念:所有可用地公有地址组成地址池。

当内部主机访问外部网络时临时分配一个地址池中未使用地址,并将该地址标记为“In Use”。当该主机不再访问外部网络时回收分配地地址,重新标记为“Not Use”。

image-7

image-8

image-9

NAPT

NAPT原理

动态NAT选择地址池中地地址进行地址转换时不会转换端口,即No-PAT(No-Port Address Translation,非端口地址转换),公有地址与私有地址还是1:1的映射关系,无法提高公有地址利用率。

NAPT(Network Address and Port Translation,网络地址端口转换):从地址池中选择地址进行地址转换时不仅转换IP地址,同时也会对端口号进行转换,从而实现公有地址与私有地址的1:n映射,可以有效提高公网地址利用率。

image-10

image-11

image-12

NAPT配置示例

image-13

在R1上配置NAPT让内网所有私有地址通过122.1.2.1访问公网

1
2
3
4
5
6
nat address-group 1 122.1.2.1(开始IP地址) 122.1.2.1(结束IP地址)
acl 2000
rule permit source 192.168.1.0 0.0.0.255
quit
int g0/0/1
nat outbound 2000 address-group 1

NAPT实验

image-14

基本配置

SW1配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sys
sysname SW1
undo info-center enable
vlan batch 10 20 2
int vlanif 10
ip add 10.0.10.254 24
int vlanif 20
ip add 10.0.20.254 24
int g0/0/1
port link-type access
port default vlan 10
int g0/0/02
port link-type access
port default vlan 20
int vlanif 2
ip add 10.0.12.1 24
int g0/0/3
port link-type access
port default vlan 2
quit

AR2配置

1
2
3
4
5
6
7
sys
sysname AR2
int g0/0/0
ip add 10.0.12.2 24
int g0/0/1
ip add 100.1.1.2 29
quit

AR1配置

1
2
3
4
5
6
7
sys
sysname AR3
int g0/0/0
ip add 100.1.1.1 29
int g0/0/1
ip add 200.1.1.254 24
quit

配置静态路由

AR2配置

1
2
3
4
ip route-static 10.0.10.0 24 10.0.12.1
ip route-static 10.0.20.0 24 10.0.12.1
#配置缺省路由确保AR2能访问公网
ip route-static 0.0.0.0 0 100.1.1.1

SW1配置

1
ip route-static 0.0.0.0 0 10.0.12.2

NAPT配置

AR2配置

配置地址池

1
nat addres-group 0 100.1.1.3 100.1.1.3

配置acl匹配需要访问公网的流量

1
2
3
4
acl 2000
rule permit source 10.0.10.0 0.0.0.255
rule permit source 10.0.20.0 0.0.0.255
quit

接口调用NAT

1
2
3
int g0/0/1
nat outbound 2000 address-group 0
quit

联通性测试

Client1访问公网中的server1

image-15

抓包分析

image-16

PC1访问公网中的server1

image-17

抓包分析

image-18

Easy IP

Easy IP概述

Easy IP:实现原理和NAPT相同,同时转换IP地址、传输层端口,区别在于Easy IP没有地址池的概念,使用接口地址作为NAT转换的公有地址。

Easy IP适用于不具备固定公网IP地址的场景:如通过DHCP、PPPoE拨号获取地址的私有网络出口,可以直接使用获取到的动态地址进行转换。

image-19

Easy IP配置示例

image-20

在R1上配置Easy IP让内网所有私有地址通过122.1.2.1访问公网。

1
2
3
4
5
acl 2000
rule permit source 192.168.1.0 0.0.0.255
quit
int g0/0/1
nat outbound 2000

NAT Server

NAR Server使用场景

NAT Server:指定[公有地址:端口]与[私有地址:端口]的一对一映射关系,将内网服务器映射到公网,当私有网络中的服务器需要对公网提供服务时使用。

外网主机主动访问[公有地址:端口]实现对内网服务器的访问。

image-21

image-22

NAT Server实验

image-20251002181721757

基本配置

SW1配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sys
sysname SW1
undo info-center enable
vlan batch 2 10 20
int vlanif 2
ip add 10.0.12.1 24
int vlanif 10
ip add 10.0.10.254 24
int vlanif 20
ip add 10.0.20.254 24
int g0/0/1
port link-type access
port default vlan 10
int g0/0/2
port link-type access
port default vlan 20
int g0/0/3
port link-type access
port default vlan 2
quit

AR2配置

1
2
3
4
5
6
7
sys
sysname AR2
int g0/0/0
ip add 10.0.12.2 24
int g0/0/1
ip add 100.1.1.2 29
quit

AR1配置

1
2
3
4
5
6
7
sys
sysname AR1
int g0/0/0
ip add 100.1.1.1 29
int g0/0/1
ip add 200.1.1.254 29
quit

静态路由配置

AR2

1
2
3
ip route-static 10.0.10.0 24 10.0.12.1
ip route-static 10.0.20.0 24 10.0.12.1
ip route-static 0.0.0.0 0 100.1.1.1

SW1

1
ip route-static 0.0.0.0 0 10.0.12.2

Easy IP配置

AR2

1
2
3
4
5
6
acl 2000
rule permit source 10.0.10.0 0.0.0.255
rule permit source 10.0.20.0 0.0.0.255
quit
int g0/0/1
nat outbound 2000

NAT Server配置

AR2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
int g0/0/1
[AR2-GigabitEthernet0/0/1]nat server ?
global Specify global information of NAT
protocol Specify protocol
[AR2-GigabitEthernet0/0/1]nat server protocol ?
<1-255> Protocol number
icmp Internet Control Message Protocol (1)
tcp Transmission Control Protocol (6)
udp User Datagram Protocol (17)
[AR2-GigabitEthernet0/0/1]nat server protocol tcp ?
global Specify global information of NAT
[AR2-GigabitEthernet0/0/1]nat server protocol tcp global ?
X.X.X.X Global IP address of NAT
current-interface Address of current interface
interface Specify the interface
[AR2-GigabitEthernet0/0/1]nat server protocol tcp global current-interface ?
<0-65535> Global port of NAT
CHARgen Character generator (19)
any Any protocol (0)
bgp Border Gateway Protocol (179)
cmd Remote commands (rcmd, 514)
daytime Daytime (13)
discard Discard (9)
domain Domain Name Service (53)
echo Echo (7)
exec Exec (rsh, 512)
finger Finger (79)
ftp File Transfer Protocol (21)
gopher Gopher (70)
hostname NIC hostname server (101)
irc Internet Relay Chat (194)
klogin Kerberos login (543)
kshell Kerberos shell (544)
login Login (rlogin, 513)
lpd Printer service (515)
nntp Network News Transport Protocol (119)
pop2 Post Office Protocol v2 (109)
pop3 Post Office Protocol v3 (110)
smtp Simple Mail Transport Protocol (25)
sunrpc Sun Remote Procedure Call (111)
tacacs TAC Access Control System (49)
talk Talk (517)
telnet Telnet (23)
time Time (37)
uucp Unix-to-Unix Copy Program (540)
whois Nicname (43)
www World Wide Web (HTTP, 80)
[AR2-GigabitEthernet0/0/1]nat server protocol tcp global current-interface 10000
?
inside Specify inside information of NAT
[AR2-GigabitEthernet0/0/1]nat server protocol tcp global current-interface 10000
inside ?
IP_ADDR<X.X.X.X> Inside IP address of NAT
[AR2-GigabitEthernet0/0/1]nat server protocol tcp global current-interface 10000
inside 10.0.10.1 ?
<0-65535> Inside port of NAT
CHARgen Character generator (19)
acl Specify acl configuration information
any Any protocol (0)
bgp Border Gateway Protocol (179)
cmd Remote commands (rcmd, 514)
daytime Daytime (13)
description Specify NAT description
discard Discard (9)
domain Domain Name Service (53)
echo Echo (7)
exec Exec (rsh, 512)
finger Finger (79)
ftp File Transfer Protocol (21)
gopher Gopher (70)
hostname NIC hostname server (101)
irc Internet Relay Chat (194)
klogin Kerberos login (543)
kshell Kerberos shell (544)
login Login (rlogin, 513)
lpd Printer service (515)
nntp Network News Transport Protocol (119)
pop2 Post Office Protocol v2 (109)
pop3 Post Office Protocol v3 (110)
smtp Simple Mail Transport Protocol (25)
sunrpc Sun Remote Procedure Call (111)
tacacs TAC Access Control System (49)
talk Talk (517)
telnet Telnet (23)
time Time (37)
uucp Unix-to-Unix Copy Program (540)
vpn-instance Specify a VPN instance
whois Nicname (43)
www World Wide Web (HTTP, 80)
<cr> Please press ENTER to execute command
[AR2-GigabitEthernet0/0/1]nat server protocol tcp global current-interface 10000
inside 10.0.10.1 80

###nat server protocol [协议:TCP,UDP,ICMP] global [出口的公网地址设置类型:ip_address(购买的公网IP地址),current-interface(当前出接口的公网IP地址,适用于小型企业)] [port:访问的端口号] inside [ip_address:访问的私网IP地址] [port:内部服务的端口号]

访问企业内部的web服务

image-24