Linux防火牆

CentOS7默認的防火牆不是iptables,而是firewalle.

CentOS 7.0默認使用的是firewall做爲防火牆,這裏改成iptables防火牆。
firewall:
systemctl start firewalld.service         #啓動firewall
systemctl stop firewalld.service         #中止firewall
systemctl disable firewalld.service        #禁止firewall開機啓動centos

systemctl status firewalld                #  查看防火牆狀態服務器


 

2.防火牆詳述



官方文檔地址:https://access.redhat.com/docume ... uction_to_firewalld

一、firewalld簡介
firewalld是centos7的一大特性,最大的好處有兩個:支持動態更新,不用重啓服務;第二個就是加入了防火牆的「zone」概念

firewalld有圖形界面和工具界面,因爲我在服務器上使用,圖形界面請參照官方文檔,本文以字符界面作介紹

firewalld的字符界面管理工具是 firewall-cmd 

firewalld默認配置文件有兩個:/usr/lib/firewalld/ (系統配置,儘可能不要修改)和 /etc/firewalld/ (用戶配置地址)

zone概念:
硬件防火牆默認通常有三個區,firewalld引入這一律念系統默認存在如下區域(根據文檔本身理解,若是有誤請指正):
drop:默認丟棄全部包
block:拒絕全部外部鏈接,容許內部發起的鏈接
public:指定外部鏈接能夠進入
external:這個不太明白,功能上和上面相同,容許指定的外部鏈接
dmz:和硬件防火牆同樣,受限制的公共鏈接能夠進入
work:工做區,概念和workgoup同樣,也是指定的外部鏈接容許
home:相似家庭組
internal:信任全部鏈接
對防火牆不算太熟悉,還沒想明白public、external、dmz、work、home從功能上都須要自定義容許鏈接,具體使用上的區別還需高人指點

二、安裝firewalld
root執行 # yum install firewalld firewall-config

三、運行、中止、禁用firewalld
啓動:# systemctl start  firewalld
查看狀態:# systemctl status firewalld 或者 firewall-cmd --state
中止:# systemctl disable firewalld
禁用:# systemctl stop firewalld

四、配置firewalld
查看版本:$ firewall-cmd --version
查看幫助:$ firewall-cmd --help
查看設置:
                顯示狀態:$ firewall-cmd --state
                查看區域信息: $ firewall-cmd --get-active-zones
                查看指定接口所屬區域:$ firewall-cmd --get-zone-of-interface=eth0
拒絕全部包:# firewall-cmd --panic-on
取消拒絕狀態:# firewall-cmd --panic-off
查看是否拒絕:$ firewall-cmd --query-panic

更新防火牆規則:# firewall-cmd --reload
                            # firewall-cmd --complete-reload
    二者的區別就是第一個無需斷開鏈接,就是firewalld特性之一動態添加規則,第二個須要斷開鏈接,相似重啓服務

將接口添加到區域,默認接口都在public
# firewall-cmd --zone=public --add-interface=eth0
永久生效再加上 --permanent 而後reload防火牆

設置默認接口區域
# firewall-cmd --set-default-zone=public
當即生效無需重啓

打開端口(貌似這個才最經常使用)
查看全部打開的端口:
# firewall-cmd --zone=dmz --list-ports
加入一個端口到區域:
# firewall-cmd --zone=dmz --add-port=8080/tcp
若要永久生效方法同上

打開一個服務,相似於將端口可視化,服務須要在配置文件中添加,/etc/firewalld 目錄下有services文件夾,這個不詳細說了,詳情參考文檔
# firewall-cmd --zone=work --add-service=smtp

移除服務
# firewall-cmd --zone=work --remove-service=smtp

還有端口轉發功能、自定義複雜規則功能、lockdown,因爲還沒用到,之後再學習


#####################################
3.安裝iptable iptable-servicetcp

 

#先檢查是否安裝了iptables

service iptables status

#安裝iptables

yum install -y iptables

#升級iptables

yum update iptables

#安裝iptables-services

yum install iptables-services

 

 
禁用/中止自帶的firewalld服務
#中止firewalld服務
systemctl stop firewalld
#禁用firewalld服務
systemctl mask firewalld

 

 
設置現有規則
#查看iptables現有規則
iptables -L -n
#先容許全部,否則有可能會杯具
iptables -P INPUT ACCEPT
#清空全部默認規則
iptables -F
#清空全部自定義規則
iptables -X
#全部計數器歸0
iptables -Z
#容許來自於lo接口的數據包(本地訪問)
iptables -A INPUT -i lo -j ACCEPT
#開放22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#開放21端口(FTP)
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#開放80端口(HTTP)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#開放443端口(HTTPS)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#容許ping
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
#容許接受本機請求以後的返回數據 RELATED,是爲FTP設置的
iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT
#其餘入站一概丟棄
iptables -P INPUT DROP
#全部出站一概綠燈
iptables -P OUTPUT ACCEPT
#全部轉發一概丟棄
iptables -P FORWARD DROP

 

其餘規則設定
#若是要添加內網ip信任(接受其全部TCP請求)
iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
#過濾全部非以上規則的請求
iptables -P INPUT DROP
#要封停一個IP,使用下面這條命令:
iptables -I INPUT -s ***.***.***.*** -j DROP
#要解封一個IP,使用下面這條命令:
iptables -D INPUT -s ***.***.***.*** -j DROP

 

保存規則設定工具

#保存上述規則
service iptables save

 

開啓iptables服務 學習

#註冊iptables服務
#至關於之前的chkconfig iptables on
systemctl enable iptables.service
#開啓服務
systemctl start iptables.service
#查看狀態
systemctl status iptables.service

 

解決vsftpd在iptables開啓後,沒法使用被動模式的問題

1.首先在/etc/sysconfig/iptables-config中修改或者添加如下內容centos7

#添加如下內容,注意順序不能調換
IPTABLES_MODULES="ip_conntrack_ftp"
IPTABLES_MODULES="ip_nat_ftp"

2.從新設置iptables設置spa

iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT

 

如下爲完整設置腳本rest

#!/bin/sh
iptables -P INPUT ACCEPT
iptables -F
iptables -X
iptables -Z
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
service iptables save
systemctl restart iptables.service

 

 

進一步補充:
redirecting to /bin/systemctl status iptables.service
iptables.service
loaded not found (reason:no such file or directory)code

出現這種錯誤,是由於沒有安裝iptables。blog

相關文章
相關標籤/搜索