linux 防火墙设置

1.永久有效

开启: chkconfig iptables on 
关闭: chkconfig iptables off

2.即刻生效

开启: service iptables start 
关闭: service iptables stop 

3.开启部分端口

vim /etc/sysconfig/iptables

添加想要开启的相关端口

-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8082 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
service iptables restart

********************************2018-11-12******************************
centos7以后默认使用是firewalld防火墙要想使用iptables就要先关闭firewalld
1,systemctl stop firewalld
2,systemctl mask firewalld

    安装iptables

yum install iptables-services

设置开机启动

systemctl enable iptables

停止/启动/重启 防火墙:

systemctl [stop|start|restart] iptables
#or
service iptables [stop|start|restart]

注意上面systemctl这个命令是centos7以后的命令,相当于service

保存防火墙配置:

service iptables save
#or
/usr/libexec/iptables/iptables.init save
查看iptables的规则
iptables -L -n

****************************************************************************18-12-24*****************************************************

firewall 为防火墙添加服务,比如mysql或者http,会自动打开相应的端口

一旦您测试了一切正常,您可能需要修改永久防火墙规则,以便在重新启动后仍然可以使用您的服务。我们可以通过键入以下命令使我们的“公共”区域更改永久

sudo firewall-cmd --zone=public --permanent --add-service=http

您可以通过向操作添加--permanent标志来验证这是否成功--list-services。您需要sudo用于任何--permanent操作:

sudo firewall-cmd --zone=public --permanent --list-services

查看所有打开的端口: 

firewall-cmd --zone=public --list-ports
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent


原文地址:https://www.cnblogs.com/albertzhangyu/p/9673949.html