10.19 iptables规则备份恢复

Linux防火墙—netfilter

1. 保存 iptables规则

(默认规则保存到:/etc/sysconfig/iptables文件中。)

[root@hao-001 ~]# service iptables save

2. 备份 iptables规则重定向(保存)到指定路径

[root@hao-001 ~]# iptables-save > /tmp/ipt.txt

查看保存的iptables规则

[root@hao-001 ~]# cat /tmp/ipt.txt

3. 清空 iptables规则

[root@hao-001 ~]# iptables -t nat -F

4. 恢复 iptables规则(刚刚备份的)

[root@hao-001 ~]# iptables-restore < /tmp/ipt.txt

10.20 firewalld的9个zone

Linux防火墙-firewalled

1. 关闭 iptables(centos6 防火墙)

[root@hao-001 ~]# systemctl disable iptables

[root@hao-001 ~]# systemctl stop iptables

2. 打开 firewalled(centos7防火墙 之前关掉了)

[root@hao-001 ~]# systemctl enable firewalld

[root@hao-001 ~]# systemctl start firewalld

3. firewalld 默认9zone(规则);默认zonepublic

4. 查看 firewalld 所有zone(共9个,默认zonepublic) :

[root@hao-001 ~]# firewall-cmd --get-zones

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_02

5. 查询 firewalled默认zone(规则)是什么(默认zonepublic)  

[root@hao-001 ~]# firewall-cmd --get-default-zone

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_04

6. 9个zone的详解:

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_firewalld_06

10.21 firewalld关于zone的操作

1. 更改 firewalled默认zone(规则)

[root@hao-001 ~]# firewall-cmd --set-default-zone=work

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_08

2. 查看 指定网卡zone(规则)

firewall-cmd --get-zone-of-interface=网卡名

[root@hao-001 ~]# firewall-cmd --get-zone-of-interface=ens33

[root@hao-001 ~]# firewall-cmd --get-zone-of-interface=ens37

[root@hao-001 ~]# firewall-cmd --get-zone-of-interface=lo

3. 增加 指定网卡zone(规则)

firewall-cmd --zone=定义zone --add-interface=网卡名

[root@hao-001 ~]# firewall-cmd --zone=public --add-interface=ens37

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_firewalld_09

[root@hao-001 ~]# firewall-cmd --zone=public --add-interface=lo

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_11

4. 更改 指定网卡zone

firewall-cmd --zone=新zone --change-interface=网卡名

[root@hao-001 ~]# firewall-cmd --zone=block --change-interface=ens33

[root@hao-001 ~]# firewall-cmd --zone=block --change-interface=ens37

[root@hao-001 ~]# firewall-cmd --zone=block --change-interface=lo

5. 删除 指定网卡zone(变成默认zone)

firewall-cmd --zone=网卡对应zone  --remove-interface=网卡名

[root@hao-001 ~]# firewall-cmd --zone=block  --remove-interface=ens33

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_firewalld_13

6. 查看 系统所有网卡 对应zone(查看系统所有网卡所在的zone)

[root@hao-001 ~]# firewall-cmd --get-active-zones

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_15

10.22 firewalld关于service的操

services是zone下的一个子单元

(指定的一个端口:如http,到service下,就不会被限制过滤)  

http操作80端口   https操作443端口   ssh操作22端口  

1. 查看 系统所有的servie

(zone下servie相当于白名单,添加http等到servie下,就不会过滤直接放行)

[root@hao-001 ~]# firewall-cmd --get-services10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_1610.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_firewalld_17

1.5 查看 默认zone

[root@hao-001 ~]# firewall-cmd --get-default-zone

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_18

2. 查看 默认zone,包含哪些service

[root@hao-001 ~]# firewall-cmd --list-services

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_1610.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_firewalld_20

3. 查看 指定zone,包含哪些service

firewall-cmd --zone=指定zone --list-services

[root@hao-001 ~]# firewall-cmd --zone=work --list-services

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_firewalld_21

4. 增加 http指定zone下(临时储存在内存中)

[root@hao-001 ~]# firewall-cmd --zone=work --add-service=http10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_1610.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_firewalld_23

5. 增加 http指定zone下(永久保存在zone配置文件)

[root@hao-001 ~]# firewall-cmd --zone=work --add-service=http --permanent

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_24

6. 查看 zone配置文件所在目录

(更新永久保存到配置文件,会重新生成.xml文件,之前的配置文件变成.xml.old)

[root@hao-001 ~]# ls /etc/firewalld/zones

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_1610.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_firewalld_26

7. 查看 zone配置文件内容

[root@hao-001 ~]# cat /etc/firewalld/zones/work.xml

8. 查看 firewalld配置文件所在目录 (默认无,还没有更改过firewalld配置文件)

[root@hao-001 ~]# ls /etc/firewalld/services

9. 查看 zone配置文件模板

[root@hao-001 ~]# ls /usr/lib/firewalld/zones

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_firewalld_27

10. 查看 services配置文件模板

[root@hao-001 ~]# ls /usr/lib/firewalld/services

•需求:ftp服务自定义端口1121

•需要在work zone下面放行ftp

1. 拷贝 services/ftp.xml配置文件/etc/...

[root@hao-001 ~]# cp /usr/lib/firewalld/services/ftp.xml  /etc/firewalld/services

2. 编辑 ftp配置文件,22端口更改为自定义的 1121

[root@hao-001 ~]# vi /etc/firewalld/services/ftp.xml

更改: port="1121"

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_28

3. 拷贝 zones/work.xml配置文件/etc/...

(注意:上面做实验把默认zone设定为work了,http设定到work的配置文件了,永久保存就会产生了一个work.xml文件,这里复制的模板覆盖掉之前的文件就可以了!)

[root@hao-001 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones

4. 编辑 work配置文件,添加一条 service名字ftp

[root@hao-001 ~]# vi /etc/firewalld/zones/work.xml

添加一行: <service name="ftp"/>

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_29

5. 重新加载 :

[root@hao-001 ~]# firewall-cmd --reload

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_1610.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_31

6. 查看zone(work)下,包含service(ftp):

[root@hao-001 ~]# firewall-cmd --zone=work --list-services

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_1610.19 iptables规则备份和恢复 10.20 firewalld的9个zone10.21_规则备份_33