命令:iptables-savevim
[root@1 ~]# iptables-save > /tmp/ipt.txt
[root@1 ~]# iptables-restore < /tmp/ipt.txt
先執行如下操做切換至firewalld防火牆:網絡
關閉iptables: [root@1 ~]# systemctl disable iptables Removed symlink /etc/systemd/system/basic.target.wants/iptables.service. [root@adai003 ~]# systemctl stop iptables 開啓firewalld: [root@1 ~]# 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@1 ~]# systemctl start firewalld
注: 此時防火牆的規則已發生改變,能夠使用命令iptables -nvL查看。ssh
[root@1 ~]# firewall-cmd --get-zones work drop internal external trusted home dmz public block
[root@1 ~]# firewall-cmd --get-default-zone public
設置默認的zone: [root@1 ~]# firewall-cmd --set-default-zone=work success 查看: [root@1 ~]# firewall-cmd --get-default-zone work
[root@1 ~]# firewall-cmd --get-zone-of-interface=ens33 public [root@1 ~]# firewall-cmd --get-zone-of-interface=lo no zone
能夠經過如下兩種方法爲網卡添加zone:tcp
方法1:this
編輯網卡配置文件(複製系統網卡配置文件進行改名)的方法爲其添加zone(配置完成後重啓網絡服務,並從新加載firewalld服務:「systemctl restart firewalld」)。spa
方法2:rest
[root@1 ~]# firewall-cmd --zone=work --add-interface=ens37 success [root@1 ~]# firewall-cmd --get-zone-of-interface=ens37 work
[root@1 ~]# firewall-cmd --zone=block --change-interface=ens37 success [root@1 ~]# firewall-cmd --get-zone-of-interface=ens37 block
[root@1 ~]# firewall-cmd --zone=bmz --remove-interface=ens37 The interface is under control of NetworkManager, setting zone to default. success [root@1 ~]# firewall-cmd --get-zone-of-interface=ens37 work
[root@1 ~]# firewall-cmd --get-active-zones work interfaces: ens37 public interfaces: ens33
[root@1 ~]# firewall-cmd --get-services
[root@1 ~]# firewall-cmd --list-services dhcpv6-client ssh
[root@1 ~]# firewall-cmd --zone=public --list-services dhcpv6-client ssh
[root@1 ~]# firewall-cmd --zone=public --add-service=http success [root@1 ~]# firewall-cmd --zone=public --list-services dhcpv6-client ssh http
[root@1 ~]# firewall-cmd --zone=public --add-service=http --permanent
刪除前: [root@1 ~]# firewall-cmd --zone=public --list-service ftp dhcpv6-client ssh [root@1 ~]# firewall-cmd --zone=public --remove-service=ftp success 刪除後: [root@1 ~]# firewall-cmd --zone=public --list-service dhcpv6-client ssh
[root@1 ~]# firewall-cmd --zone=public --list-service ftp dhcpv6-client http ssh [root@1 ~]# firewall-cmd --zone=public --remove-service=ftp --permanent success [root@1 ~]# firewall-cmd --reload success [root@1 ~]# firewall-cmd --zone=public --list-service dhcpv6-client http ssh
[root@1 ~]# ls /etc/firewalld/zones/ public.xml public.xml.old [root@1 ~]# 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。code
[root@1 ~]# ls /usr/lib/firewalld/zones/ block.xml drop.xml home.xml public.xml work.xml dmz.xml external.xml internal.xml trusted.xml
[root@1 ~]# ls /usr/lib/firewalld/ icmptypes ipsets services xmlschema zones
注: 每次編輯配置文件後須要從新加載(reload)firewall-cmd才生效。server
需求:
ftp服務自定義端口1121,須要在work zone下面放行ftp。xml
方法:
步驟一:複製ftp的配置文件到/etc/firewalld/services/ [root@1 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/ 步驟二:編輯該文件,將port="21"改成port="1121" [root@1 ~]# 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@1 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/ 步驟四:編輯該文件,增長「<service name="ftp"/>」 [root@1 ~]# 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@1 ~]# firewall-cmd --reload success Finished!