大网数据广告活动
服务器运维问答

Adding a permanent PREROUTING rule in iptables using firewall-cmd

· 52CE 我爱测

I'm trying to add a new rule in the PREROUTING chain in iptables (NAT) using firewall-cmd on RHEL 7: $ firewall-cmd --permanent --direct --add-rule ipv4 nat PREROUTING 0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8161 Then I…

问题原文

原帖链接 · xeroqu · CC BY-SA 3.0

I'm trying to add a new rule in the PREROUTING chain in iptables (NAT) using firewall-cmd on RHEL 7:

$ firewall-cmd --permanent --direct --add-rule ipv4 nat PREROUTING 0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8161

Then I check the iptables via $ iptables -t nat -L:

...
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
PREROUTING_direct  all  --  anywhere             anywhere
PREROUTING_ZONES_SOURCE  all  --  anywhere             anywhere
PREROUTING_ZONES  all  --  anywhere             anywhere
...
Chain PREROUTING_direct (1 references)
target     prot opt source               destination
REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:http redir ports 8161
...

However, if I run an equivalent iptables command as follows:

...
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
PREROUTING_direct  all  --  anywhere             anywhere
PREROUTING_ZONES_SOURCE  all  --  anywhere             anywhere
PREROUTING_ZONES  all  --  anywhere             anywhere
REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:http redir ports 8161
...
Chain PREROUTING_direct (1 references)
target     prot opt source               destination
REDIRECT   tcp  --  anywhere             anywhere             tcp dpt:http redir ports 8161

I get this additional rule in the Chain PREROUTING and this allows prerouting to work even if the firewall is disabled (i.e., disabling firewall daemon and running the iptables command).

So, my question is two-fold:

  • Is there a firewall-cmd command that does exactly the same as the iptables command above?
  • Can this rule be added permanently via firewall-cmd and stay there even after firewall daemon is disabled?

已采纳解答

原帖链接 · Khaled · CC BY-SA 3.0

You are using firewall-cmd with --direct option which means it accepts an iptables command. So, you can just the same options with iptables -t nat to have the same effect with one exception. Using firewall-cmd this way will add NAT rule to PREROUTING_direct chain while using iptables directly with add the rule to PREROUTING chain.

In the output of iptables -t nat -L, you added the rule twice: once to each chain.

As for the second part of question, firewalld service will remove all defined chains when stopped. So, rules added to PREROUTING_direct will not be available any more. Short answer is No.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。