Centos 7的Firewall流量转发

默认分类 firewall 流量转发

近期买了一个NAT机,用来做中转,所以简单记录一下。相比于iptables和firewall,我当然更倾向于使用后者,毕竟新东西造出来就是要用的,不然就没意义了。只是简单赘述一下,一些深入的firewall操作请自行在网上寻求答案。

安装firewall

首先装一下firewall,没想到这个NAT机居然没装防火墙...

yum install firewalld -y

然后设置开机自启

systemctl enable firewalld

启动firewall

systemctl start firewalld

准备工作

然后开启路由转发,在文件/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

至此准备工作完成。

设置转发

首先你要先开放端口,比如开放一个8080端口

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

这些,TCP和UDP流量就能通过8080端口进来了。

转发本地

比如要把8080端口的流量转发到自己的8090端口。

firewall-cmd --add-forward-port=port=8080:proto=tcp:toport=8090 --permanent
firewall-cmd --add-forward-port=port=8080:proto=udp:toport=8090 --permanent

这样就可以了。

转发远程

这个就是流量中转,比如把8080端口收到的流量转发到IP地址为119.28.28.28的53端口

firewall-cmd --add-forward-port=port=8080:proto=tcp:toaddr=119.28.28.28:toport=53 --permanent
firewall-cmd --add-forward-port=port=8080:proto=udp:toaddr=119.28.28.28:toport=53 --permanent

重载配置文件

最后设置完要记得重新加载配置文件生效

firewall-cmd --reload

手动修改配置文件

firewall默认的配置文件在/etc/firewalld/zones/public.xml
也可直接编辑修改。

修改完以后也要重新加载配置文件才能生效。

新评论

称呼不能为空
邮箱格式不合法
网站格式不合法
内容不能为空