2011-03-08 46 views
3

什麼我在我的Ubuntu是:iptables的轉發特定端口到特定的網卡

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的?

謝謝。

應通過 eth1

回答

1

馬克包:通過eth1

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

路線都標包:

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

路線一切

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

添加規則eth1.out路由標記的包其他通過eth2

/sbin/ip route add default via 192.168.3.1 dev eth2 

如果MARK規則將不能正常工作,請嘗試使用CONNMARK

+0

沒有在這裏工作是我的ip route命令
192.168.3.0/24 dev eth2 proto kernel scope鏈接src 192.168.3.2 192.168.2.0/24 dev eth1 proto kernel scope鏈接src 192.168.2.2 192.168.1.0/24 dev eth0 proto kernel scope鏈接src 192.168.1.1 default通過192.168.3.1 dev eth2 默認通過192.168.3.1 dev eth2 metric 100 默認通過192.168.2.1 dev eth1 metric 100 – njai 2011-03-08 12:15:11

+0

嘗試刪除192.168.0.2/24路由和192.168.0.3/24之一(其中有兩個,與默認度量和度量100) – 2011-03-08 13:06:39

+0

如何將/ proc/sys/net/ipv4/ip_forward設置爲0或1 – njai 2011-03-08 15:54:20

1

我需要在我的覆盆子pi 3模型b端口轉發到另一個IP地址,這就是我完成它的方式。

sudo vi /etc/sysctl.conf 

和uncommment行

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