思科路由器配置NAT

端口映射是指在路由器开放一个端口,映射到相应的计算机。当外网有数据访问路由器开放的端口时,路由器直接将来自于这个端口的所有数据转发到相应的计算机上,而不是路由器自己接收。这种技术可以把一个公网地址映射到多台内网服务器上。

组网结构及需求

某企业的网络拓扑,路由器通过专线连接到外网,只有一个公网地址:202.99.111.2;内网的网段是192.168.0.0/24,内网有一台Web服务器(192.168.0.100)和一台Ftp服务器(192.168.0.101)需要同时对外网提供服务。要求在路由器上设置端口映射,把公网地址202.99.111.2的TCP80端口映射到Web服务器服务器;TCP21端口映射到Ftp服务器。

配置步骤和方法

配置路由器接口地址和路由

1
2
3
4
5
6
7
8
9
10
11
12
13
interface FastEthernet0/0
ip address 192.168.0.1 255.255.255.0
ip nat inside (nat内网接口)
duplex auto
speed auto
interface Serial0/0
description to internet
ip address 202.99.111.2 255.255.255.252
ip nat outside
(nat外网接口)
encapsulation ppp
ip route 0.0.0.0 0.0.0.0 202.99.111.1
(默认路由指向internet)

配置NAT(假如内网地址不访问公网,此步骤可以不做)

1
2
3
ip nat inside source list 1 interface Serial0/0 overload 
(将192.168.0.0的地址转换为接口s1/1的地址)
access-list 1 permit 192.168.0.0 0.0.0.255 (建立访问控制列表允许192.168.0.0的地址做nat转换)

配置端口映射

1
2
ip nat inside source static tcp 192.168.0.100 80 202.99.111.2 80 extendable
ip nat inside source static tcp 192.168.0.101 21 202.99.111.2 21 extendable

把公网地址202.99.111.2的tcp80端口和tcp21端口分别映射到192.168.0.100和192.168.0.101服务器;因为202.99.111.2这个地址已经应用在s0/0接口上并做了NAT转换的地址,这里必须加上extendable这个关键字,否则报错。如果用另外的外网ip比如202.99.111.3,在这里就可以不加extendable。

端口映射信息查询

命令show ip nat translations可以查看nat转换情况,如下:

1
2
3
4
5
6
Router#sh ip nat translations
Pro Inside global Inside local Outside local Outside global
tcp 202.99.111.2:21 192.168.0.100:21 202.99.111.1:11000 202.99.111.1:11000
tcp 202.99.111.2:80 192.168.0.100:80 — —
tcp 202.99.111.2:21 192.168.0.100:21 — —
tcp 202.99.111.2:80 192.168.0.100:80 202.99.111.1:11001 202.99.111.1:11001

路由器配置

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
Router#sh run
Building configuration…
Current configuration : 1148 bytes
!
version 12.2
service timestamps debug uptime
service timestamps log uptime
no service password-encryption
!
hostname Router
!
memory-size iomem 15
ip subnet-zero
!
call rsvp-sync
!
interface FastEthernet0/0
ip address 192.168.0.1 255.255.255.0
ip nat inside
duplex auto
speed auto
!
interface Serial0/0
description to internet
ip address 202.99.111.2 255.255.255.252
ip nat outside
encapsulation ppp
!
interface FastEthernet0/1
no ip address
shutdown
duplex auto
speed auto
!
interface Serial0/1
no ip address
shutdown
!
interface Serial0/2
no ip address
shutdown
!
interface Serial0/3
no ip address
shutdown
!
!
ip nat inside source list 1 interface Serial0/0 overload
ip nat inside source static tcp 192.168.0.100 80 202.99.111.2 80 extendable
ip nat inside source static tcp 192.168.0.101 21 202.99.111.2 21 extendable
ip classless
ip route 0.0.0.0 0.0.0.0 202.99.111.1
ip http server
!
access-list 1 permit 192.168.0.0 0.0.0.255
!
dial-peer cor custom
!
line con 0
line aux 0
line vty 0 4
!
end
%