rhel7以上的版本有这个命令
开启服务
systemctl start firewalld.service
systemctl enable firewalld.service
查看允许的服务
firewall-cmd --list-all
查看所有可用区域
firewall-cmd --get-default-zone
设置默认区域
firewall-cmd --set-default-zone=trusted
firewall-cmd --set-default-zone=public
firewall-cmd --set-default-zone=block ##服务访问时不做任何回应
添加服务(临时)
firewall-cmd --add-service=http
删除服务(临时)
firewall-cmd --remove-service=smtp
重起火墙时又会恢复原来的配置,下面是永久配置:
第一种:
vim /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
<service name="dhcpv6-client"/>
<service name="ssh"/>
<service name="ftp"/>
<service name="http"/>
</zone>
重新加载
firewall-cmd --reload
第二种:
firewall-cmd --permanent--add-service=http ##添加
firewall-cmd --permanent --remove-service=smtp ##删除
再进行reload就好
reload时会默认加载下面目录的文件:
[root@server zones]# cd /usr/lib/firewalld/services/
[root@server services]# ls
amanda-client.xml http.xml libvirt.xml pmwebapis.xml ssh.xml
bacula-client.xml imaps.xml mdns.xml pmwebapi.xml telnet.xml
bacula.xml ipp-client.xml mountd.xml pop3s.xml tftp-client.xml
dhcpv6-client.xml ipp.xml ms-wbt.xml postgresql.xml tftp.xml
dhcpv6.xml ipsec.xml mysql.xml proxy-dhcp.xml transmission-client.xml
dhcp.xml kerberos.xml nfs.xml radius.xml vnc-server.xml
dns.xml kpasswd.xml ntp.xml rpc-bind.xml wbem-https.xml
ftp.xml ldaps.xml openvpn.xml samba-client.xml
high-availability.xml ldap.xml pmcd.xml samba.xml
https.xml libvirt-tls.xml pmproxy.xml smtp.xml
如果http的端口是8080,火墙还是不会允许
添加端口
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
firewall-cmd --permanent --remove-port=8080/tcp
firewall-cmd --reload
设置网络地址到指定的区域:
firewall-cmd --permanent --add-source=172.25.254.38 --zone=trusted
firewall-cmd --reload
firewall-cmd --permanent --remove-source=172.25.254.38 --zone=trusted
source也可添加网段172.25.254.0/24
添加、删除网络接口:
firewall-cmd --permanent --remove-interface=eth0 --zone=public
firewall-cmd --permanent --add-interface=eth0 --zone=trusted
systemctl restart firewalld.service
更加细化的添加规则
通过 firewall-cmd 工具,可以使用 –direct 选项在运行时间里增加或者移除链。
如果不熟悉 iptables ,使用直接接口非常危险,因为您可能无意间导致防火墙被入侵。
直接端口模式适用于服务或者程序,以便在运行时间内增加特定的防火墙规则。
直接端口模式添加的规则优先应用。
firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 ! -s 172.25.254.238 -p tcp --dport 22 -j ACCEPT
除了238这个主机不能连,其它都可以;0是第几条规则
firewall-cmd –reload
查看添加的规则
firewall-cmd --direct --get-all-rules
firewall-cmd --permanent --direct --remove-rule ipv4 filter INPUT 0 ! -s 172.25.254.238 -p tcp --dport 22 -j ACCEPT ##删除
firewall-cmd –reload
reload 之后如果还有,将火墙重起
多语言命令的格式:
添加规则:
firewall-cmd --add-rich-rule='rule family="ipv4" source address="172.25.38.138" accept'
允许172.25.38.138主机所有连接。
firewall-cmd --add-rich-rule='rule service name=ftp limit value=2/m accept'
每分钟允许2个新连接访问ftp服务。
firewall-cmd --add-rich-rule='rule service name=ftp log limit value="1/m" audit accept'
同意新的 IP v4 和 IP v6 连接 FT P ,并使用审核每分钟登录一次。
firewall-cmd --add-rich-rule='rule family="ipv4" source address="172.25.0.0/24"
service name=ssh log prefix=”ssh” level=”notice” limit value=”3/m” accept’
允许来自172.25.0.0/24地址的新 IPv4连接连接TFTP服务,并且每分钟记录一次。
firewall-cmd --permanent --add-rich-rule='rule protocol value=icmp drop'
丢弃所有icmp包
伪装:
firewall-cmd --permanent --zone=< ZONE > --add-masquerade
firewall-cmd --permanent --zone=< ZONE > --add-rich-rule='rule family=ipv4 source addres=172.25.0.0/24 masquerade'
端口转发:
firewall-cmd --permanent --zone=< ZONE > --add-forward-port=
port=80:proto=tcp:toport=8080:toaddr=172.25.38.138
firewall-cmd --permanent --zone=< ZONE > --add-rich-rule='rule family=ipv4 source address=172.25.0.0/24 forward-port port=80 protocol=tcp to-port=8080'