【问题标题】:iptables forward specific port to specific niciptables 将特定端口转发到特定网卡
【发布时间】:2016-07-22 14:23:06
【问题描述】:

我在我的 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中制定规则?

谢谢。

【问题讨论】:

    标签: iptables


    【解决方案1】:

    我需要在我的树莓派 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
    

    【讨论】:

      【解决方案2】:

      标记应该通过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

      【讨论】:

      • 在这里不起作用是我的 ip route 命令
        192.168.3.0/24 dev eth2 proto kernel scope link src 192.168.3.2 192.168.2.0/24 dev eth1 proto kernel scope link src 192.168。 2.2 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.1 default via 192.168.3.1 dev eth2 default via 192.168.3.1 dev eth2 metric 100 default via 192.168.2.1 dev eth1 metric 100
      • 尝试删除 192.168.0.2/24 路由​​和 192.168.0.3/24 之一(其中有两个,默认指标和指标 100)
      • 如何将 /proc/sys/net/ipv4/ip_forward 设置为 0 或 1
      • 是的,它肯定必须设置为 1,因为您正在运行网关
      • 第二条规则有效,所有通过 eth2 出去的东西都可以正常工作,购买标记的规则无效。(我删除了所有指标为 100 的网关)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 2016-01-17
      • 2016-03-28
      • 2018-02-07
      相关资源
      最近更新 更多