CentOS7使用firewalld防火墙

时间:2023-03-10 03:55:31
CentOS7使用firewalld防火墙

firewalld的基本使用

启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld 
开机禁用  : systemctl disable firewalld
开机启用  : systemctl enable firewalld

systemctl

systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。

启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed

配置firewalld-cmd

查看版本: firewall-cmd --version
查看帮助: firewall-cmd --help
显示状态: firewall-cmd --state
查看所有打开的端口: firewall-cmd --zone=public --list-ports
更新防火墙规则: firewall-cmd --reload
查看区域信息:  firewall-cmd --get-active-zones
查看指定接口所属区域: firewall-cmd --get-zone-of-interface=eth0
拒绝所有包:firewall-cmd --panic-on
取消拒绝状态: firewall-cmd --panic-off
查看是否拒绝: firewall-cmd --query-panic
那怎么开启一个端口呢
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
重新载入,添加完规则后 reload生效
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent

查看有哪些服务已经在列表中允许通过:

firewall-cmd --list-services

添加服务

firewall-cmd --permanent --add-service openvpn   (--permanent永久生效,没有此参数重启后失效)

firewalld对指定IP开放指定端口的配置

允许192.168.142.166访问5432端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="" accept"
删除规则
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="5432" accept"
重新加载配置文件
firewall-cmd --reload

firewalld 配置转发

firewall-cmd --zone=inernal --change-interface=eno2
firewall-cmd --zone=internal --change-interface=eno2
firewall-cmd --zone=public --add-forward-port=port=:proto=tcp:toport=:toaddr=192.168.224.5 --permanent
firewall-cmd --zone=public --add-forward-port=port=:proto=tcp:toport=:toaddr=192.168.224.5 --permanent
firewall-cmd --zone=public --add-forward-port=port=:proto=tcp:toport=:toaddr=192.168.224.5 --permanent
firewall-cmd --zone=public --add-forward-port=port=:proto=tcp:toport=:toaddr=192.168.224.5 --permanent
firewall-cmd --zone=public --add-forward-port=port=:proto=tcp:toport=:toaddr=192.168.224.5 --permanent
firewall-cmd --zone=public --add-forward-port=port=:proto=tcp:toport=:toaddr=192.168.224.5 --permanent
firewall-cmd --zone=public --add-masquerade --permanent
firewall-cmd --zone=internal --add-masquerade --permanent
firewall-cmd --reload
firewall-cmd --list-all-zones

NET转发,firewalld实现内网访问外网

应用场景:机房服务器里一些没有外网IP的,通过跳板机上网。

服务端,使用firewalld防火墙实现nat路由转发

systemctl start firewalld.service         #关闭其他防火墙,然后开启firewalld
firewall-cmd --add-masquerade --permanent    #内部主机依靠firewalld转发上网
firewall-cmd --reload                #生效

客户端,修改网关为跳板机内网IP

vim  /etc/sysconfig/network-scripts/ifcfg-em2
IPADDR=192.168.224.22
NETMASK=255.255.255.0
GATEWAY=192.168.224.9
DNS1=8.8.8.8
ZONE=internal # 重启网卡
ifdown em2 && ifup em2 # 测试
ping www.baidu.com