七周五次课(3月23日)

10.19 iptables规则备份和恢复

10.20 firewalld的9个zone

10.21 firewalld关于zone的操作

10.22 firewalld关于service的操作



10.19 iptables规则备份和恢复


10.19-10.22 firewalld的9个zone,zone的操作,service的操作_iptables

保存和备份iptables规则

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

如果需要把规则自定义保存到别的文件

例如把iptables规则备份到/tmp/ipt.txt文件中

备份命令 

iptables-save > /tmp/ipt.txt

示例操作

[root@centos7-01 ~]# iptables-save > /tmp/ipt.txt
[root@centos7-01 ~]# cat /tmp/ipt.txt 
# Generated by iptables-save v1.4.21 on Fri Mar 23 16:34:55 2018
*filter
:INPUT ACCEPT [50:3392]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [29:3100]
COMMIT
# Completed on Fri Mar 23 16:34:55 2018
# Generated by iptables-save v1.4.21 on Fri Mar 23 16:34:55 2018
*nat
:PREROUTING ACCEPT [3:373]
:INPUT ACCEPT [3:373]
:OUTPUT ACCEPT [28:2119]
:POSTROUTING ACCEPT [29:2171]
:nvL - [0:0]
-A PREROUTING -d 192.168.189.128/32 -p tcp -m tcp --dport 1122 -j DNAT --to-destination 192.168.100.100:22
-A POSTROUTING -s 192.168.100.100/32 -j SNAT --to-source 192.168.189.128
COMMIT
# Completed on Fri Mar 23 16:34:55 2018
[root@centos7-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 102 packets, 7068 bytes)
 pkts bytes target     prot opt in     out     source               destination         
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
Chain OUTPUT (policy ACCEPT 63 packets, 7684 bytes)
 pkts bytes target     prot opt in     out     source               destination

 

恢复规则

iptables-restore < /tmp/ipt.txt

可以看到规则已经被恢复回来。

