入門一班20181029

10月29日任務

10.19 iptables規則備份和恢復
10.20 firewalld的9個zone
10.21 firewalld關於zone的操作
10.22 firewalld關於service的操作



iptables的規則備份和恢復


image.png


service iptables save會把配置保存到配置文件中 /etc/sysconfig/iptables


如果不想保存在這 可以使用


iptables-save > 文件路徑

image.png


如果想恢復之前的iptables表,就使用

iptables-restore <文件路徑 

iptables  -t nat -F 是將nat表的規則清空,然後查看無規則。接着restore後查看, 規則重新出現。看下圖。

image.png



Linux防火牆 firewalled

image.png

firealled是之後centos7以及以後版本可能會使用的一個工具,所以需要將iptables關閉,打開firewalld

image.png

image.png

相對來說 firewalld自帶的規則相對iptables比較多。以下就是firewalld自帶的規則。不僅僅是filter 也包括nat表,鏈也比較多。

image.png


firewalld默認是9個zone 默認zone爲public 。一個zone就是一個規則集。

firewall-cmd --get-zones 查看所有的zone

image.png


firewall-cmd --get-default-zone查看默認的zone

image.png


不同zone的區別

image.png


firewalld關於zone的操作

image.png

默認的zone是可以修改的 

firewall-cmd --set-default-zone=work 將work更改爲默認的zone

image.png

firewall-cmd --get-zone-of-interface=ens33 查網卡的zone。如果no zone的話可以將ens33的配置文件拷貝一份給37然後修改對應的網絡文件。然後重啓網絡服務以及重啓firewalld。

image.png


也可以手動給指定的網卡設置zone

firewall-cmd --zone=public --add-interface=lo 給指定網卡設置zone

image.png

但是如果需要修改成功的話,需要先將NetworkManager關閉。systemctl stop NetworkManager why?

image.png


firewall-cmd --zone=dmz --change-interface=lo 

針對網卡更改zone

image.png

刪除當前的zone

firewall-cmd --zone=dmz--remove-interface=ens37

注意還是牽涉到一個networkmanager的開啓問題。why

image.png


查看所有的生效的網卡

firewall-cmd --get-active-zones

image.png




firewalld service操作


所謂的防火牆就是針對端口做一些限制,service可以理解爲是zone下面的端口的操作。

比如http 是操作80端口 ssh 是21端口 https是43端口 

image.png


查看所有的service

firewall-cmd --get-services

image.png


查看當前默認的zone下都有哪些服務呢?

firewall-cmd --list-services

image.png

如果是查找對應的zone的service呢?

firewall-cmd --zone=public --list-services

image.png


在某個zone下面添加service,比如下面就是給public的zone添加http服務

firewall-cmd --zone=public --add-service=http

然後 firewall-cmd --zone=public --list-services

image.png

但是,目前只是在內存中給zone添加了service,如果想寫入配置文件,需要添加一個選項 --permanent 

firewall-cmd --zone=public --add-service=http --permanent

之後會在/etc/firewalld/zones目錄下面生成配置文件

image.png

有意思的地方是,配置文件有更新的時候,舊的不會覆蓋而是會備份。

image.png

同樣 service也是有配置文件的,配置文件的目錄是 /etc/firewalld/services/

在沒有更改配置文件之前這裏面是空的

image.png

實際上這些xml格式的保存文件是有模板的,保存在/ usr/lib/firewalld/zone下

image.png

同樣 service也是有模板的 保存在/ usr/lib/firewalld/services下

image.png


做一個小練習。

需求:ftp默認端口21更改爲1121,需要在work zone下進行放行ftp

  1. 更改ftp服務。步驟,將ftp模板拷貝到配置文件路徑的service目錄下。然後編輯把21端口改成1121

    cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/

     image.png

    image.png

2. 將ftp服務放到work zone下。步驟 把work zone的模板文件拷貝到配置目錄下,然後修改work.xml文件將ftp服務添加進去

image.png

image.png


3. 重新加載。 firewall-cmd --reload

image.png


in sum,

防火牆 firewall 下主要有zone和service,zone下的service有哪些service,那就說明給哪些service 放行了。

也可以自定義service,比如進去ftp.xml進去改port


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