如何配置自定义 NAT 以在 Amazon VPC 中使用

jjm*_*tes 15 nat iptables amazon-ec2

我有一个我希望用作 NAT 实例的 Ubuntu 机器(除其他外)。我宁愿避免使用 Amazon 提供的 NAT AMI,而是自己配置 NAT。

目前,我的主机只有一个网络接口(如http://docs.amazonwebservices.com/AmazonVPC/latest/UserGuide/VPC_NAT_Instance.html 中所示)。

我是否可以将我的 Ubuntu 主机配置为 Amazon 网络中其他主机的 NAT 实例?

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination         
       5      454 MASQUERADE  all  --  *      eth0    0.0.0.0/0            0.0.0.0/0  
Run Code Online (Sandbox Code Playgroud)

我尝试在 Ubuntu 主机 (10.200.0.51) 中配置 NAT 规则。我的第二台主机位于不同的网络 (10.200.10.41/24) 上。所以我写道:

route add -net 10.200.0.0 netmask 255.255.255.0 dev eth0 # So I can reach 10.200.0.51
route add default gw 10.200.0.51
Run Code Online (Sandbox Code Playgroud)

但是机器失去了连接。

我对亚马逊中 NAT 实例和路由的使用有什么误解?

Mar*_*tin 22

您可以检查 Amazon 的脚本以在 Linux 机器上配置 NAT,它带有默认的 ami-vpc-nat AMI,在 /usr/local/sbin/configure-pat.sh

它看起来像这样:

#!/bin/bash
# Configure the instance to run as a Port Address Translator (PAT) to provide 
# Internet connectivity to private instances. 

function log { logger -t "vpc" -- $1; }

function die {
    [ -n "$1" ] && log "$1"
    log "Configuration of PAT failed!"
    exit 1
}

# Sanitize PATH
PATH="/usr/sbin:/sbin:/usr/bin:/bin"

log "Determining the MAC address on eth0..."
ETH0_MAC=$(cat /sys/class/net/eth0/address) ||
    die "Unable to determine MAC address on eth0."
log "Found MAC ${ETH0_MAC} for eth0."

VPC_CIDR_URI="http://169.254.169.254/latest/meta-data/network/interfaces/macs/${ETH0_MAC}/vpc-ipv4-cidr-block"
log "Metadata location for vpc ipv4 range: ${VPC_CIDR_URI}"

VPC_CIDR_RANGE=$(curl --retry 3 --silent --fail ${VPC_CIDR_URI})
if [ $? -ne 0 ]; then
   log "Unable to retrive VPC CIDR range from meta-data, using 0.0.0.0/0 instead. PAT may be insecure!"
   VPC_CIDR_RANGE="0.0.0.0/0"
else
   log "Retrieved VPC CIDR range ${VPC_CIDR_RANGE} from meta-data."
fi

log "Enabling PAT..."
sysctl -q -w net.ipv4.ip_forward=1 net.ipv4.conf.eth0.send_redirects=0 && (
   iptables -t nat -C POSTROUTING -o eth0 -s ${VPC_CIDR_RANGE} -j MASQUERADE 2> /dev/null ||
   iptables -t nat -A POSTROUTING -o eth0 -s ${VPC_CIDR_RANGE} -j MASQUERADE ) ||
       die

sysctl net.ipv4.ip_forward net.ipv4.conf.eth0.send_redirects | log
iptables -n -t nat -L POSTROUTING | log

log "Configuration of PAT complete."
exit 0
Run Code Online (Sandbox Code Playgroud)


jjm*_*tes 18

我已经安装了 Amazon NAT AMI 并检查了相关配置:

[root@ip-10-200-0-172 ec2-user]# iptables -L -n -v -x -t nat
Chain PREROUTING(策略接受11个数据包,660字节)
    pkts 字节目标 prot 选择退出源目标         

Chain INPUT(策略接受11个数据包,660字节)
    pkts 字节目标 prot 选择退出源目标         

Chain OUTPUT(策略接受357个数据包,24057字节)
    pkts 字节目标 prot 选择退出源目标         

链POSTROUTING(策略接受0个数据包,0个字节)
    pkts 字节目标 prot 选择退出源目标         
     357 24057 全部伪装——* eth0 10.200.0.0/16 0.0.0.0/0

[root@ip-10-200-0-172 ec2-user]# cat /proc/sys/net/ipv4/ip_forward 
1

另外机器需要有公网IP,需要禁用Sourc/Dest检查。

这台机器然后可以用作 NAT 实例。

其他主机的路由在 EC2 级别配置(使用“路由表”功能)。


Vic*_*rov 5

几乎没有任何说明对我有帮助。

笔记:

  • 10.0.0.23 - 实例的私有 IP,我决定将其设为“nat-i​​nstance”,该实例具有 EIP。
  • 10.0.0.0/24 - vpc 子网

在“nat-i​​nstance”上,作为 root 用户:

sysctl -q -w net.ipv4.ip_forward=1 net.ipv4.conf.eth0.send_redirects=0
iptables -t nat -C POSTROUTING -o eth0 -s 10.0.0.0/24 -j MASQUERADE 2> /dev/null || iptables -t nat -A POSTROUTING -o eth0 -s 10.0.0.0/24 -j MASQUERADE
Run Code Online (Sandbox Code Playgroud)

在这之后:

[sysctl file]
net.ipv4.ip_forward = 1
net.ipv4.conf.eth0.send_redirects = 0

[iptables]
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  10.0.0.0/24          0.0.0.0/0 
Run Code Online (Sandbox Code Playgroud)

通过 AWS 控制台:

Grant all traffic from 10.0.0.0/24 (into security groups)
Set disabled source/dest. check (right click on "nat" instance)
Run Code Online (Sandbox Code Playgroud)

在其他没有EIP的情况下:

sudo route add default gw 10.0.0.23
Run Code Online (Sandbox Code Playgroud)

UPD:我发现,我的 VPC 中的每个新实例在 ping 默认网关后都能正确检测到互联网。

所以:

[ec2-user@ip-10-0-0-6 ~]$ ping google.com
PING google.com (173.194.33.71) 56(84) bytes of data.
^C
--- google.com ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2356ms

[ec2-user@ip-10-0-0-6 ~]$ ping 10.0.0.23
PING 10.0.0.230 (10.0.0.23) 56(84) bytes of data.
64 bytes from 10.0.0.23: icmp_seq=1 ttl=64 time=1.22 ms
64 bytes from 10.0.0.23: icmp_seq=2 ttl=64 time=0.539 ms
64 bytes from 10.0.0.23: icmp_seq=3 ttl=64 time=0.539 ms
^C
--- 10.0.0.23 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2500ms
rtt min/avg/max/mdev = 0.539/0.768/1.228/0.326 ms
[ec2-user@ip-10-0-0-6 ~]$ ping google.com
PING google.com (173.194.33.70) 56(84) bytes of data.
64 bytes from sea09s15-in-f6.1e100.net (173.194.33.70): icmp_seq=1 ttl=55 time=13.5 ms
64 bytes from sea09s15-in-f6.1e100.net (173.194.33.70): icmp_seq=2 ttl=55 time=14.2 ms
Run Code Online (Sandbox Code Playgroud)

我知道,这不是问题,但可以节省一些时间