[root@centos7-01 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            192.168.189.128      tcp dpt:1122 to:192.168.100.100:22
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
Chain OUTPUT (policy ACCEPT 4 packets, 304 bytes)
 pkts bytes target     prot opt in     out     source               destination         
Chain POSTROUTING (policy ACCEPT 4 packets, 304 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 SNAT       all  --  *      *       192.168.100.100      0.0.0.0/0            to:192.168.189.128
Chain nvL (0 references)
 pkts bytes target     prot opt in     out     source               destination

****netfiler的知识点很丰富,功能很强大,以后可以详细慢慢深入学习。



10.20 firewalld的9个zone


10.19-10.22 firewalld的9个zone,zone的操作,service的操作_zone_02

打开firewalld需要做的操作,其实就是反操作禁掉iptables,再开启firewalld,具体步骤如何。

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

操作

[root@centos7-01 ~]# systemctl disable iptables
[root@centos7-01 ~]# systemctl stop iptables
[root@centos7-01 ~]# 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@centos7-01 ~]# systemctl start firewalld

firewalld默认有9个zone

zone是firewalld的一个单位

默认zone为public

每个zone相当于一个规则集,每个zone自带一些规则,不同zone有不同功能规则。

firewall-cmd --get-zones 查看所有zone

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

firewall-cmd --get-default-zone    查看默认zone

[root@centos7-01 ~]# firewall-cmd --get-default-zone

public


9个zone的介绍

10.19-10.22 firewalld的9个zone,zone的操作,service的操作_zone_03

  • drop(丢弃):任何接受的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接。

    最安全的,进来的包,进不来,但是可以发包出去。

  • block(限制):任何接受的网络连接都被IPv4的icmp-host-prohibited信息和IPv6的icmp6-adm-prohibited信息所拒绝。

    主要针对icmp.

  • public(公共):在公共区域内使用,不能相信网络内的其他计算机不会对你的计算机造成危害,只能接受经过选取的连接。

    默认zone.在public里,有些数据包是放行的,也有一些数据包是被禁掉的。

  • external(外部):特别是为了路由器启用了伪装功能的外部网。你不能信任来自网络的其他计算,不能相信相信它们不会对你的计算机造成危害,只能接收经过选择的连接。

    适合路由器。

  • dmz(非军事区):用于你的非军事区内的计算机,此区域内可公开访问,可以有限地进入你的内部网络,仅仅接收经过选择的连接。

  • work(工作):用于工作区。你可以基本相信网络内的其他计算机不会危害你的计算机。仅仅接收进过选择的连接。

    相当于内网。

  • home(家庭):用于家庭网络。你可以基本相信网络内的其他计算机不会危害你的计算机。仅仅接收进过选择的连接。

  • internal(内部):用于内部。你可以基本相信网络内的其他计算机不会危害你的计算机。仅仅接收进过选择的连接。

    机房内使用,内网连接。

  • trusted(信任):可接收所有的网络连接。

    没有任何限制。



10.21 firewalld关于zone的操作


10.19-10.22 firewalld的9个zone,zone的操作,service的操作_iptables_04

firewall-cmd --set-default-zone=work 
#设定默认zone
[root@centos7-01 ~]# firewall-cmd --set-default-zone=work
success
[root@centos7-01 ~]# firewall-cmd --get-default-zone 
work
firewall-cmd --get-zone-of-interface=ens33 
#查指定网卡
[root@centos7-01 ~]# firewall-cmd --get-zone-of-interface=ens33 
work
[root@centos7-01 ~]# firewall-cmd --get-zone-of-interface=ens37
no zone

!!!发现ens37为no zone,此时候,需要把ens33的网络配置文件复制多一份,名为ens-37,

然后更改对应的ip和device,name。完成后,重启网络服务和重新加载firewalld,再次get zone .

[root@centos7-01 ~]# firewall-cmd --zone=work --add-interface=ens37
The interface is under control of NetworkManager, setting zone to 'work'.
success

因为刚刚没有成功设置网卡zone,所以尝试强制加zone.

firewall-cmd --zone=public --add-interface=lo 给指定网卡设置zone 
[root@centos7-01 ~]# firewall-cmd --zone=public --add-interface=lo
success
[root@centos7-01 ~]# firewall-cmd --get-zone-of-interface=lo
public
firewall-cmd --zone=dmz --change-interface=lo 针对网卡更改zone
firewall-cmd --zone=dmz  --remove-interface=lo 针对网卡删除zone 
firewall-cmd --get-active-zones  查看系统所有网卡所在的zone


10.22 firewalld关于service的操作


10.19-10.22 firewalld的9个zone,zone的操作,service的操作_services _05

services相当于zone下面的一个子单元,可以理解成,它是一个指定的端口,因为防火墙,无外乎只是针对一个端口做一些限制。

比如http针对80端口,https针对43端口,ssh 22端口。

查看所有的services都有哪些。

#firewall-cmd --get-services  
[root@centos7-01 ~]# firewall-cmd --get-services 
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
firewall-cmd --list-services  查看当前zone下有哪些service
[root@centos7-01 ~]# firewall-cmd --list-services 
ssh dhcpv6-client
[root@centos7-01 ~]# firewall-cmd --zone=public --list-services 
ssh dhcpv6-client


把http服务增加到public zone下面

firewall-cmd --zone=public --add-service=http

演示

[root@centos7-01 ~]# firewall-cmd --zone=public --add-service=http
success
[root@centos7-01 ~]# firewall-cmd --zone=public --list-service
ssh dhcpv6-client http
[root@centos7-01 ~]# firewall-cmd --zone=public --add-service=ftp



其实上述操作只是暂时保存在内存中,还没写入到配置文件当中,要写入配置文件,需要如下操作。

firewall-cmd --zone=public --add-service=http --permanent

更改配置文件,之后会在/etc/firewalld/zones目录下面生成配置文件。

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

#cat public.xml的配置,已经有了ftp和http服务了,http和ftp成功被写入配置文件。

[root@centos7-01 ~]# 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 name="ftp"/>
</zone>
[root@centos7-01 ~]# firewall-cmd --zone=public --list-services 
ssh dhcpv6-client http ftp
#ls /etc/firewalld/zones/     zone的配置文件
[root@centos7-01 ~]# ls /etc/firewalld/zones/ 此处的.old是备份文件
public.xml  public.xml.old
[root@centos7-01 ~]# ls /etc/firewalld/services/ services默认没有文件的。
#ls /usr/lib/firewalld/zones/ zone的配置文件模板
#ls /usr/lib/firewalld/services/ services配置文件模板

10.19-10.22 firewalld的9个zone,zone的操作,service的操作_iptables_06

实验

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

操作

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

#vi /etc/firewalld/services/ftp.xml 

#把21改为1121

10.19-10.22 firewalld的9个zone,zone的操作,service的操作_iptables_07

#cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
#vi /etc/firewalld/zones/work.xml 增加一行 修改名称ftp
 <service name="ftp"/>

10.19-10.22 firewalld的9个zone,zone的操作,service的操作_services _08

操作完成后,重新加载firewall-cmd 

#firewall-cmd --reload //重新加载
[root@centos7-01 ~]# firewall-cmd --reload 
success

关注,ftp,成功放行。

[root@centos7-01 ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client ftp

总结

firewalld有两个角色,一个zone 一个service

zone 规则集 每个zone下都有对应iptables规则,每个zone都有service. 

而每个zone有serivce,zone就会把sercice当做白名单,放行service,

如果遇到需求,想放行某个服务,就可以把服务增加到配置文件里,再重新reload就可以放行了。

至于service也是可以自定义的。