iptables规则备份和恢复、firewalld的9个zone、firewalld关于zone的操作、firewalld关于service的操作

目录

一、iptables规则备份和恢复
二、firewalld的9个zone
三、 firewalld关于zone的操作
四、 firewalld关于service的操作

一、iptables规则备份和恢复

  • service iptables save 会把规则保存到 /etc/sysconfig/iptables
[root@minglinux-01 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]
  • 使用iptables-save命令把规则重定向到一个文件里
[root@minglinux-01 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
[root@minglinux-01 ~]# iptables-save > /tmp/ipt.txt
[root@minglinux-01 ~]# cat /tmp/ipt.txt 
# Generated by iptables-save v1.4.21 on Mon Oct 29 21:21:58 2018
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
COMMIT
# Completed on Mon Oct 29 21:21:58 2018
# Generated by iptables-save v1.4.21 on Mon Oct 29 21:21:58 2018
*filter
:INPUT ACCEPT [132:8932]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [72:8992]
COMMIT
# Completed on Mon Oct 29 21:21:58 2018
  • 使用iptables-restore恢复刚才的规则
[root@minglinux-01 ~]# iptables -t nat -F
[root@minglinux-01 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@minglinux-01 ~]# iptables-restore < /tmp/ipt.txt 
[root@minglinux-01 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      ens33   192.168.100.0/24     0.0.0.0/0 

二、firewalld的9个zone

  • 打开firewalled
    前面我已经把firewalled禁用了,现在反过来要关闭iptables
    服务,打开firewalld服务,示例命令如下:
[root@minglinux-01 ~]# systemctl disable iptables 
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@minglinux-01 ~]# systemctl stop iptables
[root@minglinux-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@minglinux-01 ~]# systemctl start firewalld
  • firewalled的zone
    firewalld有两个基础概念,分别是zone和service,每一个zone里面有不同的iptables规则,默认一共有9个zone,而CentOS 7默认的zone为public。

获取系统所有的zone:firewall-cmd --get-zones
查看系统默认的zone: firewall-cmd --get-default-zone

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

firewalled的9个zone:

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

三、 firewalld关于zone的操作

  • firewall-cmd --set-default-zone=work //设定默认zone

  • fire-cmd --get-zone-of-interface=ens33 //查指定网卡所在zone

  • firewall-cmd --zone=public --add-interface=lo //给指定网卡设置zone

  • firewall-cmd --zone=dmz --change-interface=lo //针对网卡更改zone

  • firewall-cmd --zone=dmz --remove-interface=lo //针对网卡删除zone

  • firewall-cmd --get-active-zones //查看系统所有网卡所在的zone

四、 firewalld关于service的操作

每一个zone里面都使用了不同
的service,而service就是针对一个服务(端口)做的iptables规则。
这些service都是由一个个配置文件定义的,配置文件的模板在/usr/lib/firewalld/services/目录下,真正生效的配置在/etc/firewalld/services目录下面(默认为空):

[root@minglinux-01 ~]#  ls /usr/lib/firewalld/services/
amanda-client.xml        nfs.xml
amanda-k5-client.xml     nrpe.xml
bacula-client.xml        ntp.xml
bacula.xml               openvpn.xml
bitcoin-rpc.xml          ovirt-imageio.xml
bitcoin-testnet-rpc.xml  ovirt-storageconsole.xml
bitcoin-testnet.xml      ovirt-vmconsole.xml
bitcoin.xml              pmcd.xml
ceph-mon.xml             pmproxy.xml
ceph.xml                 pmwebapis.xml
cfengine.xml             pmwebapi.xml
condor-collector.xml     pop3s.xml
ctdb.xml                 pop3.xml
dhcpv6-client.xml        postgresql.xml
dhcpv6.xml               privoxy.xml
dhcp.xml                 proxy-dhcp.xml
dns.xml                  ptp.xml
docker-registry.xml      pulseaudio.xml
dropbox-lansync.xml      puppetmaster.xml
elasticsearch.xml        quassel.xml
freeipa-ldaps.xml        radius.xml
freeipa-ldap.xml         RH-Satellite-6.xml
freeipa-replication.xml  rpc-bind.xml
freeipa-trust.xml        rsh.xml
ftp.xml                  rsyncd.xml
ganglia-client.xml       samba-client.xml
ganglia-master.xml       samba.xml
high-availability.xml    sane.xml
https.xml                sips.xml
http.xml                 sip.xml
imaps.xml                smtp-submission.xml
imap.xml                 smtps.xml
ipp-client.xml           smtp.xml
ipp.xml                  snmptrap.xml
ipsec.xml                snmp.xml
iscsi-target.xml         spideroak-lansync.xml
kadmin.xml               squid.xml
kerberos.xml             ssh.xml
kibana.xml               synergy.xml
klogin.xml               syslog-tls.xml
kpasswd.xml              syslog.xml
kshell.xml               telnet.xml
ldaps.xml                tftp-client.xml
ldap.xml                 tftp.xml
libvirt-tls.xml          tinc.xml
libvirt.xml              tor-socks.xml
managesieve.xml          transmission-client.xml
mdns.xml                 vdsm.xml
mosh.xml                 vnc-server.xml
mountd.xml               wbem-https.xml
mssql.xml                xmpp-bosh.xml
ms-wbt.xml               xmpp-client.xml
mysql.xml                xmpp-local.xml
nfs3.xml                 xmpp-server.xml
  • firewall-cmd --get-service //查看当前系统所有的service
[root@minglinux-01 ~]# firewall-cmd --get-service
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 nfs3 nrpe ntp openvpn 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@minglinux-01 ~]# firewall-cmd --get-default-zone 
work
[root@minglinux-01 ~]# firewall-cmd --list-services 
ssh dhcpv6-client
[root@minglinux-01 ~]#  firewall-cmd --zone=public --list-service    //查看指定zone下有哪些service
ssh dhcpv6-client
  • firewall-cmd --zone=public --add-service=http //在一个zone里面增加一个service
[root@minglinux-01 ~]# firewall-cmd --zone=public --add-service=http
success
[root@minglinux-01 ~]# firewall-cmd --zone=public --list-service 
ssh dhcpv6-client http
  • firewall-cmd --zone=public --add-service=http --permanent
    //修改配置文件,之后会在/etc/firewalled/zones目录下生成配置文件
[root@minglinux-01 ~]# firewall-cmd --zone=public --add-service=http --permanent 
success
[root@minglinux-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"/>
</zone>
  • 需求:ftp服务自定义端口1121,需要在work zone下面放行ftp
[root@minglinux-01 ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/      //usr/lib/firewalld/services/目录下面为所有service的模板配置文件
[root@minglinux-01 ~]# vim /etc/firewalld/services/ftp.xml    // 把里面的21改为1121
[root@minglinux-01 ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/    
[root@minglinux-01 ~]# vim /etc/firewalld/zones/work.xml     // 在里面增加一行FTP相关的配置,内容为 <service name="ftp"/>
 [root@minglinux-01 ~]# firewall-cmd --reload    // 重新加载
success
  • 验证一下work zone里面的service是否有FTP:
[root@minglinux-01 ~]# firewall-cmd --zone=work --list-services 
ssh dhcpv6-client ftp
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容