Skip to content

Nat 网关

概述

iptablesnat 表用于网络地址转换,是实现网络访问控制、端口映射、NAT 网关的基础。

nat网关类型说明
公网 nat 网关共享上网,端口映射
VPC nat 网关VPC 互联(内网互通)

环境准备

准备两台 没有公网 IP 的实例,无法访问外网。

阿里云

shell
# 没有公网 IP,无法连接外网
root@iZbp16cy5jtgpriherlshrZ:~# ping jd.com
PING jd.com (211.144.27.126) 56(84) bytes of data.

创建公网 NAT 网关测试

部署 NAT 网关后,实例可以访问公网。

阿里云

阿里云

shell
# 开启公网 NAT 网关后,可以连接外网
root@iZbp16cy5jtgpriherlshrZ:~# ping baidu.com
PING baidu.com (39.156.66.10) 56(84) bytes of data.
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=1 ttl=53 time=35.8 ms
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=2 ttl=53 time=35.7 ms
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=3 ttl=53 time=35.6 ms
^C
--- baidu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 35.633/35.713/35.826/0.082 ms

# 查看公网 IP
root@iZbp16cy5jtgpriherlshrZ:~# curl ifconfig.io
114.215.255.206

通过 NAT 网关可以实现 无公网 IP 实例访问公网,也可以进行 端口映射共享上网 等网络策略,非常适合私有网络场景下的公网访问需求。

感谢阅读,欢迎交流!