目录
前言
本篇文章利用cisco packet tracer网络模拟进行nat端口映射,实现外网访问内网服务器。
一、搭建网络拓扑
1.1 配置server和pc
机器名 | ip地址 | 网关ip | 子网掩码 |
---|---|---|---|
server0 | 192.168.1.2 | 192.168.1.1 | 255.255.255.0 |
server1 | 192.168.1.3 | 192.168.1.1 | 255.255.255.0 |
pc0 | 114.25.63.24 | 114.25.63.1 | 255.255.255.0 |
1.1.1 配置server0
1.1.2 配置server1
1.1.3 配置pc0
1.2 配置客户路由器
1.2.1 配置路由器ip
接口名称 | ip地址 | 子网掩码 |
---|---|---|
fa0/0 | 192.168.1.1 | 255.255.255.0 |
fa0/1 | 66.1.1.1 | 255.255.255.0 |
配置信息如下:
1. 配置fa0/0接口
router>enable
router#
router#configure
router(config)#
router(config)#interface fa0/0
router(config-if)#ip address 192.168.1.1 255.255.255.0
router(config-if)#no shutdown
router(config-if)#exit
router(config)#
2. 配置fa0/1接口
router(config)#interface fa0/1
router(config-if)#ip address 66.1.1.1 255.255.255.0
router(config-if)#no shutdown
1.2.2 配置静态路由
为客户路由器配置默认路由,让客户路由器可以达到114.25.63.0/24网络。
命令:ip route 目标网段 目标网段掩码 下一跳路由的ip
这里设置默认路由
下一跳路由ip的意思是通过其可以到达目标网段
router(config)#ip route 0.0.0.0 0.0.0.0 66.1.1.254
或者可以设置静态路由
router(config)#ip route 114.25.63.0 255.255.255.0 66.1.1.254
以上两种配置的其中一种,均可以到达114.25.63.0/24网络
1.3 配置isp路由器
接口名称 | ip地址 | 子网掩码 |
---|---|---|
fa0/0 | 66.1.1.254 | 255.255.255.0 |
fa0/1 | 114.25.63.1 | 255.255.255.0 |
配置信息如下:
1. 配置fa0/0接口
router>enable
router#
router#configure
router(config)#
router(config)#interface fa0/0
router(config-if)#ip address 66.1.1.254 255.255.255.0
router(config-if)#no shutdown
router(config-if)#exit
router(config)#
2. 配置fa0/1接口
router(config)#interface fa0/1
router(config-if)#ip address 114.25.63.1 255.255.255.0
router(config-if)#no shutdown
以上配置完成后,测试各个主机的连通性,这里不再介绍。
二、配置端口映射
2.1 在客户路由器配置端口映射
由于这里使用http服务,则应该对应两个服务器的80端口进行映射
端口映射表 | |||
---|---|---|---|
私网ip | 私网机器端口 | 公网ip | 公网机器端口 |
192.168.1.2 | 80 | 66.1.1.2 | 6666 |
192.168.1.3 | 80 | 66.1.1.2 | 8888 |
配置信息如下:
router>enable
router#
router#configure
router(config)#
1. 配置接口fa0/0为内网口
router(config)#interface fa0/0
router(config-if)#ip nat inside
router(config-if)#exit
router(config)#
2. 配置接口fa0/1为外网口
router(config)#interface fa0/1
router(config-if)#ip nat outside
router(config-if)#exit
router(config)#
3. 私网ip 端口->公网ip 端口
命令:ip nat inside source static 协议(udp或tcp) 私网ip 端口 公网ip 端口
router(config)#ip nat inside source static tcp 192.168.1.2 80 66.1.1.2 6666
router(config)#ip nat inside source static tcp 192.168.1.3 80 66.1.1.2 8888
2.2 测试公网计算机访问私网服务器
2.2.1 pc0向server0发送http请求
如果映射成功,则可以通过66.1.1.2:6666访问server0(192.168.1.2)
这里,可以去修改server0的index.html的内容
2.2.1 pc0向server1发送http请求
如果映射成功,则可以通过66.1.1.2:8888访问server1(192.168.1.3)
总结
nat还有一种静态配置,即一个公网ip映射一个私网ip。
nat端口映射可以实现一个公网ip映射多个私网ip,只需要控制端口号的选择。
发表评论