iptables代理上网

除 iptables 外,也可以使用 nginx 代理也可上网 能上网的那台机器: 10.209.204.31 不能上网的机器: 10.209.204.32

除 iptables 外,也可以使用 nginx 代理也可上网

能上网的那台机器: 10.209.204.31
不能上网的机器: 10.209.204.32 10.209.204.33

能上网的机器

1.因为我的网卡地址 是 10.209.204.31 ,所以 snat 地址就写这个,如果网卡地址显示的是公网,snat 地址就写公网
2.-s 后面不要加网段,会导致广播风暴,因为网段内也包含了 10.209.204.31 这个地址

 iptables -t nat -A POSTROUTING -s 10.209.204.32 -j SNAT --to-source 10.209.204.31iptables -t nat -A POSTROUTING -s 10.209.204.33 -j SNAT --to-source 10.209.204.31

开启数据包转发

echo 1 > /proc/sys/net/ipv4/ip_forward

不能上网的机器

[root@nginx1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.209.204.1    0.0.0.0         UG    0      0        0 ens160       //默认路由
10.209.204.0    0.0.0.0         255.255.255.0   U     100    0        0 ens160       //广播地址,不能删route add -net 10.209.204.0/24 gw 10.209.204.1 dev ens160
route add -net default gw 10.209.204.31 dev ens160       //新增默认路由(网关是能上网机器的ip)route del -net default gw 10.209.204.1 dev ens160        //删除以前的默认路由[root@nginx1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.209.204.31   0.0.0.0         UG    0      0        0 ens160
10.209.204.0    10.209.204.1    255.255.255.0   UG    0      0        0 ens160
10.209.204.0    0.0.0.0         255.255.255.0   U     100    0        0 ens160

¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
有问题评论区回复,或者私信
¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