防火墙规则保存及自定义链

防火墙规则保存

命令:iptables -save

工具:iptables services

[root@localhost ~]# iptables-save > /opt/iptables.bak
 #将文件保存并备份到opt目录下
 #iptables.bak为自定义文件名
 ​
 [root@localhost ~]# iptables-restore < /opt/iptables.bak
 #将备份文件导入到规则中
 ​
 将文件设置开机时启动防火墙配置规则
 [root@localhost ~]# vim .bashrc
 ······
 iptables-restore < /opt/iptables.bak
 #将导入命令添加到最后一行
 #开机是读取.bashrc文件,自动生效
 ​
 [root@localhost ~]# vim /etc/profile
 #此文件配置必须登录生效,生产环境中会有不登录的情况,所以不建议使用
 ​
 将防火墙规则永久保存
 [root@localhost ~]# vim /etc/rc.d/rc.local
 ······
 iptables-restore < /opt/iptables.bak
 #/etc/rc.d/rc.local是加载用户自定义服务的文件,优先运行
 ​
 [root@localhost ~]# chmod +x /etc/rc.d/rc.local
 #配置完必须将文件赋予执行权限
  • 保存命令

防火墙规则保存及自定义链_第1张图片

防火墙规则保存及自定义链_第2张图片

  • 自动配置文件

防火墙规则保存及自定义链_第3张图片

防火墙规则保存及自定义链_第4张图片

防火墙规则保存及自定义链_第5张图片

备份工具 iptables services
 [root@localhost ~]# yum install iptables-services.x86_64 -y
 [root@localhost ~]# rpm -ql iptables-services
 /etc/sysconfig/iptables
 #存放系统自带规则的文件
 ​
 方法1
 [root@centos7 ~]# cp /etc/sysconfig/iptables{,.bak}
 #保存现在的规则到文件中
 ​
 方法2
 [root@centos7 ~]# /usr/libexec/iptables/iptables.init save
 #保存现在的规则到文件中
 ​
 [root@centos7 ~]# iptables-save > /etc/sysconfig/iptables
 #开机启动
 [root@localhost ~]# systemctl start iptables-services
 [root@localhost ~]# systemctl mask firewalld.service nftables.service

自定义链

类似于函数,将类型相同的规则放入同一个链中

自定义链实现方式
 [root@localhost ~]# iptables -N WEB
 #自定义添加WEB链
 [root@localhost ~]# iptables -A WEB -p tcp -m multiport --dport 80,443 -j ACCEPT
 #创建一个名为WEB的链,使用离散方式定义tcp协议的目的端口80和443,允许通过
 [root@localhost ~]# iptables -A WEB -p tcp --dport 8080 -j ACCEPT
 #在WEB链中添加,允许TCP协议的目的端口8080允许通过的规则
 [root@localhost ~]# iptables -vnL
 Chain INPUT (policy ACCEPT 6 packets, 364 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 4 packets, 552 bytes)
  pkts bytes target     prot opt in     out     source               destination         
 ​
 Chain WEB (0 references)
  pkts bytes target     prot opt in     out     source               destination         
     0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            multiport dports 80,443
     0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8080
 ​
 [root@localhost ~]# iptables -A INPUT -s 192.168.190.130 -j WEB 
 #在INPUR链中添加规则,将WEB链的规则加载到主机192.168.190.130上
 [root@localhost ~]# iptables -vnL --line-num
 Chain INPUT (policy ACCEPT 6 packets, 364 bytes)
 num   pkts bytes target     prot opt in     out     source               destination         
 1        0     0 WEB        all  --  *      *       192.168.190.130      0.0.0.0/0           
 ​
 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 num   pkts bytes target     prot opt in     out     source               destination         
 ​
 Chain OUTPUT (policy ACCEPT 4 packets, 568 bytes)
 num   pkts bytes target     prot opt in     out     source               destination         
 ​
 Chain WEB (1 references)
 num   pkts bytes target     prot opt in     out     source               destination         
 1        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            multiport dports 80,443
 2        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8080
删除自定义链
[root@localhost ~]# iptables -X WEB
 #删除WEB链
 #当WEB链被赋予链接,及链内有规则时,不可以被删除
 ​
 [root@localhost ~]# iptables -vnL --line-num
 Chain INPUT (policy ACCEPT 6 packets, 364 bytes)
 num   pkts bytes target     prot opt in     out     source               destination         
 1        0     0 WEB        all  --  *      *       192.168.190.130      0.0.0.0/0           
 ​
 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 num   pkts bytes target     prot opt in     out     source               destination         
 ​
 Chain OUTPUT (policy ACCEPT 4 packets, 568 bytes)
 num   pkts bytes target     prot opt in     out     source               destination         
 ​
 Chain WEB (1 references)
 num   pkts bytes target     prot opt in     out     source               destination         
 1        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            multiport dports 80,443
 2        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8080
 ​
 [root@localhost ~]# iptables -F INPUT
 #先将与WEB链关联的INPUT规则删除
 [root@localhost ~]# iptables -F WEB
 #再将WEB链内的规则全部清空
 #注意:当有人使用时无法清空
 [root@localhost ~]# iptables -X WEB
 #此时使用-X命令,就可以删除WEB链了
重命名自定义链
[root@localhost ~]# iptables -E WEB NEW
#将WEB链改名为NEW
#修改自定义链时,与删除条件一致,必须清空

你可能感兴趣的:(服务器,linux,网络)