iptables规则的备份和恢复
service iptables save //会把规则这个命令保存到配置文件/etc/sysconfig/iptables中。但是有是有我们想保存在另外一个文件中。我们可以用命令
iptables-save >/文件名。
比如我们想把iptables的规则保存在/tmp/ipt.txt中,我们就可以这样做
[root@linletao-001 ~]# iptables-save > /tmp/ipt.txt
[root@linletao-001 ~]# cat /tmp/ipt.txtGenerated by iptables-save v1.4.21 on Thu Mar 22 22:25:05 2018
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [168:18528]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMITCompleted on Thu Mar 22 22:25:05 2018
这样就可以了。
恢复则用命令iptables restore < /文件名
比如我们想将tmp/ipt.txt中的内容恢复到iptables中。
[root@linletao-001 ~]# iptables-restore < /tmp/ipt.txt
这样就可以了。
备份的规则只有我们恢复时候能用到他,如果想要设备启动时加载一些规则的话,还是要放到配置文件/etc/sysconfig/iptables中,然后我们用service iptables save保存。firewalled(centos7以及以后版本的防火墙)
由于我们刚才关闭了firewalled,所以首先我们打开firewalled。
[root@linletao-001 ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@linletao-001 ~]# systemctl stop iptables
[root@linletao-001 ~]# 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@linletao-001 ~]# systemctl start firewalld打开完后,我们查看一下iptables的规则
[root@linletao-001 ~]# iptables -nvL |head -5
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination 
116 7820 ACCEPT all --   0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
0 0 ACCEPT all -- lo  0.0.0.0/0 0.0.0.0/0 
19 2276 INPUT_direct all --  * 0.0.0.0/0 0.0.0.0/0 (以下省略)
这些都是firewalled自带的规则。nat也同样带有规则。链也比较多。firewalld默认自带有9个zone,zone是firewalld的一个单位,默认zone为public。每个zone都好比一个规则集。
查看所有zone:firewalld-cmd --get-zones
[root@linletao-001 ~]# firewall-cmd --get-zones
block dmz drop external home internal public(默认的) trusted work
一共有9个zone,其中public为默认的zone查看默认的zone:firewall-cmd --get-default-zone
[root@linletao-001 ~]# firewall-cmd --get-default-zone
public9个zone的区别
设定默认的zone
比如说我们想将默认的zone改为work
[root@linletao-001 ~]# firewall-cmd --set-default-zone=work
success
这样就改好了。然后我们查看一下
[root@linletao-001 ~]# firewall-cmd --get-default-zone
work查看指定网卡
firewall-cmd --get-zone-of-interface=ens33
[root@linletao-001 ~]# firewall-cmd --get-zone-of-interface=ens33
work重启firewall服务
systemctl restart firewalld给指定的网卡设置zone
我们将网卡lo的zone设置成public
[root@linletao-001 ~]# firewall-cmd --zone=public --add-interface=lo
success
[root@linletao-001 ~]# firewall-cmd --get-zone-of-interface=lo
public针对网卡更改zone
我们还是用lo网卡做实验
[root@linletao-001 ~]# firewall-cmd --zone=dmz --change-interface=lo
success
[root@linletao-001 ~]# firewall-cmd --get-zone-of-interface=lo 
dmz针对网卡删除zone
我们还是用lo网卡做实验
[root@linletao-001 ~]# firewall-cmd --zone=dmz --remove-interface=lo 
然后查看,发现他变成了默认的zone查看系统所有网卡所在的zone
[root@linletao-001 ~]# firewall-cmd --get-active-zones 
work
interfaces: ens33这些命令是关于zone的操作。
zone中的service
centos中有9种zone,每一个zone都使用了不同的service,而service就是针对一个服务(端口)做的iptables规则。查看系统中所有的service[root@linletao-001 ~]# firewall-cmd --get-service
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap ldaps libvirt libvirt-tls managesieve mdns mosh mountd ms-wbt mssql mysql nfs nrpe ntp open*** ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server查看当前的zone 
firewall-cmd --list-service
[root@linletao-001 ~]# firewall-cmd --list-service
work查看当前zeon中的service
firewall-cmd --list-service
[root@linletao-001 ~]# firewall-cmd --list-service
ssh dhcpv6-client查看指定的zone有哪些service。
firewall-cmd --zone=zone的名字 --list-service
比如我们要查一下public下的service
[root@linletao-001 ~]# firewall-cmd --zone=public --list-service
ssh dhcpv6-client增加某一项service到指定的zone中去
firewall-cmd --zone=指定的zone --add-service=指定的service
比如我们要将http添加到public中。
[root@linletao-001 ~]# firewall-cmd --zone=public --add-service=http
success
然后我们查询一下
[root@linletao-001 ~]# firewall-cmd --zone=public --list-service 
ssh dhcpv6-client http
发现http已经添加到public。我们现在增加的这些service只是储存在内存中,如果终端重启的话这些就会消失。所以如果想永久有效,就要将它写入配置文件中去,更改配置文件。如果想更改配置文件,只要在命令的最后增加一个选项“--permanent”。比如我们想把http写入public的配置文件中。
[root@linletao-001 ~]# firewall-cmd --zone=public --add-service=http --permanent
success
然后我们去配置文件中查看是否添加成功。配置文件的路径为/etc/firewalld/zones/
[root@linletao-001 ~]# 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="ssh"/>
<service name="dhcpv6-client"/>
<service name="http"/>(新添加的service)
</zone>有时候还有这样一种情况
[root@linletao-001 ~]# firewall-cmd --zone=public --add-service=ftp --add-service=ftp
Warning: ALREADY_ENABLED: 'ftp' already in 'public'
说某项服务已经存在了,但是配置文件中没有,那是因为我们没有将它写入配置文件。如果想变成永久的,就要在后面添加参数 “--permanent”。每当我们永久保存一个zone或者service时他都会将以前的配置文件同时备份,并在文件名后面加上.old
[root@linletao-001 ~]# cat /etc/firewalld/zones/public.xml
public.xml public.xml.old同时这些zone或者service也有自己的模板,他们都在/usr/lib/firewalld/zone中
[root@linletao-001 ~]# ls /usr/lib/firewalld/zones
block.xml dmz.xml drop.xml external.xml home.xml internal.xml public.xml trusted.xml work.xml
以上就是zone的模板。[root@linletao-001 ~]# ls /usr/lib/firewalld/services
amanda-client.xml dropbox-lansync.xml kadmin.xml nrpe.xml quassel.xml ssh.xml
amanda-k5-client.xml elasticsearch.xml kerberos.xml ntp.xml radius.xml synergy.xml
bacula-client.xml freeipa-ldaps.xml kibana.xml open***.xml RH-Satellite-6.xml syslog-tls.xml
bacula.xml freeipa-ldap.xml klogin.xml ovirt-imageio.xml rpc-bind.xml syslog.xml
bitcoin-rpc.xml freeipa-replication.xml kpasswd.xml ovirt-storageconsole.xml rsh.xml telnet.xml
bitcoin-testnet-rpc.xml freeipa-trust.xml kshell.xml ovirt-vmconsole.xml rsyncd.xml tftp-client.xml
bitcoin-testnet.xml ftp.xml ldaps.xml pmcd.xml samba-client.xml tftp.xml
bitcoin.xml ganglia-client.xml ldap.xml pmproxy.xml samba.xml tinc.xml
ceph-mon.xml ganglia-master.xml libvirt-tls.xml pmwebapis.xml sane.xml tor-socks.xml
ceph.xml high-availability.xml libvirt.xml pmwebapi.xml sips.xml transmission-client.xml
cfengine.xml https.xml managesieve.xml pop3s.xml sip.xml vdsm.xml
condor-collector.xml http.xml mdns.xml pop3.xml smtp-submission.xml vnc-server.xml
ctdb.xml imaps.xml mosh.xml postgresql.xml smtps.xml wbem-https.xml
dhcpv6-client.xml imap.xml mountd.xml privoxy.xml smtp.xml xmpp-bosh.xml
dhcpv6.xml ipp-client.xml mssql.xml proxy-dhcp.xml snmptrap.xml xmpp-client.xml
dhcp.xml ipp.xml ms-wbt.xml ptp.xml snmp.xml xmpp-local.xml
dns.xml ipsec.xml mysql.xml pulseaudio.xml spideroak-lansync.xml xmpp-server.xml
docker-registry.xml iscsi-target.xml nfs.xml puppetmaster.xml squid.xml
以上是service的模板比如我们现在有一个需求,就是将ftp服务添加到1121端口,并在work zone中放行。
我们利用配置文件的方法进行操作。
首先将ftp的文件拷贝到/etc/firewalld/services中。
[root@linletao-001 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/
然后我们去编辑它
[root@linletao-001 ~]# 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 t
he vsftpd package installed for this option to be useful.</description>
<port protocol="tcp" port="1121"/>(这是端口,只要将它改成我们想要的端口号就可以)
<module name="nf_conntrack_ftp"/>
</service>然后我们将/usr/lib/firewalld/zones/work.xml 放到/etc/firewalld/zones/中
然后再去编辑/etc/firewalld/zones/work.xml
[root@linletao-001 ~]# 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 a
re accepted.</description>
<service name="ssh"/>
<service name="dhcpv6-client"/>
<service name="ftp"/>(新增加的ftp)
</zone>
这样配置文件就操作完成了。然后我们重新加载一下。
firewall-cmd --reload
[root@linletao-001 ~]# firewall-cmd --reload
success然后我们再检查一下
[root@linletao-001 ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client ftp
这样就完成了。

https://blog.51cto.com/13067688/2090802