1. 程式人生 > >iptables備份 firewalld9個zone firewalld操作zone service

iptables備份 firewalld9個zone firewalld操作zone service

Linux

iptables規則備份和恢復

保存和備份iptables規則

service iptables save //會把規則保存到/etc/sysconfig/iptables
service iptables save

技術分享圖片

想要備份到指定文件中可以使用如下命令:(把iptables規則備份到/tmp/ipt.txt文件中)
iptables-save > /tmp/ipt.txt

技術分享圖片

我們cat一下看一下
cat /tmp/ipt.txt

技術分享圖片

想要恢復指定文件裏的規則使用如下命令:(把備份的/tmp/ipt.txt恢復到iptables裏面去)
iptables-restore < /tmp/ipt.txt

技術分享圖片

使用service iptables save保存在/etc/sysconfig/iptables裏的規則可以開啟自動讀取,而保存在其他文件裏的規則只有恢復的時候才會用到。

firewalld的9個zone

下面學習firewalld,在7以後的系統中用的是firewalld防火墻。

我們之前把firewalld禁掉了開啟了iptables,現在要把iptables禁掉開啟firewalld。

systemctl disable iptables
 systemctl stop iptables
 systemctl enable firewalld
 systemctl start firewalld

技術分享圖片

firewalld的9個zone默認有9個zone,zone是firewalld的單位,firewalld默認使用public zone,每個zone就好比一個規則集(就是自帶一些規則)

查看所有的zone使用如下命令
firewall-cmd --get-zones

技術分享圖片

查看默認的zone使用如下命令
firewall-cmd --get-default-zone

技術分享圖片

所有zone介紹

技術分享圖片

firewalld關設定默認zone於zone的操作

設定默認zone(設置為work)
firewall-cmd --set-default-zone=work

技術分享圖片

查看指定網卡使用的哪個zone(查詢網卡ens33)
firewall-cmd --get-zone-of-interface=ens33

技術分享圖片

如果添加的網卡沒有zone,可以做一個設置,將原來的網卡配置文件復制一份並改名成新添加的網卡名,然後寫一下配置文件,然後重啟網絡服務,然後在重新加載一下firewalld,使用 systemctl restart firewalld 命令,然後再查新添加的網卡有沒有zone。

進入網卡配置目錄進行復制
cd /etc/sysconfig/network-scripts

重新加載firewalld
systemctl restart firewalld

如果還沒有我們就來增加一下。給指定網卡設置zone
firewall-cmd --zone=public --add-interface=lo

我們還可以給網卡更改zone。針對網卡更改zone。(zone=你需要設置的zone,lo為你要設置的網卡名)
firewall-cmd --zone=dmz --change-interface=lo

我們還可以針對網卡刪除zone,刪除了之後就變成了默認zone(zone=你需要刪除的zone,lo為你要刪除的網卡名)
firewall-cmd --zone=dmz --remove-interface=lo

還可以查看所有網卡所在的zone
firewall-cmd --get-active-zones

firewalld關於service的操作

service就是zone下面的子單元,可以理解成他是指定的端口,因為防火墻就是針對某個端口進行限制。

查看所有的service
firewall-cmd --get-services

技術分享圖片

查看當前zone裏有哪些service.
firewall-cmd --list-services

技術分享圖片

查看指定zone裏有哪些service(zone=你需要設置的zone)
firewall-cmd --zone=public --list-services

把http增加到public zone下面
firewall-cmd --zone=public --add-service=http

技術分享圖片

添加後只是添加到了內存裏,還需要保存到配置文件裏去,不然重啟之後就還原了,(public你需要設置的zone,http為你要設置的網卡名)保存後會在/etc/firewalld/zones目錄下面生成配置文件
firewall-cmd --zone=public --add-service=http --permanent

技術分享圖片

zone的配置文件模板
ls /usr/lib/firewalld/zones/

技術分享圖片

下面我們來介紹一個案例
需求:ftp服務自定義端口1121,需要在work zone下面放行ftp

操作方法:
把ftp配置文件模板拷貝到/etc/firewalld/services下去
cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services

技術分享圖片

iptables備份 firewalld9個zone firewalld操作zone service