iptables规则的备份和恢复

iptables规则保存,默认保存的位置路径:/etc/sysconfig/iptables文件里;

service iptables save

iptables规则备份到指定路径

iptables-save > /tmp/ipt.txt

iptables恢复规则可以选项将/etc/sysconfig/iptables文件做备份,也可以使用下面命令将输出的文件恢复:

iptables-restore < /tmp/ipt.txt

firewalld的9个zone

鉴于上节课做实验我们关闭了firewalld打开了iptables,我们需要关闭iptables和开启firewalld

1.关闭iptables

systemctl disable iptables #禁用开机启动
systemctl stop iptables #停止iptables

2.启用firewalld

systemctl enable firewalld #设置开机启动
systemctl start firewalld #启动firewalld

3.重新查看规则发现默认规则已经和之前不同,说明已经更改过来

iptables -nvL

firewalld默认有9个zone,zone是firewalld的最小单位,默认zone为public;

查看firewalld的9个zone的命令

[root@yolks1 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work

查看firewalld默认使用的zone

[root@yolks1 ~]# firewall-cmd --get-default-zone
public

上面9个zone的简介:

  • drop(丢弃):任何接收的网络数据包都被抛弃,没有任何回复。仅能有发送出去的网络连接。
  • block(限制):任何接收的网络连接都被IPv4的icmp-host-prohibited信息和IPv6的icmp-adm-prohibited信息所拒绝。
  • public(公共):在公共区域使用,不能相信网络内的其他计算机不会对你的计算机造成危害,只能接收经过选取的连接
  • external(外部):特别是为路由器启用了伪装功能的外部网。你不能信任来自网络的其他计算,不能相信它们不会对你的计算机造成危害,只能接收经过选择的连接。
  • dmz(非军事区):用于你的非军事区内的计算机,此区域内可公开访问,可以有限地进入你的内部网络,仅仅接收经过选择的连接。
  • work(工作):用于工作区。你可以基本相信网络内的其它计算机不会危害你的计算机。仅仅接收经过选择的连接。
  • home(家庭):用于家庭网络。你可以基本信任网络内的其它计算机不会危害你的计算机。仅仅接收经过选择的连接。
  • internal(内部):用于内部网络。你可以基本上信任网络内的其它计算机不会威胁你的计算机,仅仅接收经过选择的连接。
  • trusted(信任):可接收所有的网络连接

firewalld关于zone的操作

设定默认的zone为work

[root@yolks1 ~]# firewall-cmd --set-default-zone=work
success
[root@yolks1 ~]# firewall-cmd --get-default-zone
work

查看指定网卡所在的zone

[root@yolks1 ~]# firewall-cmd --get-zone-of-interface=ens33
work

给指定网卡设置zone

[root@yolks1 ~]# firewall-cmd --zone=home --add-interface=ens33
The interface is under control of NetworkManager, setting zone to 'home'.
success
[root@yolks1 ~]# firewall-cmd --get-zone-of-interface=ens33
home

针对网卡更改zone

[root@yolks1 ~]# firewall-cmd --zone=work --change-interface=ens33
The interface is under control of NetworkManager, setting zone to 'work'.
success
[root@yolks1 ~]# firewall-cmd --get-zone-of-interface=ens33
work

针对网卡删除zone

firewall-cmd --zone=work --remove-interface=ens33

查看系统所有网卡所在的zone

[root@yolks1 ~]# firewall-cmd --get-active-zones
work
  interfaces: ens33

firewalld关于service的操作

在/usr/lib/firewalld/services/目录中,还保存了另外一类配置文件,每个文件对应一项具体的网络服务,如ssh服务等。
与之对应的配置文件中记录了各项服务所使用的tcp/udp端口,在最新版的firewalld中默认已经定义了70多种服务供我们使用。
zone就是调用了不同的service而实现了不同的效果。

列出当前系统所有的service

firewall-cmd --get-service

查看当前zone下有哪些service

firewall-cmd --list-services

查看指定zone下有哪些service

firewall-cmd --zone=public --list-services

把http和ftp增加到public zone下面(临时保存)

[root@yolks1 ~]# firewall-cmd --zone=public --add-service=http --add-service=ftp
success

以修改配置文件来增加service

[root@yolks1 ~]# firewall-cmd --zone=public --add-service=http --permanent
success

查看zone对应的配置文件(配置文件在/etc/firewalld/zones/下,每次修改完配置文件,他都会把旧的配置文件后缀名加上.old也保存在目录下)

[root@yolks1 ~]# ls /etc/firewalld/zones/
public.xml  public.xml.old
[root@yolks1 ~]# 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"/>
</zone>

在**/usr/lib/firewalld/services下保存的是services的模板
/usr/lib/firewalld/zone下保存的是zone**的模板

需求:把ftp服务自定义端口1121,需要在work zone下面放行ftp

1.复制模板到service下

cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services

2.修改ftp的配置文件,修改端口1121

3.复制模板到zones下

cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/

4.修改work.xml的配置文件,把ftp加到里面

5.重新查看workzone的服务

[root@yolks1 ~]# firewall-cmd --zone=work --list-service
ssh dhcpv6-client ftp