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

news/2024/7/24 2:35:32 标签: linux, 运维, 服务器

目录

防火墙规则保存

备份工具 iptables services

自定义链

自定义链实现方式

删除自定义链

重命名自定义链


防火墙规则保存

命令: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
#配置完必须将文件赋予执行权限
  • 保存命令

  • 自动配置文件

备份工具 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

自定义链

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

自定义链实现方式
-   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
#修改自定义链时,与删除条件一致,必须清空


http://www.niftyadmin.cn/n/5237484.html

相关文章

【bug排查解决】现象级延迟8-10s

业务背景 最近公司在做物联网相关的项目&#xff0c;调试过程中发现好玩的bug。 首先一个数据采集场景&#xff0c;plc采集数据全链路&#xff1a; kepServer&#xff08;kepserver IOT gateway&#xff09; -> emqx &#xff08;查看日志&#xff09;-> iot服务 -> 业…

新版IDEA中,module模块无法被识别,类全部变成咖啡杯无法被识

新版IDEA中&#xff0c;module模块无法被识别&#xff0c;类全部变成咖啡杯无法被识 如下图&#xff1a; 解决方法&#xff1a;java的Directory文件没有被设置为根目录&#xff0c;解决方法如下&#xff1a; 这是方法之一&#xff0c;还有很多的原因 可能的原因&#xff1a; …

【广州华锐互动】VR沉浸式体验铝厂安全事故让伤害教育更加深刻

随着科技的不断发展&#xff0c;虚拟现实&#xff08;VR&#xff09;技术已经逐渐渗透到各个领域&#xff0c;为我们的生活带来了前所未有的便捷和体验。在安全生产领域&#xff0c;VR技术的应用也日益受到重视。 VR公司广州华锐互动就开发了多款VR安全事故体验系统&#xff0c…

练习十一:简单卷积器的设计

简单卷积器的设计 1&#xff0c;任务目的&#xff1a;2&#xff0c;明确设计任务2.1,目前这部分代码两个文件没找到&#xff0c;见第5、6节&#xff0c;待解决中。 &#xff0c;卷积器的设计&#xff0c;RTL&#xff1a;con1.v4&#xff0c;前仿真和后仿真&#xff0c;测试信号…

Vue3 组合式实现 带连接线的Tree型 架构图(一级树形图)

创建组件名称 TreeNodeView.vue <template><div class"tree-node"><div class"node">{{ rootNodeName }}</div><div class"children" :style"childrenLineStyle"><div class"child-node"…

在龙蜥 anolis os 23 上 源码安装 PostgreSQL 16.1

在龙蜥 OS 23上&#xff0c;本来想使用二进制安装&#xff0c;结果发现没有针对龙蜥的列表&#xff1a; 于是想到了源码安装&#xff0c;下面我们列出了PG源码安装的步骤&#xff1a; 1.安装准备 1.1.创建操作系统组及用户 groupadd postgres useradd -g postgres -m postgr…

Java 设计模式——备忘录模式

目录 1.概述2.结构3.案例实现3.1.“白箱”备忘录模式3.2.”黑箱”备忘录模式 4.优缺点5.使用场景 1.概述 &#xff08;1&#xff09;备忘录模式 (Memento Pattern) 又称为快照模式&#xff0c;是一种行为型设计模式&#xff0c;它提供了一种保存和恢复对象状态的机制。备忘录模…

11.29 知识回顾(视图层、模板层)

一、视图层 1.1 响应对象 响应---》本质都是 HttpResponse -HttpResponse---》字符串 -render----》放个模板---》模板渲染是在后端完成 -js代码是在客户端浏览器里执行的 -模板语法是在后端执行的 -redirect----》重定向 -字符串参数不是…