思科CCNA认证之 NAT 子网地址转换原理与配置详解

本文讲述了思科CCNA认证之 NAT 子网地址转换。分享给大家供大家参考,具体如下:

NAT 网络地址转换

全称:Network Address Translation

为什么需要NAT?

因为公网IP(IPv4)地址紧缺,内容地址通过NAT转换成一个公有地址去访问公网资源;

如下图展示:
思科CCNA认证之 NAT 子网地址转换原理与配置详解

私有地址列表

  • A类: 10.0.0.0/8
  • B类: 172.16.0.0/16
  • C类: 192.168.1.0/24
名词 解释
内部本地 转换之前内部原地址的名字
外部本地 转换之前目标主机的名字
内部全局 转换之后内部主机的名字
外部全局 转换之后外部目标主机的名字

思科CCNA认证之 NAT 子网地址转换原理与配置详解

NAT的三种类型

1、静态NAT

内部本地地址与内部全局地址进行一对一的明确转换。这种方法主要用在内部网络中有对外提供服务的服务器,如WEBMAIL服务器时。该方法的缺点是需要占独宝贵的合法IP地址。即如果某个合法IP地址已经被NAT静态地址转换定义,即使该地址当前没有被使用,也不能被用作其他的地址转换。

配置:

 

Router(config)#ip nat inside source static local-ip globl-ip

  • Establishes static translation between an inside local address and an inside global address

     

Router(config)#ip nat inside

  • Marks the interface as connected to the inside

     

Router(config)#ip nat outside

  • Marks the interface as connected to the outside

     

Router(config)#show ip nat translations

  • Displays active translations

     

案例:
如下图:
思科CCNA认证之 NAT 子网地址转换原理与配置详解

2、动态NAT

动态地址转换也是将内部本地地址与内部全局地址进行一对一的转换。但是是从内部全局地址池中动态的选择一个未使用的地址对内部本地地址进行转换。该地址是由未被使用的地址组成的地址池中在定义时排在最前面的一个当数据传输完毕后,路由器将把使用完的内部全局地址放回到地址池中,以供其他内部本地地址进行转换。但是在改地址被使用时,不能用改地址再进行一次转换。

配置:

Router(config)#ip nat poll name start-ip end-ip {netmask netmask | prefix-length prefix-length}

  • Defines a pool of global addresses to be allocated as needed

     

Router(config)#access-list access-list-number permit source [source-wildcard]

  • Defines a standard IP ACL permitting those inside local addresses that are to be translated

     

Router(config)#ip nat inside source list access-list-number pool name

  • Establishes dynameic source translation, specifying the ACL that was defined in the previous step

     

Router(config)#show ip nat translations

  • Displays active translations

案例:
思科CCNA认证之 NAT 子网地址转换原理与配置详解

3、端口复用(PAT)

  • 服用地址转换也称为端口地址转换(Port Address Translation, PAT),首先是一种动态地址转换,路由器将通过几率地址、应用程序端口等唯一标示一个转换。通过这种转换,可以使多个内部本地地址同时与同一个内部全局地址进行转换并对外部网络进行访问。很多只申请到少量IP地址甚至只有一个合法IP、地址,却京城有很多用户同时要求上午的情况,这转换方式非常有用;
  • 理想状态下,一个单一的IP地址可以使用的端口为4000个

配置:

Router(config)#access-list access-list-number permit source source-wildcard

  • Defines a standard IP ACL that will permit the inside local addresses that are to be translated

     

Router(config)#ip nat inside source list access-list-number interface interface overload

  • Establishes dynameic source translation, specifying the ACL that was defined in the previous step

     

Router(config)#show ip nat translations

  • Displays active translations

案例:
思科CCNA认证之 NAT 子网地址转换原理与配置详解

NAT试验

按照下图中的网络拓扑进行配置:
思科CCNA认证之 NAT 子网地址转换原理与配置详解

同样server和PC均使用路由来代替,需要一下配置:

