# CentOS 7开启firewalld流量转发功能,简单配置服务器TCP/UDP中转加速教程-转发 > 源地址:[https://zhujiget.com/3451.html](https://zhujiget.com/3451.html) > > 源作者:zhujiget ## 简介 firewalld是Linux系统下的防火墙,基本上会默认安装在centos7版本系统,而centos7以下版本则使用的是iptables(iptables转发设置),本文主要介绍下firewalld的简单使用,及利用firewalld的流量转发功能开启TCP/UDP中转加速。  ## 安装firewall防火墙 首先装一下firewalld,默认centos7系统会自带,不过有的机器并没有: ```bash yum install firewalld -y ``` 常用命令 ```bash systemctl start firewalld ##开启防火墙 systemctl stop firewalld ##关闭防火墙 firewall-cmd --reload ##重启防火墙 systemctl status firewalld ##查看防火墙状态 systemctl enable firewalld ##设置开启启动 systemctl disable firewalld ##禁用开机启动 firewall-cmd --list-ports ##查看开放的端口 firewall-cmd --zone=public --add-port=8080/tcp --permanent ##开放指定端口(此处为开放8080,可替换为指定端口号),开放后需重启防火墙生效 firewall-cmd --zone=public --remove-port=8080/tcp --permanent ##关闭指定端口(此处为关闭8080,可替换为指定端口号) ``` ## 准备工作 首先需要设置开启路由转发,在文件/etc/sysctl.conf的最后加上代码net.ipv4.ip_forward = 1,执行下列命令: `echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf` 然后命令行执行下列命令使其生效: `sysctl -p` 最后开启防火墙的流量伪装功能,执行下列命令: `firewall-cmd --zone=public --permanent --add-masquerade` 至此准备工作完成。 ## 设置TCP/UDP中转 首先需要先开放端口,比如开放一个8080端口(端口替换你需要开放的端口),执行下列命令,如已开放服务器全部端口可跳过这步: ```bash #开启TCP流量端口 firewall-cmd --add-port=8080/tcp --permanent #开启UDP流量端口 firewall-cmd --add-port=8080/udp --permanent ``` ## 转发本地口 比如我需要要把8080端口的流量转发到自己的8090端口(端口替换为自己需要的相应端口),执行下列命令即可: ```bash #开启TCP流量转发 firewall-cmd --add-forward-port=port=8080:proto=tcp:toport=8090 --permanent #开启UDP流量转发 firewall-cmd --add-forward-port=port=8080:proto=udp:toport=8090 --permanent ``` ## 转发远程服务器端口 这个就是流量中转,比如要把IP地址1.1.1.1这台服务器的8080端口收到的流量,转发到服务器IP地址为2.2.2.2的666端口,则在IP地址1.1.1.1这台服务器上执行下列命令: ```bash #开启TCP流量转发 firewall-cmd --add-forward-port=port=8080:proto=tcp:toaddr=2.2.2.2:toport=666 --permanent #开启UDP流量转发 firewall-cmd --add-forward-port=port=8080:proto=udp:toaddr=2.2.2.2:toport=666 --permanent ``` # 禁用端口&删除转发 ```bash #禁用转发端口 firewall-cmd --remove-masquerade --permanent # 要删除特定的转发规则,需要使用--remove-forward-port选项, # 并指定要删除的规则的详细信息,包括端口号、协议类型、目标端口和目标地址 [root@root sbin]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: ssh dhcpv6-client ports: protocols: masquerade: yes forward-ports: port=123:proto=tcp:toport=123:toaddr=10.110.105.81 port=124:proto=tcp:toport=124:toaddr=10.110.105.81 source-ports: icmp-blocks: rich rules: # --permanent:这个选项表示对防火墙的更改是永久性的, # 即这些更改会被保存到防火墙的配置文件中,并在防火墙重启后仍然有效。 # 如果不使用这个选项,更改将仅对当前会话有效,重启防火墙后会丢失。 [root@root sbin]# firewall-cmd --permanent --zone=public --remove-forward-port=port=123:proto=tcp:toport=123:toaddr=10.110.105.81 success [root@root sbin]# firewall-cmd --permanent --zone=public --remove-forward-port=port=124:proto=tcp:toport=124:toaddr=10.110.105.81 success [root@root sbin]# firewall-cmd --reload success ``` ## 重载配置文件 设置完规则后需要重新加载配置文件生效,执行下列命令: `firewall-cmd --reload` ## 手动修改配置文件 firewalld默认的配置文件是`/etc/firewalld/zones/public.xml`,直接使用文本编辑修改,修改完以后也要重新加载配置文件才能生效。 ## 快照 - https://pic.rmb.bdstatic.com/bjh/240911/1a88c997a901ee5cc837355e4445b9ca7734.png - https://i3.wp.com/pic.rmb.bdstatic.com/bjh/240911/1a88c997a901ee5cc837355e4445b9ca7734.png