centos7

有用的firewall-cmd命令备忘

remove public zone service:
firewall-cmd –zone=public –remove-service=ssh –permanent
firewall-cmd –reload

add ip range to trusted zone and add ssh service to trusted zone

firewall-cmd –permanent –zone=”trusted” –add-source=”123.51.11.2/32″
firewall-cmd –permanent –zone=”trusted” –add-service=”ssh”
firewall-cmd –zone=public –add-port=7788/tcp –permanent
firewall-cmd –reload
firewall-cmd –list-all-zones

add anthor ip range to trusted zone

firewall-cmd –permanent –zone=”trusted” –add-source=”172.30.30.0/24″
firewall-cmd –reload
firewall-cmd –list-all-zones

一、centos7版本对防火墙进行加强,不再使用原来的iptables,启用firewalld
1.firewalld的基本使用
启动: systemctl start firewalld
查状态:systemctl status firewalld
停止: systemctl disable firewalld
禁用: systemctl stop firewalld
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl –failed

2.配置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

3.那怎么开启一个端口呢
添加
firewall-cmd –zone=public(作用域) –add-port=80/tcp(端口和访问类型) –permanent(永久生效)
firewall-cmd –zone=public –add-service=http –permanent
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 –get-services
firewall-cmd –add-service=<service>
firewall-cmd –delete-service=<service>
在每次修改端口和服务后/etc/firewalld/zones/public.xml文件就会被修改,所以也可以在文件中之间修改,然后重新加载
使用命令实际也是在修改文件,需要重新加载才能生效。

4.详细使用
firewall-cmd –permanent –zone=public –add-rich-rule=’rule family=”ipv4″ source address=”192.168.0.4/24″ service name=”http” accept’ //设置某个ip访问某个服务
firewall-cmd –permanent –zone=public –remove-rich-rule=’rule family=”ipv4″ source address=”192.168.0.4/24″ service name=”http” accept’ //删除配置
firewall-cmd –permanent –add-rich-rule ‘rule family=ipv4 source address=192.168.0.1/2 port port=80 protocol=tcp accept’ //设置某个ip访问某个端口
firewall-cmd –permanent –remove-rich-rule ‘rule family=ipv4 source address=192.168.0.1/2 port port=80 protocol=tcp accept’ //删除配置

firewall-cmd –query-masquerade # 检查是否允许伪装IP
firewall-cmd –add-masquerade # 允许防火墙伪装IP
firewall-cmd –remove-masquerade # 禁止防火墙伪装IP

firewall-cmd –add-forward-port=port=80:proto=tcp:toport=8080 # 将80端口的流量转发至8080
firewall-cmd –add-forward-port=proto=80:proto=tcp:toaddr=192.168.1.0.1 # 将80端口的流量转发至192.168.0.1
firewall-cmd –add-forward-port=proto=80:proto=tcp:toaddr=192.168.0.1:toport=8080 # 将80端口的流量转发至192.168.0.1的8080端口