iptables规则备份和恢复与firewalled介绍和相关操作

一、iptables规则备份和恢复

我们在设置防火墙规则的时候,规则只是保存在内存中,并没有保存到某一个文件中。系统重启重启之后,之前设定的规则就么有了,所以设定好之后要先保存一下,使用命令:

[root@zlinux ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]

会看到提示,防火墙规则保存在/etc/sysconfig/iptables文件内,这个文件就是iptables的配置文件。所以如要备份防火墙规则,复制一份这个文件的复本即可。
有事我们需要清楚防火墙规则,使用iptables -F固然可以,但是最好的方法还是停止防火墙服务:

[root@zlinux ~]# service iptables stop 
Redirecting to /bin/systemctl stop  iptables.service
[root@zlinux ~]# service iptables stop 
Redirecting to /bin/systemctl stop  iptables.service
[root@zlinux ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 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 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 

这样防火墙就停止了,并且规则也清除了。但是一旦重新设置规则,防火墙服务回自动重启。那么我们来为防火情重新设置规则,并备份规则:

[root@zlinux ~]# sh /usr/local/sbin/iptables.sh     //通过脚本设置规则
[root@zlinux ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    6   428 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     tcp  --  *      *       192.168.204.0/24     0.0.0.0/0            tcp dpt:22
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:21

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

Chain OUTPUT (policy ACCEPT 4 packets, 528 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@zlinux ~]# iptables-save > myipt.rule    //备份规则,重定向到myipt.rule
[root@zlinux ~]# cat myipt.rule                        //打开备份文件
# Generated by iptables-save v1.4.21 on Fri Jan 26 16:19:22 2018
*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [40:4728]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -s 192.168.204.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
COMMIT
# Completed on Fri Jan 26 16:19:22 2018

如果需要恢复这些规则,使用以下命令:

[root@zlinux ~]# iptables-restore < myipt.rule

二、firewalld的介绍与简单应用

CentOS7的默认防火墙是firewalld,在之前使用iptables时,关闭了firewalld服务,现在反过来关闭iptables服务,打开firewalld服务:

[root@zlinux ~]# systemctl disable iptables
Removed symlink /etc/systemd/system/basic.target.wants/iptables.service.
[root@zlinux ~]# systemctl stop iptables
[root@zlinux ~]# 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/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
[root@zlinux ~]# systemctl start firewalld

打开firewalld服务之后,iptables相关的命令其实也可以继续使用的,但在CentOS7中不用那么操作,因为firewalld有自己的命令。
firewalld有两个基础概念,zone和service,每个zone里面有不同的iptables规则,默认一共有9个zone,CentOS7默认zone为public,查看系统所有zone,使用命令

[root@zlinux ~]# firewall-cmd --get-zones                           //查看所有zone
work drop internal external trusted home dmz public block
[root@zlinux ~]# firewall-cmd --get-default-zone                //查看默认zone
public

1、9个zone的简单介绍:

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

2、几个关于zone的命令

# firewall-cmd --set-default-zone=work                     //设定默认zone
# firewall-cmd --get-zone-of-interface=ens33           //查看指定网卡
# 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-zctive-zones                             //查看系统所有网卡所在的zone

3、fired中的service

service 就是zone下面的一个子单元,每个zone里都使用了不同的service,而service就是针对服务(端口)做的iptables规则。

[root@zlinux ~]# firewall-cmd --get-services          //查看当前系统所有service
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client ceph ceph-mon dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync freeipa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mosh mountd ms-wbt mysql nfs ntp open*** pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster radius rpc-bind rsyncd samba samba-client sane smtp smtps snmp snmptrap 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

这些service都是由一个个配置文件定义的,配置文件的模版子啊/usr/lib/firewalld/services/目录下,真正生效的配置文件则在/etc/firewalld/services/目录下(默认为空):

[root@zlinux ~]# ls /etc/firewalld/services/                  //默认为空
[root@zlinux ~]# 

关于service的一些操作命令:

# firewall-cmd --get-services                                          //查看所有的servies
# firewall-cmd --list-services                                          //查看当前zone下有哪些service
# firewall-cmd --zone=public --add-service=http           //把http服务增加到public zone下面
# firewall-cmd --zone=public --remove-service=http     //把http服务移除
# ls /usr/lib/firewalld/zones/                                           //zone的配置文件模板
# firewall-cmd --zone=public --add-service=http --permanent           //更改配置文件,否则只是保存在内存中,之后会在/etc/firewalld/zones目录下面生成配置文件

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



```[root@zlinux ~]# cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/
[root@zlinux ~]# vi /etc/firewalld/services/ftp.xml 
[root@zlinux ~]# vi /etc/firewalld/services/ftp.xml      //修改ftp端口号

<?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"/>       ##修改端口为1121
  <module name="nf_conntrack_ftp"/>
</service>
[root@zlinux ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@zlinux ~]# vi /etc/firewalld/zones/work.xml    //增加ftp相关配置

<?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@zlinux ~]# firewall-cmd --reload               //重新加载
success
[root@zlinux ~]# firewall-cmd --zone=work --list-services         //查看是否有ftp
ssh dhcpv6-client ftp

转载于:https://blog.51cto.com/3069201/2065555

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值