iptables規則備份和恢復、firewalld相關

iptables規則備份和恢復

  • 介紹:以前說道,咱們設定的防火牆規則只保存在內存中,重啓失效。那麼怎麼保存規則呢 以下
  • 保存規則命令:
    • 這裏能看到出現了一個路徑,這就是咱們的規則所保存的路徑。
    • 之後要是遇到須要備份防火牆規則,只要複製這個文件的副本就好了
[root@centos001 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  肯定  ]
  • 保存、恢復規則至指定的地方
[root@centos001 ~]#  iptables-save > /tmp/ipt.txt  //保存
[root@centos001 ~]# iptables-restore < /tmp/ipt.txt //恢復

firewalld的9個zone

  • 準備 :
    以前咱們關閉了firewalld,如今要開啓,因此要把iptables先禁用掉
[root@centos001 ~]# systemctl disable iptables
[root@centos001 ~]# systemctl stop iptables
[root@centos001 ~]# systemctl enable firewalld
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@centos001 ~]# systemctl start firewalld
  • 介紹:
    • 咱們能夠先查看一下firewalld的規則,用iptables -nvL(注意最後一個L是大寫)查看其默認規則
    • firewalld的規則有兩個基礎概念,分別是zone(能夠看成是個單位)和service,每一個zone裏面有不一樣的iptables規則,默認一共9個zone,
      而centos7默認的zone爲public
      • 每一個zone都是至關於一個規則集(就是裏面有屬於它本身的規則)
  • 命令:查看全部zone
[root@centos001 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
  • 命令:查詢系統默認的zone
[root@centos001 ~]# firewall-cmd --get-default-zone
public                     //這裏能看到默認的zone是public
  • zone的解釋

firewalld關於zone的操做

  • 設定默認的zone是什麼
[root@centos001 ~]# firewall-cmd --set-default-zone=work//設定默認zone是,並查看
success
[root@centos001 ~]# firewall-cmd --get-default-zone
work
  • 查看網卡的zone
[root@centos001 ~]# firewall-cmd --get-zone-of-interface=lo
no zone
  • 當zone沒有的時候
[root@centos001 ~]# cd /etc/sysconfig/network-scripts/      //1.複製配置文件到網卡
[root@centos001 network-scripts]# ls
ifcfg-e        ifdown-ib      ifdown-Team      ifup-ippp    ifup-sit
ifcfg-ens      ifdown-ippp    ifdown-TeamPort  ifup-ipv6    ifup-Team
ifcfg-ens33    ifdown-ipv6    ifdown-tunnel    ifup-isdn    ifup-TeamPort
ifcfg-ens33:0  ifdown-isdn    ifup             ifup-plip    ifup-tunnel
ifcfg-lo       ifdown-post    ifup-aliases     ifup-plusb   ifup-wireless
ifdown         ifdown-ppp     ifup-bnep        ifup-post    init.ipv6-global
ifdown-bnep    ifdown-routes  ifup-eth         ifup-ppp     network-functions
ifdown-eth     ifdown-sit     ifup-ib          ifup-routes  network-functions-ipv6
[root@centos001 network-scripts]# cp ifcfg-ens33 ifcfg-ens37                 
[root@centos001 network-scripts]# vi /etc/sysconfig/network-scripts/ifcfg-ens37     //2.配置下這個文件
[root@centos001 network-scripts]# uuidgen ens37           //這裏是查詢uuid 
0868ef08-a957-44d8-9554-c4966a7f0bf5
[root@centos001 network-scripts]# vi /etc/sysconfig/network-scripts/ifcfg-ens37
[root@centos001 network-scripts]# systemctl restart network.service          //3.重啓網絡服務
Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.
[root@centos001 network-scripts]# systemctl restart firewalld   //4.重啓firewalld服務
[root@centos001 network-scripts]# firewall-cmd --get-zone-of-interface=ens37      //5.從新運行查看網卡的zone
no zone              //這裏出錯了 坑能是咱們網絡環境的問題
  • 給指定的網卡設置zone
[root@centos001 network-scripts]# firewall-cmd --zone=public --add-interface=lo //設定並查看
success
[root@centos001 network-scripts]# firewall-cmd --get-zone-of-interface=lo
public
  • 針對指定網卡修改zone
[root@centos001 network-scripts]# firewall-cmd --zone=block --change-interface=lo //修改並查看
success
[root@centos001 network-scripts]# firewall-cmd --get-zone-of-interface=lo
block
  • 針對指定網卡刪除zone
    • 這裏最後顯示no zone的緣由是:沒有開啓NetworkManager服務
[root@centos001 network-scripts]# firewall-cmd --zone=block  --remove-interface=lo  
success
[root@centos001 network-scripts]# firewall-cmd --get-zone-of-interface=lo
no zone
  • 查看系統全部網卡所在的zone
firewall-cmd --get-active-zones  //查看系統全部網卡所在的zone

firewalld關於service的操做

  • 介紹:什麼是service 其實之因此有9中zone,其緣由是由於每個zone裏面都是用了不一樣的service,而service就是針對一個服務(端口)作的iptables規則。

service的用法:

  • 列出當前系統裏的全部service
firewall-cmd --get-services  查看全部的servies

-查看當前zone下有哪些servicecentos

[root@centos001 network-scripts]# firewall-cmd --get-default-zone //查看當前的zone是什麼
work
[root@centos001 network-scripts]# firewall-cmd --list-services
ssh dhcpv6-client
  • 查看指定zone下的service
[root@centos001 network-scripts]# firewall-cmd --zone=public --list-service
dhcpv6-client ssh http
  • 把http增長到public zone下面
    • 這裏設定的只會保存在內存中,下個命令將介紹什麼保存至配置文件
[root@centos001 network-scripts]# firewall-cmd --zone=public --add-service=http
success
[root@centos001 network-scripts]# firewall-cmd --zone=public --list-service
dhcpv6-client ssh http
  • 將添加的service 保存至配置文件
    firewall-cmd --zone=public --add-service=http --permanent
    更改配置文件,以後會在/etc/firewalld/zones目錄下面生成配置文件
[root@centos001 network-scripts]# firewall-cmd --zone=public --add-service=http --permanent 
success
[root@centos001 network-scripts]# ls /etc/firewalld/zones/public.xml  //保存的路徑
/etc/firewalld/zones/public.xml
[root@centos001 network-scripts]# ls /etc/firewalld/zones/
public.xml  public.xml.old
[root@centos001 network-scripts]# cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="dhcpv6-client"/>
  <service name="ssh"/>
  <service name="http"/>      //能看到咱們添加的service
</zone>
  • zone的配置文件模板
ls /usr/lib/firewalld/zones/

需求:ftp服務自定義端口1121,須要在work zone下面放行ftp

  • 首先要將ftp的服務作一下更改,而後吧ftp添加到work zone
  • 複製模版一
cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
  • 修改配置文件
    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"/>         //這裏修改成1121
  <module name="nf_conntrack_ftp"/>
</service>
  • 複製模版二
cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
  • 修改配置文件 vi /etc/firewalld/zones/work.xml 增長一行<service name="ftp"/>
<?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"/>
</zone>
  • 從新加載
[root@centos001 ~]# firewall-cmd --reload 
success
  • 查看結果
[root@centos001 ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client ftp   //新添加的ftp
相關文章
相關標籤/搜索