1、NAT的目的
由于IPV4地址短缺,为解决公网 IP 地址短缺的问题,所以诞生了NAT技术,来重写IP数据包源IP或者目的IP。
2、NAT分类
静态 NAT,即内网 IP 与公网 IP 是一对一的永久映射关系;
动态 NAT,即内网 IP 从公网 IP 池中,动态选择一个进行映射;
网络地址端口转换 NAPT(Network Address and Port Translation),即把内网 IP 映射到公网 IP 的不同端口上,让多个内网 IP 可以共享同一个公网 IP 地址。
3、NAPT分类
源地址转换SNAT,即目的地址不变,只替换源 IP 或源端口。SNAT 主要用于,多个内网 IP 共享同一个公网 IP ,来访问外网资源的场景。
目的地址转换 DNAT,即源 IP 保持不变,只替换目的 IP 或者目的端口。DNAT 主要通过公网 IP 的不同端口号,来访问内网的多种服务,同时会隐藏后端服务器的真实 IP 地址。
双向地址转换,即同时使用 SNAT 和 DNAT。当接收到网络包时,执行 DNAT,把目的 IP 转换为内网 IP;而在发送网络包时,执行 SNAT,把源 IP 替换为外部 IP。
IPtables 是linux常用的一种配置工具,我们可以man iptables,可以发现table,用来管理链规则的,其中包含5个表,filter(过滤),nat(转换)、mangle(修改分组数据)、raw(原始数据包)、security(安全)
Filter表中,内置INPUT、OUTPUT和FORWARD链
-t, --table table
This option specifies the packet matching table which the command should operate on. If
the kernel is configured with automatic module loading, an attempt will be made to load the
appropriate module for that table if it is not already there.
The tables are as follows:
filter:
This is the default table (if no -t option is passed). It contains the built-in chains
INPUT (for packets destined to local sockets), FORWARD (for packets being routed
through the box), and OUTPUT (for locally-generated packets).
nat:
This table is consulted when a packet that creates a new connection is encountered. It
consists of four built-ins: PREROUTING (for altering packets as soon as they come in),
INPUT (for altering packets destined for local sockets), OUTPUT (for altering locally-
generated packets before routing), and POSTROUTING (for altering packets as they are
about to go out). IPv6 NAT support is availab