Linux下利用Firewall进行端口转发

博客之前写过一篇文章,利用iptables进行端口转发,但是在实际使用中发现,部分机器的iptables规则无法生效,加上iptables已经是过去式,所以本文介绍使用Firewall进行端口转发。

如果安装过iptables的机器,需要先停止,然后设置开机禁用:

[root@localhost ~]#servcie iptables stop                     --临时关闭防火墙
[root@localhost ~]#chkconfig iptables off                    --永久关闭防火墙

特别注意:请勿使用yum remove iptables来卸载防火墙,因为iptables附带了很多依赖文件,这样卸载会导致机器SSH丢失。

安装firewall防火墙

yum install firewalld -y

设置开机自启

systemctl enable --now firewalld

开启路由转发

echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p

开启防火墙的流量伪装功能

firewall-cmd --zone=public --permanent --add-masquerade

开启TCP流量转发

firewall-cmd --add-forward-port=port=8080:proto=tcp:toaddr=2.2.2.2:toport=666 --permanent
#将本地8080端口,转发到2.2.2.2的tcp666端口。

开启UDP流量转发

firewall-cmd --add-forward-port=port=8080:proto=udp:toaddr=2.2.2.2:toport=666 --permanent
#将本地8080端口,转发到2.2.2.2的udp666端口。

重载配置文件

firewall-cmd --reload

如何开放端口?

开启TCP流量端口

firewall-cmd --add-port=8080/tcp --permanent

开启UDP流量端口

firewall-cmd --add-port=8080/udp --permanent

注意:每次操作完成后,都需要对Firewall进行重载:

重载配置文件firewall-cmd --reload
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容