10.19 iptables规则备份和恢复linux
10.20 firewalld的9个zonecentos
10.21 firewalld关于zone的操做服务器
10.22 firewalld关于service的操做网络
10.19 iptables规则备份和恢复:ssh
~1. 保存和备份iptables规则tcp
~2. service iptables save 会把规则保存到 /etc/sysconfig/iptablesthis
~3. 把iptables规则备份到my.ipt文件中centos7
~4. iptables-save > my.iptrest
iptables-save重定向到这个文件中。名字能够随便起,也能够是.txt文件server
~5. 恢复刚才备份的规则
备份的规则,可能只有在恢复的时候能用到它。可是咱们一重启服务器就想要加载规则,那么最好把规则备份到默认的 /etc/sysconfig/iptables里
~6. iptables-restore < my.ipt
跟上面~4. 备份的重定向正好相反
----------------------------------------------------------------------------------------------------------------------------------------------------
10.20 firewalld的9个zone:
filewalld是centos7以及之后得版本要用到的防火墙机制。是一个新的趋势
~1. 打开firewalld
由于咱们以前作实验,禁掉了firewalld,全部咱们在把他打开。执行~2 3 4 5 就能够了
~2. systemctl disable iptables
~3. systemctl stop iptables
连续执行~2 3 禁掉iptables
~4. systemctl enable firewalld
~5. systemctl start firewalld
连续执行~4 5 打开firewalld
咱们在打开firewalld后,再iptables -nvL,会发现跟netfilter的规则不太同样,会比netfilter多不少,由于他包含了nat里的一些规则
~6. firewalld 默认有9个zone
firewalld的用法,zone是firewalld的单位。默认的zone是public,也就是~7. 。每一个zone比如是一个规则集(规则集就是,好比咱们放开了22端口、80端口,关闭了一些端口。这些规则就是一个规则集)
~7. 默认zone为public
~8. firewall-cmd --get-zones 查看全部zone (注意是firewall,敲命令是没有d)
[root@axinlinux-01 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
这就是他的9个zone
~9. firewall-cmd --get-default-zone 查看默认zone (注意是firewall,敲命令是没有d)
[root@axinlinux-01 ~]# firewall-cmd --get-default-zone
public
默认的就是public,也就是~7.
下图为firewalld的9个zone的解释
----------------------------------------------------------------------------------------------------------------------------------------------------
10.21 firewalld关于zone的操做:
firewall 的几个命令:
~1. firewall-cmd --set-default-zone=work 设定默认zone
等于号后面跟想更改的默认zone
~2. firewall-cmd --get-zone-of-interface=ens33 查指定网卡,属于那个zone
阿鑫在查看ens37的时候发现没有zone,作了一些操做,详见实例2.
~3. firewall-cmd --zone=public --add-interface=lo 给指定网卡设置zone
~4. firewall-cmd --zone=dmz --change-interface=lo 针对网卡更改zone
~5. firewall-cmd --zone=dmz --remove-interface=lo 针对网卡删除zone
好比,把lo这个网卡设置成了dmz,可是要想把dmz里的lo给他删掉
须要注意的是,把原有的zone里的网卡删掉,他就变成默认的zone了
~6. firewall-cmd --get-active-zones 查看系统全部网卡所在的zone
实例:
1.
[root@axinlinux-01 ~]# firewall-cmd --set-default-zone=work
success
[root@axinlinux-01 ~]# firewall-cmd --get-default-zone
work
2.
[root@axinlinux-01 ~]# cd /etc/sysconfig/network-scripts/
[root@axinlinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens37 复制一份ens33为ens37
[root@axinlinux-01 network-scripts]# vi !$ 修改一下IP,又删除了网关和dns1
vi ifcfg-ens37
[root@axinlinux-01 network-scripts]# systemctl restart network.service 重启一下网络服务
[root@axinlinux-01 network-scripts]# systemctl restart firewalld 重启防火墙
[root@axinlinux-01 network-scripts]# firewall-cmd --get-zone-of-interface=ens37 发现仍是没有,尴尬了
no zone
[root@axinlinux-01 network-scripts]# firewall-cmd --zone=public --add-interface=ens37 有执行了3.,才有的
success
[root@axinlinux-01 network-scripts]# firewall-cmd --get-zone-of-interface=ens37
public
5.
[root@axinlinux-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
no zone
[root@axinlinux-01 network-scripts]# firewall-cmd --zone=dmz --add-interface=lo
success
[root@axinlinux-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
dmz
[root@axinlinux-01 network-scripts]# firewall-cmd --zone=dmz --remove-interface=lo
success
[root@axinlinux-01 network-scripts]# firewall-cmd --get-zone-of-interface=lo
no zone
6.
[root@axinlinux-01 network-scripts]# firewall-cmd --get-active-zones
work
interfaces: ens33
----------------------------------------------------------------------------------------------------------------------------------------------------
10.22 firewalld关于service的操做:
service是zone下面的一个子单位,可理解为他是一个指定一个端口。由于防火墙无外乎就是对某一个端口作限制
好比,http他操做的是80端口。https操做的是43端口。ssh操做的是22端口
service的操做:
~1. firewall-cmd --get-service 查看全部的service
~2. firewall-cmd --list-service 查看当前zone下有哪些service
指定zone查看有哪些service。 firewall-cmd --zone=public --list-service
~3. firewall-cmd --zone=public --add-service=http 把http增长到public zone下面
这样仅仅只是在内存中增长了,要改为永久的须要更改配置文件 ~6.
~4. firewall-cmd --zone=public --remove-service=http
把http在public中去除,与 3 相对应的
~5. ls /usr/lib/firewalld/zones/ zone的配置文件模板
有9个,9个zone9个模板
更改后的配置文件也会在这里存放。每次只有--permanent才会加入。
而咱们 /etc/firewalld/zones/下,是咱们系统etc下的firewalld服务所用到的配置文件,每次改完并永久保存,他都会把旧的作一个备份并加上后缀.old
~6. firewall-cmd --zone=public --add-service=http --permanent 更改配置文件,以后会 在/etc/firewalld/zones目录下生成配置文件
~7. 需求:(使用配置文件的方式进行操做)
(案例)
ftp服务自定义端口1121,须要在work zone下面放行ftp
8. cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services (作实验因此cp一份)
把ftp的文件先cp到etc下面去
9. vi /etc/firewalld/services/ftp.xml 把21改成1121
而后编辑他,把21端口改成1121端口
10. cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/ (作实验因此cp一份)
11. vi /etc/firewalld/zones/work.xml 增长一行
复制上面的一行格式,在双引号里面的改为ftp。也就是12的操做
12. <service name="ftp"/>
13. firewall-cmd --reload 从新加载
14. firewall-cmd --zone=work --list-services
指定一个zone,查看他的service
检查以上操做,是否成功
实例:
7.
[root@axinlinux-01 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
[root@axinlinux-01 ~]# 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 the vsftpd package installed for this option to be useful.</description>
<port protocol="tcp" port="1121"/> 就改这
<module name="nf_conntrack_ftp"/>
</service>
[root@axinlinux-01 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@axinlinux-01 ~]# vi /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"/> 复制上面的一行(格式),把双引号里面的改为 ftp
</zone>
[root@axinlinux-01 ~]# firewall-cmd --reload
success
而后,咱们在检查一下
[root@axinlinux-01 ~]# firewall-cmd --zone=work --list-service
ssh dhcpv6-client ftp work zone下有ftp