10.19 iptables規則備份和恢復linux
10.20 firewalld的9個zonecentos
10.21 firewalld關於zone的操做服務器
10.22 firewalld關於service的操做網絡
10.19 iptables規則備份和恢復:ssh
~1. 保存和備份iptables規則tcp
~2. service iptables save 會把規則保存到 /etc/sysconfig/iptablesthis
~3. 把iptables規則備份到my.ipt文件中centos7
~4. iptables-save > my.iptrest
iptables-save重定向到這個文件中。名字能夠隨便起,也能夠是.txt文件server
~5. 恢復剛纔備份的規則
備份的規則,可能只有在恢復的時候能用到它。可是咱們一重啓服務器就想要加載規則,那麼最好把規則備份到默認的 /etc/sysconfig/iptables裏
~6. iptables-restore < my.ipt
跟上面~4. 備份的重定向正好相反
----------------------------------------------------------------------------------------------------------------------------------------------------
10.20 firewalld的9個zone:
filewalld是centos7以及之後得版本要用到的防火牆機制。是一個新的趨勢
~1. 打開firewalld
由於咱們以前作實驗,禁掉了firewalld,全部咱們在把他打開。執行~2 3 4 5 就能夠了
~2. systemctl disable iptables
~3. systemctl stop iptables
連續執行~2 3 禁掉iptables
~4. systemctl enable firewalld
~5. systemctl start firewalld
連續執行~4 5 打開firewalld
咱們在打開firewalld後,再iptables -nvL,會發現跟netfilter的規則不太同樣,會比netfilter多不少,由於他包含了nat裏的一些規則
~6. firewalld 默認有9個zone
firewalld的用法,zone是firewalld的單位。默認的zone是public,也就是~7. 。每一個zone比如是一個規則集(規則集就是,好比咱們放開了22端口、80端口,關閉了一些端口。這些規則就是一個規則集)
~7. 默認zone爲public
~8. firewall-cmd --get-zones 查看全部zone (注意是firewall,敲命令是沒有d)
[root@axinlinux-01 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
這就是他的9個zone
~9. firewall-cmd --get-default-zone 查看默認zone (注意是firewall,敲命令是沒有d)
[root@axinlinux-01 ~]# firewall-cmd --get-default-zone
public
默認的就是public,也就是~7.
下圖爲firewalld的9個zone的解釋
----------------------------------------------------------------------------------------------------------------------------------------------------
10.21 firewalld關於zone的操做:
firewall 的幾個命令:
~1. firewall-cmd --set-default-zone=work 設定默認zone
等於號後面跟想更改的默認zone
~2. firewall-cmd --get-zone-of-interface=ens33 查指定網卡,屬於那個zone
阿鑫在查看ens37的時候發現沒有zone,作了一些操做,詳見實例2.
~3. firewall-cmd --zone=public --add-interface=lo 給指定網卡設置zone
~4. firewall-cmd --zone=dmz --change-interface=lo 針對網卡更改zone
~5. firewall-cmd --zone=dmz --remove-interface=lo 針對網卡刪除zone
好比,把lo這個網卡設置成了dmz,可是要想把dmz裏的lo給他刪掉
須要注意的是,把原有的zone裏的網卡刪掉,他就變成默認的zone了
~6. firewall-cmd --get-active-zones 查看系統全部網卡所在的zone
實例:
1.
[root@axinlinux-01 ~]# firewall-cmd --set-default-zone=work
success
[root@axinlinux-01 ~]# firewall-cmd --get-default-zone
work
2.
[root@axinlinux-01 ~]# cd /etc/sysconfig/network-scripts/
[root@axinlinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens37 複製一份ens33爲ens37
[root@axinlinux-01 network-scripts]# vi !$ 修改一下IP,又刪除了網關和dns1
vi ifcfg-ens37
[root@axinlinux-01 network-scripts]# systemctl restart network.service 重啓一下網絡服務
[root@axinlinux-01 network-scripts]# systemctl restart firewalld 重啓防火牆
[root@axinlinux-01 network-scripts]# firewall-cmd --get-zone-of-interface=ens37 發現仍是沒有,尷尬了
no zone
[root@axinlinux-01 network-scripts]# firewall-cmd --zone=public --add-interface=ens37 有執行了3.,纔有的
success
[root@axinlinux-01 network-scripts]# firewall-cmd --get-zone-of-interface=ens37
public
5.
[root@axinlinux-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
no zone
[root@axinlinux-01 network-scripts]# firewall-cmd --zone=dmz --add-interface=lo
success
[root@axinlinux-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
dmz
[root@axinlinux-01 network-scripts]# firewall-cmd --zone=dmz --remove-interface=lo
success
[root@axinlinux-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
no zone
6.
[root@axinlinux-01 network-scripts]# firewall-cmd --get-active-zones
work
interfaces: ens33
----------------------------------------------------------------------------------------------------------------------------------------------------
10.22 firewalld關於service的操做:
service是zone下面的一個子單位,可理解爲他是一個指定一個端口。由於防火牆無外乎就是對某一個端口作限制
好比,http他操做的是80端口。https操做的是43端口。ssh操做的是22端口
service的操做:
~1. firewall-cmd --get-service 查看全部的service
~2. firewall-cmd --list-service 查看當前zone下有哪些service
指定zone查看有哪些service。 firewall-cmd --zone=public --list-service
~3. firewall-cmd --zone=public --add-service=http 把http增長到public zone下面
這樣僅僅只是在內存中增長了,要改爲永久的須要更改配置文件 ~6.
~4. firewall-cmd --zone=public --remove-service=http
把http在public中去除,與 3 相對應的
~5. ls /usr/lib/firewalld/zones/ zone的配置文件模板
有9個,9個zone9個模板
更改後的配置文件也會在這裏存放。每次只有--permanent纔會加入。
而咱們 /etc/firewalld/zones/下,是咱們系統etc下的firewalld服務所用到的配置文件,每次改完並永久保存,他都會把舊的作一個備份並加上後綴.old
~6. firewall-cmd --zone=public --add-service=http --permanent 更改配置文件,以後會 在/etc/firewalld/zones目錄下生成配置文件
~7. 需求:(使用配置文件的方式進行操做)
(案例)
ftp服務自定義端口1121,須要在work zone下面放行ftp
8. cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services (作實驗因此cp一份)
把ftp的文件先cp到etc下面去
9. vi /etc/firewalld/services/ftp.xml 把21改成1121
而後編輯他,把21端口改成1121端口
10. cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/ (作實驗因此cp一份)
11. vi /etc/firewalld/zones/work.xml 增長一行
複製上面的一行格式,在雙引號裏面的改爲ftp。也就是12的操做
12. <service name="ftp"/>
13. firewall-cmd --reload 從新加載
14. firewall-cmd --zone=work --list-services
指定一個zone,查看他的service
檢查以上操做,是否成功
實例:
7.
[root@axinlinux-01 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
[root@axinlinux-01 ~]# vi /etc/firewalld/services/ftp.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>FTP</short>
<description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description>
<port protocol="tcp" port="1121"/> 就改這
<module name="nf_conntrack_ftp"/>
</service>
[root@axinlinux-01 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@axinlinux-01 ~]# vi /etc/firewalld/zones/work.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Work</short>
<description>For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
<service name="ssh"/>
<service name="dhcpv6-client"/>
<service name="ftp"/> 複製上面的一行(格式),把雙引號裏面的改爲 ftp
</zone>
[root@axinlinux-01 ~]# firewall-cmd --reload
success
而後,咱們在檢查一下
[root@axinlinux-01 ~]# firewall-cmd --zone=work --list-service
ssh dhcpv6-client ftp work zone下有ftp