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開機啟動

systemctl status firewalld                #  查看防火牆狀態


 

2.防火牆詳述



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

1、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從功能上都需要自定義允許連接,具體使用上的區別還需高人指點

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

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

4、配置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-service

 

#先檢查是否安裝了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中修改或者添加以下內容

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

2.重新設置iptables設置

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

 

以下為完整設置腳本

#!/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)

出現這種錯誤,是因為沒有安裝iptables。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM