Saturday, June 24, 2017

CentOS 7安装启用iptables(停用firewalld)

CentOS 7默认的防火墙是firewalld,不是iptables。如果想使用iptables,首先应该停用默认的firewalld,然后安装iptables服务,并且开启80端口、3306端口等。

关闭firewalld防火墙

systemctl stop firewalld.service      #停用firewalld防火墙
systemctl disable firewalld.service      #禁止firewalld防火墙开机启动

安装iptables防火墙

yum install iptables-services

编辑iptables防火墙配置文件

vi /etc/sysconfig/iptables #编辑防火墙配置文件

下边是一个完整的配置文件:

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

:wq! #保存退出

systemctl start iptables.service      #开启iptables防火墙
systemctl restart iptables.service      #重启iptables防火墙使配置生效
systemctl enable iptables.service      #设置iptables防火墙开机启动
iptables -L      #查看防火墙规则,默认的是-t filter,如果是nat表查看,即iptables -t nat -L

关闭SELINUX

vim /etc/selinux/config

#SELINUX=enforcing      #注释掉
#SELINUXTYPE=targeted      #注释掉
SELINUX=disabled      #增加
setenforce 0      #使配置立即生效

Leave a Reply

Your email address will not be published. Required fields are marked *