AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 720207
Accepted
Oli
Oli
Asked: 2016-01-13 05:14:51 +0800 CST2016-01-13 05:14:51 +0800 CST 2016-01-13 05:14:51 +0800 CST

桥接接口之间的端口转发

  • 772

所以我有一堆与我的主要以太网设备绑定的桥接接口(em1怪HP)。它们为我在服务器上运行的各种 LXC 容器提供服务,并且可以轻松地让我从网络上的其他物理设备访问它们。

name    id                  STP   interfaces    IP
br0     8000.989096db8b8a   no    em1           10.10.0.2
                                  veth236T4V    10.10.0.15
                                  veth269GNR    10.10.0.16
                                  vethBYBC0Y    10.10.0.17

这些都从主网络 DHCP(分配静态租约)获取 IP。

我想将已在主主机(、、端口 9000、9001)上运行的服务移动em1到10.10.0.2第一个 LXC 容器。我已经这样做了,现在可以通过它访问东西10.10.0.15:9000-9001,但网络上的所有其他东西都希望看到它10.10.0.2:9000-9001。

传统的端口转发iptables似乎不起作用。我试过了:

-A PREROUTING -i em1 -p tcp --dport 9000 -j DNAT --to 10.10.0.15:9000
-A PREROUTING -i em1 -p tcp --dport 9001 -j DNAT --to 10.10.0.15:9001

我已经尝试过br0,em1但都没有工作。

在凌晨 3 点的冰雹研究中,我发现了大量的东西表明我需要ebtables,但我以前从未听说过。问题的一半似乎是大多数人使用lxcbrN带有 LXC 的设备,但我需要外部 IP。我不确定我需要什么。ebtables文档似乎将“端口”一词定义为其他内容,这无济于事。

我已经超出了我的深度。我再也感觉不到地板了,我开始踩水了。任何人都可以告诉我一条线并确定我需要在桥接接口之间重定向几个端口吗?

iptables
  • 1 1 个回答
  • 7315 Views

1 个回答

  • Voted
  1. Best Answer
    Doug Smythies
    2016-01-15T10:08:32+08:002016-01-15T10:08:32+08:00

    你可以使用iptables。以下是建议解决方案的脚本版本。我不知道您可能已经拥有哪些 iptables 规则,因此可能需要进行一些合并工作。

    #!/bin/sh
    FWVER=0.02
    #
    # test-oli rule set 2016.01.14 Ver:0.02
    #     Having tested this on my test server using port 80,
    #     convert for what Oli actually wants (which I can not test).
    #
    # test-oli rule set 2016.01.14 Ver:0.01
    #     Port forward when this computer has one nic and
    #     is not a router / gateway.
    #     In this case the destination is a guest VM on this
    #     host but, with bridged networking and all IP addresses
    #     from the main LAN, that should not be relevant.
    #
    #     This script may conflict with other iptables rules on the
    #     host, I don't know. On my test server, clobbering the existing
    #     iptables rules is O.K. because I do not use the virbr0 stuff,
    #     nor the default virtual network,  anyhow.
    #
    #     References:
    #     http://askubuntu.com/questions/720207/port-forwarding-between-bridged-interfaces
    #     http://ubuntuforums.org/showthread.php?t=1855192
    #     http://www.linuxquestions.org/questions/linux-networking-3/iptables-forwarding-with-one-nic-80009/
    #
    #     run as sudo
    #
    echo "test-oli rule set version $FWVER..\n"
    
    # The location of the iptables program
    #
    IPTABLES=/sbin/iptables
    
    # Setting the EXTERNAL and INTERNAL interfaces and addresses for the network
    # Use br0 instead of eth0. While using eth0 seems to work fine, the packet counters
    # don't work, so debugging information is better and more complete using br0.
    #
    #
    INTIF="br0"
    INTIP="10.10.0.2"
    FORIP="10.10.0.15"
    UNIVERSE="0.0.0.0/0"
    
    echo " Internal Interface: $INTIF  Internal IP: $INTIP  Forward IP $FORIP"
    
    # CRITICAL:  Enable IP forwarding since it is disabled by default
    #
    echo Enabling forwarding...
    echo "1" > /proc/sys/net/ipv4/ip_forward
    
    # Clearing any previous configuration
    #
    echo " Clearing any existing rules and setting default policy to ACCEPT.."
    $IPTABLES -P INPUT ACCEPT
    $IPTABLES -F INPUT
    $IPTABLES -P OUTPUT ACCEPT
    $IPTABLES -F OUTPUT
    $IPTABLES -P FORWARD ACCEPT
    $IPTABLES -F FORWARD
    $IPTABLES -t nat -F
    # Delete user defined chains
    $IPTABLES -X
    # Reset all IPTABLES counters
    $IPTABLES -Z
    # While my references do not have it, I think this is needed.
    $IPTABLES -t nat -Z
    
    # First we change the destination of any incoming port 80 traffic
    #
    $IPTABLES -t nat -A PREROUTING -p tcp -i $INTIF --dport 9000 -j DNAT --to-destination $FORIP:9000
    $IPTABLES -t nat -A PREROUTING -p tcp -i $INTIF --dport 9001 -j DNAT --to-destination $FORIP:9001
    
    # And then we do the actual forward
    # FORWARD rules would only be needed if the default policy is not ACCEPT
    # (Shown here for completeness)
    #
    $IPTABLES -A FORWARD -p tcp -i $INTIF -d $FORIP --dport 9000 -j ACCEPT
    $IPTABLES -A FORWARD -p tcp -i $INTIF -d $FORIP --dport 9001 -j ACCEPT
    
    # Now, we need to change the source address, otherwise the reply packets
    # would be sent directly to the client, causing confusion.
    $IPTABLES -t nat -A POSTROUTING -o $INTIF -j SNAT --to-source $INTIP
    
    echo "test-oli rule set version $FWVER done."
    
    • 6

相关问题

  • iptables 规则强制所有浏览器使用代理

  • iptables 重定向的奇怪问题

  • iptables 允许动态域名和自动更新规则

  • ubuntu 上的 iptables 和 nmap

  • iptables 的图形用户界面?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve