以下情况对应的linux系统版本为CentOS7,若使用的环境为CentOS6请使用service iptables state/start/stop/restart替换对应的systemctl status /start/stop/restart firewalld.service命令;
1、查看防火墙当前状态方法1 [root@hu ~]# firewall-cmd --statenot running ---未运行[root@hu ~]# firewall-cmd --staterunning ---运行中 方法2 [root@hu ~]# systemctl status firewalld.service 正常运行中 
异常/未运行 
2、启动防火墙服务[root@hu ~]# systemctl start firewalld.service
3、关闭防火墙服务[root@hu ~]# systemctl stop firewalld.service
4、开放指定端口#添加--permanent参数,重启服务才能生效且永久生效;#若不加--permanent参数,立即生效,重启服务后失效;[root@hu ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanentsuccess[root@hu ~]# firewall-cmd --zone=public --add-port=80/tcp --permanentsuccess#重启服务使开放的端口生效[root@hu ~]# firewall-cmd --reloadsuccess#4.1、验证新开放端口是否生效,yes表示生效,no表示没有失效[root@hu ~]# firewall-cmd --zone=public --query-port=8080/tcpyes
5、移除指定端口,需重启防火墙服务才能生效[root@hu ~]# firewall-cmd --zone=public --remove-port=8080/tcp --permanent#命令验证是否移除成功[root@hu ~]# firewall-cmd --zone=public --query-port=80/tcp#或者通过检查当前防火墙开放的端口查看[root@hu ~]# firewall-cmd --permanent --zone=public --list-ports8081/tcp 3306/tcp 111/tcp 111/udp 2049/tcp 2049/udp 1001/tcp 1001/udp 1002/tcp 1002/udp 30001/tcp 30002/udp 80/tcp 8080/tcp#添加多个端口可通过配置多个--add-port参数,例:[root@hu ~]# firewall-cmd --zone=public --add-port=80/tcp --add-port=8080/tcp --permanent
6、重启防火墙方法1 [root@hu ~]# systemctl restart firewalld.service 方法2 [root@hu ~]# firewall-cmd --reloadsuccess
7、设置防火墙开机自启动[root@hu ~]# systemctl enable firewalld.service#重启机器并查看防火墙状态[root@hu ~]# reboot......[root@hu ~]# firewall-cmd --staterunning ---表示开机自启成功
总结 下载地址: Linux和GNU系统的关系详解 shell脚本设置防止暴力破解ssh |