iptables規則備份和恢復 | firewalld
備份(另存為)
命令:iptables-save
[root@centos-01 ~]# iptables-save > /tmp/ipt.txt
恢復
[root@centos-01 ~]# iptables-restore < /tmp/ipt.txt
10.20 firewalld的9個zone
先執行以下操作切換至firewalld防火墻:
關閉iptables:
[root@centos-01 ~]# systemctl disable iptables Removed symlink /etc/systemd/system/basic.target.wants/iptables.service. [root@centos-01 ~]# systemctl stop iptables
開啟firewalld:
[root@centos-01 ~]# 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/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service. [root@centos-01 ~]# systemctl start firewalld
註: 此時防火墻的規則已發生改變,可以使用命令iptables -nvL查看。
9種zone:
查看zone類型:
[root@centos-01 ~]# firewall-cmd --get-zones
work drop internal external trusted home dmz public block
關於9種zone的解析:
註: 9個zone中內容分別保存著不同的規則!
查看系統默認的zone:
[root@centos-01 ~]# firewall-cmd --get-default-zone
public
10.21 firewalld關於zone的操作
設置默認的zone
設置默認的zone:
[root@centos-01 ~]# firewall-cmd --set-default-zone=work
success
查看:
[root@centos-01 ~]# firewall-cmd --get-default-zone
work
查看指定網卡的zone:
[root@centos-01 ~]# firewall-cmd --get-zone-of-interface=ens33
public
[root@centos-01 ~]# firewall-cmd --get-zone-of-interface=lo
no zone
給指定網卡增加zone
可以通過以下兩種方法為網卡添加zone:
方法1:
編輯網卡配置文件(復制系統網卡配置文件進行更名)的方法為其添加zone(配置完成後重啟網絡服務,並重新加載firewalld服務:“systemctl restart firewalld”)。
方法2:
[root@centos-01 ~]# firewall-cmd --zone=work --add- interface=ens37
success
[root@centos-01 ~]# firewall-cmd --get-zone-of-interface=ens37
work
給指定網卡更改zone
[root@centos-01 ~]# firewall-cmd --zone=block --change-interface=ens37
success
[root@centos-01 ~]# firewall-cmd --get-zone-of-interface=ens37
block
給指定網卡刪除zone
[root@centos-01 ~]# firewall-cmd --zone=bmz --remove-interface=ens37
The interface is under control of NetworkManager, setting zone to default.
success
[root@centos-01 ~]# firewall-cmd --get-zone-of-interface=ens37
work
查看系統中所有網卡所在的zone
[root@centos-01 ~]# firewall-cmd --get-active-zones
work
interfaces: ens37
public
interfaces: ens33
10.22 firewalld關於service的操作
查看系統所有service
[root@centos-01 ~]# firewall-cmd --get-services
查看當前zone下的service
[root@centos-01 ~]# firewall-cmd --list-services
dhcpv6-client ssh
查看指定zone下的service
[root@centos-01 ~]# firewall-cmd --zone=public --list-services
dhcpv6-client ssh 添加一個服務到某個zone下
臨時添加(配置文件中不存在,重啟會恢復原配置)
[root@centos-01 ~]# firewall-cmd --zone=public --add-service=http
success
[root@centos-01 ~]# firewall-cmd --zone=public --list-services
dhcpv6-client ssh http
永久添加(即,更改配置文件)
[root@centos-01 ~]# firewall-cmd --zone=public --add- service=http --permanent
刪除zone下某服務
臨時刪除
刪除前:
[root@centos-01 ~]# firewall-cmd --zone=public --list-service
ftp dhcpv6-client ssh
[root@centos-01 ~]# firewall-cmd --zone=public --remove- service=ftp
success
刪除後:
[root@centos-01 ~]# firewall-cmd --zone=public --list-service
dhcpv6-client ssh
永久刪除
[root@centos-01 ~]# firewall-cmd --zone=public --list-service
ftp dhcpv6-client http ssh
[root@centos-01 ~]# firewall-cmd --zone=public --remove- service=ftp --permanent
success
[root@centos-01 ~]# firewall-cmd --reload
success
[root@centos-01 ~]# firewall-cmd --zone=public --list-service
dhcpv6-client http ssh
配置文件
zone的系統配置文件位置:/etc/firewalld/zones/
[root@centos-01 ~]# ls /etc/firewalld/zones/
public.xml public.xml.old
[root@centos-01 ~]# 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="http"/>
<service name="ssh"/>
</zone>
說明: public.xml.old相當於一個備份文件,每次編輯public.xml時,系統會自動將原public.xml內容備份到public.xml.old。
zone配置文件模板
[root@centos-01 ~]# ls /usr/lib/firewalld/zones/
block.xml drop.xml home.xml public.xml work.xml
dmz.xml external.xml internal.xml trusted.xml
firewalld內各項服務的配置文件模板
[root@centos-01 ~]# ls /usr/lib/firewalld/
icmptypes ipsets services xmlschema zones
註: 每次編輯配置文件後需要重新加載(reload)firewall-cmd才生效。
應用
需求: ftp服務自定義端口1121,需要在work zone下面放行ftp。
方法:
步驟一:復制ftp的配置文件到/etc/firewalld/services/
[root@centos-01 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/
步驟二:編輯該文件,將port="21"改為port="1121"
[root@centos-01 ~]# vim /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>
步驟三:復制workzone的配置文件到/etc/firewalld/zones/
[root@centos-01 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
步驟四:編輯該文件,增加“<service name="ftp"/>”
[root@centos-01 ~]# vim /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"/>
</zone>
步驟五:重新加載
[root@centos-01 ~]# firewall-cmd --reload
success
Finished!
iptables規則備份和恢復 | firewalld