我在私有子网中的
10.0.3.22
有一个 EC2 实例。
我在公共子网中有另一个 EC2 实例,我试图将其用作 NAT 实例(以节省 AWS 提供的 NAT 网关的成本)。在大多数情况下,我都遵循了本指南。
NAT 实例具有以下用户数据,使用 here.
记录的 mime 多部分格式Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#! /bin/bash
yum install iptables-services -y
systemctl enable iptables
systemctl start iptables
sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
--//--
我可以通过 SSH 连接到 NAT 实例,并确认我可以从那里访问互联网(使用
ping ietf.org
)。从那个 SSH 会话,我可以通过 SSH 连接到位于 10.0.3.22
的私有子网中的实例,但是从这里我无法访问互联网:
[ec2-user@ip-10-0-3-22 ~]$ ping ietf.org
PING ietf.org (50.223.129.194) 56(84) bytes of data.
From ip-10-0-4-248.eu-west-1.compute.internal (10.0.4.248) icmp_seq=1 Destination Host Prohibited
为什么这行不通?
私有子网的路由表(其中 eni-0a3... 是 NAT EC2 实例的网络接口):
以及包含 NAT EC2 实例的公共子网的路由表:
我还禁用了 NAT EC2 实例上的源/目标检查。