将IPTables规则转换为Firewalld规则以redirect

我不是很熟悉networking的东西,我很难理解干草firewalld作品。

我正在开发一个REST服务,实际上在端口8080上侦听,而且我希望能够发送端口80上的请求,这个请求将被redirect到8080。

为了在CentOS 6上做到这一点,我使用了iptables和这样一个规则:

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 

我在CentOS 7上迁移,甚至iptables仍然存在,仍然有效,事实上,firewalld是默认的防火墙软件,让我想我应该开始使用该软件…事实是,我不明白它是如何工作的,如何将我的单个iptables规则转换成firewalld规则。 我知道firewalld“理解”了iptables规则(实际上,我正在使用firewalld的这个规则继续工作),但是我想知道如何去做,而且我也希望这个规则也是永久的。

谢谢

使用--add-forward-port来设置端口转发。

firewall-cmd手册页:

  --add-forward-port=port=portid[-portid]:proto=protocol[:toport=portid[-portid]][:toaddr=address[/mask]] [--timeout=timeval] Add the IPv4 forward port for zone. If zone is omitted, default zone will be used. This option can be specified multiple times. If a timeout is supplied, the rule will be active for the specified amount of time and will be removed automatically afterwards. timeval is either a number (of seconds) or number followed by one of characters s (seconds), m (minutes), h (hours), for example 20m or 1h. The port can either be a single port number portid or a port range portid-portid. The protocol can either be tcp, udp, sctp or dccp. The destination address is a simple IP address. 

所以你会做这样的事情:

 firewall-cmd ---zone=whatever --add-forward-port=80:proto=tcp:toport=8080 

如果它做到了你想要的东西, 永久性的 。

iptables是默认的防火墙工具,你可以在所有的linux版本下find它。 firewalld是一个方便的工具,所以用户可以在不知情的情况下与“iptables规则”进行交互。 使用firewall-cmd命令似乎很简单,因为您可以在运行中提取预定义的区域和服务(会自动转换为特定的端口)。 您可以重新加载firewalld.service,而无需重新启动或造成不便。

你仍然可以在CentOS7上使用iptables,但是你必须禁用firewalld(并且更好地屏蔽它):

 systemctl stop firewalld systemctl disable firewalld systemctl mask firewalld 

安装iptables-services和iptables-utils:

 yum install -y iptables-services iptables-utils 

现在你已经可以在CentOS7上使用iptables了

如果您需要保存您的configuration以保持重新启动:

 iptables-save >/etc/sysconfig/iptables 

如果你想改变你的iptables规则的顺序,你可以编辑他们的文件(例如/ your_file),然后:

 iptables-restore </your_file 

规则将被恢复。

希望能帮到你