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
    • 最新
    • 标签
主页 / server / 问题 / 574150
Accepted
Harun Baris Bulut
Harun Baris Bulut
Asked: 2014-02-09 04:58:58 +0800 CST2014-02-09 04:58:58 +0800 CST 2014-02-09 04:58:58 +0800 CST

将传入流量转发到本地侦听器

  • 772

我有一个在 localhost 上运行的服务,它监听端口 localhost:10000

我想要做的是将所有进入 publicip:15000 的流量转发到 localhost:10000 ,其中更改服务配置不可用。

服务仅侦听本地主机,但流量来自外部。

顺便说一句,服务在linux上运行。

编辑; 我尝试像这样添加 NAT 规则,但我无法成功。

配置 NAT 我做了;

iptables --flush
iptables --table nat --flush
iptables --delete-chain
iptables --table nat --delete-chain
iptables --table nat --append POSTROUTING --out-interface lo -j MASQUERADE
iptables -A FORWARD -i eth0 -o lo -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i lo -o eth0 -j ACCEPT
echo 1 > /proc/sys/net/ipv4/ip_forward
service ufw restart

并开始路由执行这个;

iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 15000 -j DNAT --to 127.0.0.1:10000

你觉得我错过了什么?

提前致谢

巴里斯

networking
  • 1 1 个回答
  • 2579 Views

1 个回答

  • Voted
  1. Best Answer
    krisFR
    2014-02-09T12:08:45+08:002014-02-09T12:08:45+08:00

    环回流量的 DNAT 似乎是不可能的。

    环回流量跳过 PREROUTING 和 OUTPUT 链。

    RFC 5735(第 3 页)说网络127.0.0.0/8无法在主机本身之外路由:

    127.0.0.0/8 - 此块被分配用作 Internet 主机环回地址。由更高级别协议发送到该块内任何地址的数据报在主机内部循环返回。这通常仅使用 127.0.0.1/32 进行环回来实现。如 [RFC1122] 第 3.2.1.3 节所述,整个 127.0.0.0/8 块内的地址不会合法地出现在任何地方的任何网络上。


    此外,流量loopback interface被视为Martian Packets:

    这些数据包实际上不能像声称的那样起源,也不能被传递

    • http://en.wikipedia.org/wiki/Martian_packet

    解决方法:

    一个简单的替代方法是inted在服务器端使用服务器及其redirect 功能。

    这样,您可以在inetd其中定义 un 服务并设置该服务将侦听的端口。然后,设置redirect指令将此端口绑定到127.0.0.1:port.

    在下面的示例中,我将使用xinetd(在 Ubuntu 12.04 LTS 上)绑定mysql运行在以下服务器上的服务器127.0.0.1:10000:

    第 1 步:安装软件包:apt-get install xinetd

    第二步:编辑配置文件/etc/xinetd.conf

    添加类似于以下的服务定义:

    service my_redirector
    {
     type = UNLISTED
     disable = no
     socket_type = stream
     protocol = tcp
     user = root
     wait = no
     port = 15000
     redirect = 127.0.0.1 10000
     log_type = FILE /tmp/somefile.log
    }
    

    第 3 步:重新启动xinetd守护进程:service xinetd restart

    第 4 步:让我们检查端口上的侦听器15000:

    # netstat -anp | grep 15000
    tcp     0      0 0.0.0.0:15000      0.0.0.0:*     LISTEN      4654/xinetd
    

    第 5 步:添加您的iptables规则:

    iptables -A INPUT -i eth0 -p tcp -d 192.168.0.60 --dport 15000 -j ACCEPT
    iptables -A OUTPUT -o eth0 -p tcp -s 192.168.0.60 --sport 15000 -j ACCEPT
    

    让我们测试一下:

    为了进行测试,我设置mysql了监听127.0.0.1:10000。我将尝试通过xinetdport 上的服务访问它15000。

    首先,在服务器端,我确保mysql服务器只在监听127.0.0.1:10000:

    # netstat -anp | grep :10000
    tcp    0    0 127.0.0.1:10000    0.0.0.0:*      LISTEN      4247/mysqld
    

    然后,在客户端,让我们检查是否可以使用端口进行连接15000:

    # telnet 192.168.0.60 15000
    Trying 192.168.0.60...
    Connected to 192.168.0.60.
    Escape character is '^]'.
    _
    5.5.35-0ubuntu0.12.04.2-logD46S}<P`.6cr4ITIQ<wcmysql_native_password
    

    看来我们可以!:)

    让我们尝试连接到mysql服务器:

    # mysql -h 192.168.0.60 --port=15000 -u root -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 67
    Server version: 5.5.35-0ubuntu0.12.04.2-log (Ubuntu)
    
    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>
    

    完毕 !

    • 2

相关问题

  • 谁能指出我的 802.11n 范围扩展器?

  • 我怎样才能得到一个网站的IP地址?

  • 在一个 LAN 中使用两台 DHCP 服务器

  • 如何在 Linux 下监控每个进程的网络 I/O 使用情况?

  • 为本地网络中的名称解析添加自定义 dns 条目

Sidebar

Stats

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

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve