3

我在我的 ubuntu 中拥有的是:

eth0(local) = 192.168.1.1/24 attemp to be gateway for local network
eth1(net1) = 192.168.2.2/24 gateway is 192.168.2.1 is a DSL 1
eth2(net2) = 192.168.3.2/24 gateway is 192.168.3.1 is a DSL 2

我想要的是:

port 22,53,80,443 force to use only through eth1
port 6881 to 6889 and other ports force to use only through eth2

如何在 iptables 中制定规则?

谢谢你。

4

2 回答 2

2

我需要在我的树莓派 3 型号 b 上转发到另一个 IP 地址,这就是我完成它的方式。

sudo vi /etc/sysctl.conf

并取消注释该行

net.ipv4.ip_forward=1

重新加载 sysctl 或重启你的树莓派

sudo sysctl -p /etc/sysctl.conf

然后运行以下 iptables 命令

iptables -t nat -A PREROUTING -i eth1-p tcp --dport 22 -j DNAT --to-destination 192.168.0.198:22
iptables -t nat -A PREROUTING -i eth1-p tcp --dport 53 -j DNAT --to-destination 192.168.0.198:53
iptables -t nat -A PREROUTING -i eth1-p tcp --dport 80 -j DNAT --to-destination 192.168.0.198:80
iptables -t nat -A PREROUTING -i eth1-p tcp --dport 443 -j DNAT --to-destination 192.168.0.198:443

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
于 2016-07-22T06:23:06.473 回答
1

标记应该通过的包eth1

iptables -A PREROUTING -i eth0 -t mangle -p tcp --dports 22,53,80,443 -j MARK --set-mark 1

添加规则eth1.out以路由标记的包裹:

echo "201 eth1.out" >> /etc/iproute2/rt_tables
ip rule add fwmark 1 table eth1.out

通过以下方式路由所有标记的包裹eth1

/sbin/ip route add default via 192.168.2.1 dev eth1 table eth1.out

通过以下方式路由其他所有内容eth2

/sbin/ip route add default via 192.168.3.1 dev eth2 

如果MARK规则不起作用,请尝试使用CONNMARK.

于 2011-03-08T11:05:37.153 回答