no ip routing
ip default-gateway xxx.xxx.xxx.xxx

注:如果没有配置no ip routing,则默认网关的配置不生效;

下面进行配置:

Server配置:

R2>en
R2#conf t
R2(config)#hostname Server
Server(config)#int f 0/0
Server(config-if)#ip add 192.168.1.100 255.255.255.0
Server(config-if)#no sh
Server(config-if)#exit
Server(config)#no ip routing
Server(config)#ip default-gateway 192.168.1.254
Server(config)#line vty 0 4
Server(config-line)#pass
Server(config-line)#password sijiayong

服务器配置了vty,等下需要测试telnet连接;

PC配置:

R1>en
R1#conf t
R1(config)#hostname PC
PC(config)#int f 0/0
PC(config-if)#ip add 192.168.1.1 255.255.255.0
PC(config-if)#no sh
PC(config-if)#exit
PC(config)#no ip routing
PC(config)#ip default-gateway 192.168.1.254

路由配置:

R3>en
R3#conf t
Router(config)#hostname Gateway
Gateway(config)#int f 0/0
Gateway(config-if)#ip add 192.168.1.254 255.255.255.0
Gateway(config-if)#no sh
Gateway(config-if)#exit
Gateway(config)#int s 1/0
Gateway(config-if)#ip add 10.0.0.1 255.255.255.0
Gateway(config-if)#no sh
Gateway(config-if)#exit
Gateway(config)#ip route 0.0.0.0 0.0.0.0 10.0.0.2
Gateway(config)#access-list 1 permit 192.168.1.0 0.0.0.255
Gateway(config)#ip nat inside source list 1 interface s1/0 overload
Gateway(config)#int f 0/0
Gateway(config-if)#ip nat inside
Gateway(config-if)#int s 1/0
Gateway(config-if)#ip nat outside
Gateway(config-if)#end

模拟外网配置:

R4>en
R4#conf t
R4(config)#hostname Internet
Internet(config)#int s 0/0
Internet(config-if)#ip add 10.0.0.2 255.255.255.0
Internet(config-if)#no sh

这样配置,服务器和PC就都可以访问外网地址10.0.0.2

测试:

PC#ping 10.0.0.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 10.0.0.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 4/25/76 ms


Server#ping 10.0.0.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 10.0.0.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 8/15/40 ms

验证通过;

实验要求2:

要求公网能够通过10.0.0.3的公网地址,连接到服务器Server,也就是能够telnet连接到服务器;

这样只需要配置路由器,吧10.0.0.3映射到192.168.1.100地址上即可;
配置如下:

Gateway(config)#ip nat inside source static 192.168.1.100 10.0.0.3

这个映射的配置,就可以了,通过10.0.0.1的公网进行测试pingtelnet

Internet#ping 10.0.0.3
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 10.0.0.3, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 8/27/80 ms
Internet#telnet 10.0.0.3
Trying 10.0.0.3 ... Open
User Access Verification
Password:
Server>exit
[Connection to 10.0.0.3 closed by foreign host]

测试通过;

但同时发现有个危险,这种配置,是把Server服务器整个暴露在公网上,存在安全隐患;

常规工作中,一般只需要对公网暴露一个或者两个端口,一般都是Http80端口和SSH22端口,这样更能够保障服务器的安全;

本次以开放telnet23端口进行测试;
首先要删除掉当前的nat配置,然后在进行端口映射;
配置如下:

Gateway(config)#no ip nat inside source static 192.168.1.100 10.0.0.3
Gateway(config)#ip nat inside source static tcp 192.168.1.100 23 10.0.0.3 8080

这里对外暴露的是8080端口,映射到内网地址的192.168.1.10023端口上,下面进行telnet测试。

Internet#telnet 10.0.0.3 8080
Trying 10.0.0.3, 8080 ... Open
User Access Verification
Password:
Server>exit
[Connection to 10.0.0.3 closed by foreign host]

测试通过,至此,配置完成;