iptables规则备份和恢复

保存和备份iptables规则

service iptables save //会把规则保存到/etc/sysconfig/iptables service iptables save

想要备份到指定文件中可以使用如下命令:(把iptables规则备份到/tmp/ipt.txt文件中) iptables-save > /tmp/ipt.txt

我们cat一下看一下 cat /tmp/ipt.txt

想要恢复指定文件里的规则使用如下命令:(把备份的/tmp/ipt.txt恢复到iptables里面去) iptables-restore < /tmp/ipt.txt

使用service iptables save保存在/etc/sysconfig/iptables里的规则可以开启自动读取,而保存在其他文件里的规则只有恢复的时候才会用到。

firewalld的9个zone

下面学习firewalld,在7以后的系统中用的是firewalld防火墙。

我们之前把firewalld禁掉了开启了iptables,现在要把iptables禁掉开启firewalld。

systemctl disable iptables
 systemctl stop iptables
 systemctl enable firewalld
 systemctl start firewalld

firewalld的9个zone默认有9个zone,zone是firewalld的单位,firewalld默认使用public zone,每个zone就好比一个规则集(就是自带一些规则)

查看所有的zone使用如下命令 firewall-cmd --get-zones

查看默认的zone使用如下命令 firewall-cmd --get-default-zone

所有zone介绍

firewalld关设定默认zone于zone的操作

设定默认zone(设置为work) firewall-cmd --set-default-zone=work

查看指定网卡使用的哪个zone(查询网卡ens33) firewall-cmd --get-zone-of-interface=ens33

如果添加的网卡没有zone,可以做一个设置,将原来的网卡配置文件复制一份并改名成新添加的网卡名,然后写一下配置文件,然后重启网络服务,然后在重新加载一下firewalld,使用 systemctl restart firewalld 命令,然后再查新添加的网卡有没有zone。 进入网卡配置目录进行复制 cd /etc/sysconfig/network-scripts

重新加载firewalld systemctl restart firewalld

如果还没有我们就来增加一下。给指定网卡设置zone firewall-cmd --zone=public --add-interface=lo

我们还可以给网卡更改zone。针对网卡更改zone。(zone=你需要设置的zone,lo为你要设置的网卡名) firewall-cmd --zone=dmz --change-interface=lo

我们还可以针对网卡删除zone,删除了之后就变成了默认zone(zone=你需要删除的zone,lo为你要删除的网卡名) firewall-cmd --zone=dmz --remove-interface=lo

还可以查看所有网卡所在的zone firewall-cmd --get-active-zones

firewalld关于service的操作

service就是zone下面的子单元,可以理解成他是指定的端口,因为防火墙就是针对某个端口进行限制。

查看所有的service firewall-cmd --get-services

查看当前zone里有哪些service. firewall-cmd --list-services

查看指定zone里有哪些service(zone=你需要设置的zone) firewall-cmd --zone=public --list-services

把http增加到public zone下面 firewall-cmd --zone=public --add-service=http

添加后只是添加到了内存里,还需要保存到配置文件里去,不然重启之后就还原了,(public你需要设置的zone,http为你要设置的网卡名)保存后会在/etc/firewalld/zones目录下面生成配置文件 firewall-cmd --zone=public --add-service=http --permanent

zone的配置文件模板 ls /usr/lib/firewalld/zones/

下面我们来介绍一个案例 需求:ftp服务自定义端口1121,需要在work zone下面放行ftp

操作方法: 把ftp配置文件模板拷贝到/etc/firewalld/services下去 cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services

然后vi它把里面的21端口改成1121 vi /etc/firewalld/services/ftp.xml

之后把work配置文件模板拷贝到/etc/firewalld/zones/下 cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/

然后vim它,在里面增加一行service name="ftp vim /etc/firewalld/zones/work.xml

然后需要重新加载服务,然后我们可以插查看一下work zone里有没有ftp,如果有就完成了。 firewall-cmd --reload

firewall-cmd --zone=work --list-services