CentOS7防火牆關閉與配置

CentOS7用firewall命令“替代”了iptables。在這裏我們需要區分“iptables服務”和“iptables命令”。雖然firewalld是替代提供的防火牆管理iptables服務,但是它仍然使用iptables對內核命令動態通信包過濾。所以它只是iptables服務代替,而不是iptables命令。

systemctl stop firewalld
systemctl disable firewalld

yum install iptables-services

touch /etc/sysconfig/iptables
systemctl start iptables
systemctl enable iptables

touch /etc/sysconfig/ip6tables
systemctl start ip6tables
systemctl enable ip6table

同上
CentOS 7.0默認使用的是firewall作爲防火牆,這裏改爲iptables防火牆。
1、關閉firewall:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啓動

2、安裝iptables防火牆

yum install iptables-services #安裝
systemctl restart iptables.service #最後重啓防火牆使配置生效
systemctl enable iptables.service #設置防火牆開機啓動

關閉SELINUX

vi /etc/selinux/config
#SELINUX=enforcing #註釋掉
#SELINUXTYPE=targeted #註釋掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效

安裝firewalld和firewall-cmd請參考結尾link
檢查防火牆狀態

firewall-cmd --stat

臨時開放ftp服務
firewall-cmd --add-service=ftp
永久開放ftp服務
firewall-cmd --add-service=ftp --permanent
關閉ftp服務
firewall-cmd --remove-service=ftp --permanent
配置防火牆在public區域永久開放http服務
firewall-cmd --permanent --zone=public --add-service=http
加入指定開放端口
firewall-cmd --add-port=1324/tcp

爲了讓之前的設定生效當然要重啓服務咯
systemctl restart firewalld
或者使用下面的命令免去重啓服務(防火牆策略配置後重新載入)
firewall-cmd --complete-reload
firewall-cmd --reload   (這兩句功能相同)

檢查ftp服務的21端口是否開放

iptables -L -n | grep 21
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:21 ctstate NEW

查詢ftp服務啓用狀態

firewall-cmd --query-service ftp

查看當前規則

firewall-cmd --list-all

僅允許部分IP訪問本機服務配置

firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" \
        source address="192.168.0.4/24" service name="http" accept"

僅允許部分IP訪問本機端口配置

firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" \
     source address="192.168.0.4/24" \
     port protocol="tcp" port="8080" accept" 

公共配置文件位置:/etc/firewalld/zones/public.xml

CentOS6關閉防火牆

service iptables status          //查看防火牆狀態
service iptables stop            //關閉防火牆
chkconfig iptables –list         //查看防火牆開機啓動狀態
chkconfig iptables off           //關閉防火牆開機啓動

一.Linux下開啓/關閉防火牆命令
1) 永久性生效,重啓後不會復原

開啓: chkconfig iptables on
關閉: chkconfig iptables off

2) 即時生效,重啓後復原

開啓: service iptables start
關閉: service iptables stop

需要說明的是對於Linux下的其它服務都可以用以上命令執行開啓和關閉操作。
在當開啓了防火牆時,做如下設置,開啓相關端口,
修改/etc/sysconfig/iptables 文件,添加以下內容:

-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT

-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT

二.UBuntu關閉防火牆
iptables -A INPUT -i ! PPP0 -j ACCEPT

三.CentOS Linux 防火牆配置及關閉
執行”setup”命令啓動文字模式配置實用程序,在”選擇一種工具”中選擇”防火牆配置”,然後選擇”運行工具”按鈕,出現防火牆配置界面,將”安全級別”設爲”禁用”,然後選擇”確定”即可.
或者用命令:

/sbin/iptables -I INPUT -p tcp –dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp –dport 22 -j ACCEPT
/etc/rc.d/init.d/iptables save

這樣重啓計算機後,防火牆默認已經開放了80和22端口
這裏應該也可以不重啓計算機:
/etc/init.d/iptables restart

驗證防火牆是否關閉

chkconfig --list | grep iptables
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章