Sunday, May 26, 2019

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      #使配置立即生效

Related Post

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.