什么是NAT
nat是通过解析ip报文头部,替换报文头部的源地址或目的地址,实现私网访问公网
NAT优缺点
nat优点
①缓解公网地址紧缺
②解决ip地址空间冲突或重叠
③增加网络安全性
nat缺点
①存在转发延迟
②端到端寻址变得困难
③nat产生的表项占用设备内存空间
NAT类型
静态nat
私有地址对公有地址的一对一映射
动态nat
基于地址池的一对一映射
napt
允许多个私有地址映射到同个公有地址的不同端口
easyip
允许多个私有地址映射到网关出接口的不同端口
nat服务器
可以使外网用户访问内网服务器
实验
首先将sw1上的端口状态改一下,接主机的改为access,接交换机改为trunk
#
interface GigabitEthernet0/0/1
port link-type access
port default vlan 20
#
interface GigabitEthernet0/0/2
port link-type access
port default vlan 10
#
interface GigabitEthernet0/0/3
port link-type trunk
port trunk allow-pass vlan 2 to 4094
#
然后在sw2上配置接口状态和在vlan接口配置网关地址
#
interface GigabitEthernet0/0/1
port link-type trunk
port trunk allow-pass vlan 2 to 4094
#
interface GigabitEthernet0/0/2
port link-type access
port default vlan 100
#
interface Vlanif1
#
interface Vlanif10
ip address 192.168.10.254 255.255.255.0
#
interface Vlanif20
ip address 192.168.20.254 255.255.255.0
#
interface Vlanif100
ip address 192.168.30.1 255.255.255.0
#
在R1上配置ip地址,然后配置一个动态nat地址转换,然后配置一个nat服务器
#
acl number 2000
rule 5 permit source 192.168.0.0 0.0.255.255
#
nat address-group 1 12.1.1.20 12.1.1.30
#
interface GigabitEthernet0/0/0
ip address 192.168.30.2 255.255.255.0
#
interface GigabitEthernet0/0/1
ip address 12.1.1.1 255.255.255.0
nat server protocol tcp global 12.1.1.100 www inside 192.168.20.1 www
nat outbound 2000 address-group 1 no-pat
#
在R2上只需要配置地址
#
interface GigabitEthernet0/0/0
ip address 12.1.1.2 255.255.255.0
#
interface GigabitEthernet0/0/1
ip address 100.1.1.2 255.255.255.0
#
然后在sw2,R1,R2上配置静态路由
#
ip route-static 0.0.0.0 0.0.0.0 192.168.30.2
[sw2]dis ip ro
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 9 Routes : 9
Destination/Mask Proto Pre Cost Flags NextHop Interface
0.0.0.0/0 Static 60 0 RD 192.168.30.2 Vlanif100
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.10.0/24 Direct 0 0 D 192.168.10.254 Vlanif10
192.168.10.254/32 Direct 0 0 D 127.0.0.1 Vlanif10
192.168.20.0/24 Direct 0 0 D 192.168.20.254 Vlanif20
192.168.20.254/32 Direct 0 0 D 127.0.0.1 Vlanif20
192.168.30.0/24 Direct 0 0 D 192.168.30.1 Vlanif100
192.168.30.1/32 Direct 0 0 D 127.0.0.1 Vlanif100
[sw2]
#
ip route-static 0.0.0.0 0.0.0.0 12.1.1.2
ip route-static 192.168.0.0 255.255.0.0 192.168.30.1
#
[R1]dis ip ro
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 24 Routes : 24
Destination/Mask Proto Pre Cost Flags NextHop Interface
0.0.0.0/0 Static 60 0 RD 12.1.1.2 GigabitEthernet
0/0/1
12.1.1.0/24 Direct 0 0 D 12.1.1.1 GigabitEthernet
0/0/1
12.1.1.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
12.1.1.20/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.21/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.22/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.23/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.24/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.25/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.26/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.27/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.28/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.29/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.30/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.100/32 Unr 64 0 D 127.0.0.1 InLoopBack0
12.1.1.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/1
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
192.168.0.0/16 Static 60 0 RD 192.168.30.1 GigabitEthernet
0/0/0
192.168.30.0/24 Direct 0 0 D 192.168.30.2 GigabitEthernet
0/0/0
192.168.30.2/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
192.168.30.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet
0/0/0
255.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0
此时pc1能ping通R2的g0/0/0,然后抓包,发现源ip为nat地址池里的ip地址
pc能ping通内网的sever服务器
外网pc能够访问websever服务器
实验总结:nat命令写在网关的出接口上配置,且不知道通往外网的路由,所以需要写一个默认路由使数据包默认转发。