当前位置:   article > 正文

防火墙(三) -----------------iptables规则的保存_iptables 保存

iptables 保存

目录

一、关于iptables规则的保存

①防火墙iptables规则保存

让防火墙规则在开机后自动运行

方法一:

方法二:

开机自动加载规则

②使用iptables-services软件来进行规则的保存和加载(不建议使用)

二、自定义链

1.添加自定义链

2.给自定义链添加规则

3.使用自定义链

4.删除自定义链


一、关于iptables规则的保存

之前写的iptables的设置,但是都是临时生效的,一旦电脑重启,那么就会失效,如何永久保存,需要借助iptables-save命令,开机生效需要借助iptables-restore命令,并写入规定的配置文件中。

  1. [root@zzcentos1 ~]#iptables-save > /opt/iprule
  2. [root@zzcentos1 ~]#cat /opt/iprule
  3. # Generated by iptables-save v1.4.21 on Sun Feb 18 18:32:36 2024
  4. *filter
  5. :INPUT ACCEPT [0:0]
  6. :FORWARD ACCEPT [0:0]
  7. :OUTPUT ACCEPT [59:4744]
  8. -A INPUT -s 192.168.246.0/24 -j ACCEPT
  9. -A INPUT -s 192.168.246.0/24 -p icmp -j DROP
  10. COMMIT
  11. # Completed on Sun Feb 18 18:32:36 2024
  12. [root@zzcentos1 ~]#iptables -F
  13. [root@zzcentos1 ~]#iptables -vnL
  14. Chain INPUT (policy ACCEPT 20 packets, 1168 bytes)
  15. pkts bytes target prot opt in out source destination
  16. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  17. pkts bytes target prot opt in out source destination
  18. Chain OUTPUT (policy ACCEPT 11 packets, 908 bytes)
  19. pkts bytes target prot opt in out source destination
  20. [root@zzcentos1 ~]#iptables-restore < /opt/iprule
  21. [root@zzcentos1 ~]#iptables -vnL
  22. Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
  23. pkts bytes target prot opt in out source destination
  24. 45 2888 ACCEPT all -- * * 192.168.246.0/24 0.0.0.0/0
  25. 0 0 DROP icmp -- * * 192.168.246.0/24 0.0.0.0/0
  26. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  27. pkts bytes target prot opt in out source destination
  28. Chain OUTPUT (policy ACCEPT 36 packets, 2760 bytes)
  29. pkts bytes target prot opt in out source destination
  30. [root@zzcentos1 ~]#

防火墙iptables规则保存

让防火墙规则在开机后自动运行

方法一:

方法二:

开机自动加载规则

第一种:个性化设置放到用户家目录下的配置文件~/.bashrc

第二种:放到全局配置文件中,/etc/profile中,对所有用户生效

第三种:放到开机自启的最后一项,加载系统服务的配置文件中,可以让系统重启即可生效

综上所述,建议放在系统自带的配置/etc/rc.d/rc.local中

使用iptables-services软件来进行规则的保存和加载(不建议使用)

  1. [root@centos7 ~]#yum -y install iptables-services
  2. [root@centos7 ~]#cp /etc/sysconfig/iptables{,.bak}
  3. #保存现在的规则到文件中方法1
  4. [root@centos7 ~]#/usr/libexec/iptables/iptables.init save
  5. #保存现在的规则到文件中方法2
  6. [root@centos7 ~]#iptables-save > /etc/sysconfig/iptables
  7. #开机启动
  8. [root@centos7 ~]#systemctl enable iptables.service    
  9. [root@centos7 ~]#systemctl mask firewalld.service nftables.service

  1. [root@zzcentos1 ~]#cp /etc/sysconfig/iptables{,.bak}
  2. [root@zzcentos1 ~]#cd /etc/sysconfig/
  3. [root@zzcentos1 sysconfig]#ls
  4. anaconda crond iptables.bak netconsole rdisc selinux
  5. atd ebtables-config iptables-config network readonly-root smartmontools
  6. authconfig fcoe irqbalance network-scripts rpcbind sshd
  7. autofs firewalld kdump nfs rpc-rquotad sysstat
  8. cbq grub kernel ntpd rsyncd sysstat.ioconf
  9. cgred init ksm ntpdate rsyslog virtlockd
  10. chronyd ip6tables libvirtd qemu-ga run-parts virtlogd
  11. console ip6tables-config man-db radvd samba wpa_supplicant
  12. cpupower iptables modules raid-check saslauthd
  13. [root@zzcentos1 sysconfig]#iptables-save > /etc/sysconfig/iptables
  14. [root@zzcentos1 sysconfig]#systemctl enable iptables.service
  15. Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
  16. [root@zzcentos1 sysconfig]#reboot

二、自定义链

  1. [root@localhost ~]#iptables -t filter -N WEB
  2. ##指定是在filter下面添加WEB链,不写默认是filter

1.添加自定义链

一般链是大写

2.给自定义链添加规则

  1. [root@zzcentos1 ~]#iptables -A WEB -p tcp -m multiport --dport 80,443 -j ACCEPT
  2. [root@zzcentos1 ~]#iptables -I WEB -s 192.168.246.8 -p tcp --dport 80 -j DROP

随意添加两条规则

3.使用自定义链

当有流量经过时,进入INPUT链时,会去自定义链WEB中匹配规则

这样优点:可以把相同规则放在一起,方便管理

4.删除自定义链

  1. [root@zzcentos1 ~]#iptables -X WEB
  2. iptables: Too many links.
  3. [root@zzcentos1 ~]#iptables -F WEB
  4. [root@zzcentos1 ~]#iptables -vnL
  5. Chain INPUT (policy ACCEPT 8 packets, 488 bytes)
  6. pkts bytes target prot opt in out source destination
  7. 92 6417 WEB all -- * * 0.0.0.0/0 0.0.0.0/0
  8. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  9. pkts bytes target prot opt in out source destination
  10. Chain OUTPUT (policy ACCEPT 5 packets, 460 bytes)
  11. pkts bytes target prot opt in out source destination
  12. Chain WEB (1 references)
  13. pkts bytes target prot opt in out source destination
  14. [root@zzcentos1 ~]#iptables -F INPUT
  15. [root@zzcentos1 ~]#
  16. [root@zzcentos1 ~]#iptables -X WEB
  17. [root@zzcentos1 ~]#iptables -vnL
  18. Chain INPUT (policy ACCEPT 8 packets, 488 bytes)
  19. pkts bytes target prot opt in out source destination
  20. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  21. pkts bytes target prot opt in out source destination
  22. Chain OUTPUT (policy ACCEPT 5 packets, 460 bytes)
  23. pkts bytes target prot opt in out source destination
  24. [root@zzcentos1 ~]#

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号