iptables 将所有请求重定向到本地主机

发布于 2024-12-22 05:27:56 字数 118 浏览 6 评论 0原文

我想将对特定 IP 发出的所有请求重定向到本地主机 (127.0.0.1)。我需要它,因为我有服务器的本地副本,并且想要测试一些东西,

我如何编写 iptables 规则来做到这一点?

谢谢

I want to redirect all requests made to a particular ip to localhost (127.0.0.1). I need it because I have a local replica of a server and want to test some things

How can I write a iptables rule to do that?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

于我来说 2024-12-29 05:27:56

您的意思是“我希望从机器 A 向机器 B 发出的所有请求都重定向到机器 A”吗?如果是这样,我相信您正在寻找的命令是

sudo iptalbes -t nat -A PREROUTING -d <DESTINATION_IP> -p <PROTOCOL> --dport <DESTINATION_PORT_NUMBER> -j DNAT --to 127.0.0.1

Did you mean "I want all requests made from machine A to machine B, to be redirected to machine A"? If so, I believe the command you are looking for is

sudo iptalbes -t nat -A PREROUTING -d <DESTINATION_IP> -p <PROTOCOL> --dport <DESTINATION_PORT_NUMBER> -j DNAT --to 127.0.0.1
揪着可爱 2024-12-29 05:27:56
iptables -t mangle -A PREROUTING -p tcp ''otherconditions'' -j TPROXY --on-port ''dst''

引用联机帮助页:此目标仅在 PREROUTING 链中的 mangle 表中有效
以及仅从此链调用的用户定义链。它重新
将数据包调整到本地套接字而不更改数据包标头
以任何方式。

iptables -t mangle -A PREROUTING -p tcp ''otherconditions'' -j TPROXY --on-port ''dst''

Quoting manpage: This target is only valid in the mangle table, in the PREROUTING chain
and user-defined chains which are only called from this chain. It redi-
rects the packet to a local socket without changing the packet header
in any way.

街道布景 2024-12-29 05:27:56

我知道这是一个很旧的线程......但仍然没有答案。所需的主要更改是附加 OUTPUT 规则而不是 PREROUTING 规则。

从手册页:

nat:
    This table is consulted when a packet that creates a new connection is encountered.  It consists of three built-ins: PREROUTING (for altering packets as soon as they come in), OUTPUT (for altering locally-generated packets  before  routing),  and  POSTROUTING  (for altering packets as they are about to go out).

所以,这可能是所需的命令:

iptables -t nat -A OUTPUT -p tcp --src <SOURCE PORT TO BE REDIRECTED> --dst 127.0.0.1 --dport 9090 -j REDIRECT --to-ports 9090

I know this is a pretty old thread... But still without an answer. The main change required will be to append an OUTPUT rule rather than PREROUTING rule.

From the man page:

nat:
    This table is consulted when a packet that creates a new connection is encountered.  It consists of three built-ins: PREROUTING (for altering packets as soon as they come in), OUTPUT (for altering locally-generated packets  before  routing),  and  POSTROUTING  (for altering packets as they are about to go out).

So, this might be the command required:

iptables -t nat -A OUTPUT -p tcp --src <SOURCE PORT TO BE REDIRECTED> --dst 127.0.0.1 --dport 9090 -j REDIRECT --to-ports 9090
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文