系统环境
CentOS 6.5

今天本来可以平静的度过一天,正品味着下午茶的美好,突然接到防火墙iptables的报警。
进入到服务器中,执行下面的命令查看,结果报错

  1. /etc/init.d/iptables  restart
  2. iptables: Applying firewall rules: iptables-restore v1.4.7: iptables-restore: unable to initialize table 'filter'
  3. Error occurred at line: 3
  4. Try `iptables-restore -h' or 'iptables-restore --help' for more information.
  5.                                                            [FAILED]

一个醒目的失败突然出现

为什么重启防火墙就失败了呀,十分不解,看下报错
报错中说 无法找到初始化的表filter,然而策略中第三行正是filter

但是回想了下,防火墙的策略是没人动的呀,用last查看下

  1. last

最近登陆的除了我自己也没别人
难道自己梦游了?用history查看下修改过策略么

  1. history

也没有呀

那是为什么突然报错呢,在网上找了很多相关的文章
他们说的最多的就是执行

  1. iptables -F

强制清空防火墙规则,
于是我将防火墙策略先备份,备份大于一切

  1. cd /etc/sysconfig;cp iptables iptables.2018bak

执行iptables -F
结果又报错了!

  1. [root@zclinux ~]# iptables -F
  2. iptables v1.4.7: can't initialize iptables table `filter': iptables who? (do you need to insmod?)
  3. Perhaps iptables or your kernel needs to be upgraded.

这又是啥! 还是提示filter无法找到

查看下防火墙状态是开启还是已经关闭了
结果又有报错

  1. /etc/init.d/iptables status
  2. iptables: Firewall modules are not loaded.

但是报错不一样了,提示我防火墙的模块没有被加载。什么鬼!防火墙模块没有被加载

那我就查看下防火墙模块是否加载了

  1. lsmod | grep iptable

成功执行,但是没有任何有价值的内容,这和我预想不得一样啊
那我手动加载一下试试

  1.  modprobe  ip_tables 
  2.  modprobe  iptable_filter

上面两句都是在网上找的。地址如下:
https://blog.csdn.net/gbenson/article/details/51829585

但是还是没有回显,那还是加载失败呀。
突然想到,防火墙规则要清空的话,我手动清空试下,就将防火墙的配置文件iptables中只留下了头部和COMMIT再次重启,还是老错误

  1. /etc/init.d/iptables  restart
  2. iptables: Applying firewall rules: iptables-restore v1.4.7: iptables-restore: unable to initialize table 'filter'
  3. Error occurred at line: 3
  4. Try `iptables-restore -h' or 'iptables-restore --help' for more information.
  5.                                                            [FAILED]

那我先停止在开启呢
于是分别执行如下命令:

  1. /etc/init.d/iptables stop
  2. /etc/init.d/iptables start

我去,都执行成功了,但是为啥没显示ok呢,回忆里都是会有一个ok的呀,不管了,查看下状态

  1. /etc/init.d/iptables status
  2. iptables: Firewall modules are not loaded.

还是报错

同事推荐我在别的服务器中拷贝一个好的iptables文件试下,看是不是文件损坏,结果拷贝回来放在服务器中替换掉,还是报错,报错如上,升级yum,升级内核,升级防火墙 ,reinstall防火墙,reboot全都无果,特别难受,绝望的边缘!!

算了,还是从内核入手看下,他说的模块有问题,我记得加载模块一种是自动查找,一种是手动加载
查看下相关的文章:
文章地址:http://blog.51cto.com/qidai/1835782

我这一看,这第二种刚好符合我的要求,和我的报错类似,但是不同
照着做下;

  1. #modprobe -l|grep iptable
  2. kernel/net/ipv4/netfilter/iptable_filter.ko
  3. kernel/net/ipv4/netfilter/iptable_mangle.ko
  4. kernel/net/ipv4/netfilter/iptable_nat.ko
  5. kernel/net/ipv4/netfilter/iptable_raw.ko
  6. kernel/net/ipv4/netfilter/iptable_security.ko

有希望,找到了那个有问题的内核iptable_filter.ko
查看下内核的相关信息

  1. modinfo iptable_filter
  2. filename:       /lib/modules/`uname -r`/kernel/net/ipv4/netfilter/iptable_filter.ko
  3. description:    iptables filter table
  4. author:         Netfilter Core Team <coreteam@netfilter.org>
  5. license:        GPL
  6. retpoline:      Y
  7. srcversion:     666666666666666666668 --已打码
  8. depends:        ip_tables
  9. vermagic:       `uname -r` SMP mod_unload modversions 
  10. parm:           forward:bool

导入试下,结果全都失败了

  1. insmod /lib/modules/`uname -r`/kernel/net/ipv4/netfilter/iptable_filter.ko
  2. insmod: error inserting '/lib/modules/`uname -r`/kernel/net/ipv4/netfilter/iptable_filter.ko': -1 Unknown symbol in module
  3. modprobe /lib/modules/`uname -r`kernel/net/ipv4/netfilter/iptable_filter.ko
  4. FATAL: Module /lib/modules/`uname -r`/kernel/net/ipv4/netfilter/iptable_filter.ko not found.

我这回直接进到路径底下查看是否存在这个内核:

  1. cd /lib/modules/`uname -r`/kernel/net/ipv4/netfilter
  2. ...
  3. -rwxr--r-- 1 root root  8960 Oct 10 00:42 iptable_filter.ko
  4. -rwxr--r-- 1 root root  9008 Oct 10 00:42 iptable_mangle.ko
  5. -rwxr--r-- 1 root root 16232 Oct 10 00:42 iptable_nat.ko
  6. -rwxr--r-- 1 root root  7248 Oct 10 00:42 iptable_raw.ko
  7. -rwxr--r-- 1 root root  7904 Oct 10 00:42 iptable_security.ko
  8. -rwxr--r-- 1 root root 37512 Oct 10 00:42 ip_tables.ko
  9. ....

这正是我所需要的,哈哈哈哈哈
再次执行命令:

  1. [root@zclinux netfilter]# insmod ip_tables.ko       #我感觉应该限制性这个,才对,再执行后面的
  2. [root@zclinux netfilter]# insmod iptable_filter.ko 
  3. #lsmod | grep ip #查看下是否导入成功
  4. iptable_filter          2793  0 
  5. ip_tables              17895  1 iptable_filter
  6. ipv6                  337436  30 

nice 导入成功

  1. /etc/init.d/iptables restart
  2. iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
  3. iptables: Flushing firewall rules:                         [  OK  ]
  4. iptables: Unloading modules:                               [  OK  ]
  5. iptables: Applying firewall rules: iptables-restore v1.4.7: iptables-restore: unable to initialize table 'filter'
  6. Error occurred at line: 2
  7. Try `iptables-restore -h' or 'iptables-restore --help' for more information.
  8.                                                            [FAILED]

还是failed! what 不对,换个思路

  1. iptables -vnL
  2. iptables v1.4.7: can't initialize iptables table `filter': iptables who? (do you need to insmod?)
  3. Perhaps iptables or your kernel needs to be upgraded.

还是报错,但是已经导进去了呀!
查看下:

  1. #lsmod | grep ip 
  2. ipv6                  337436  30 

没了,一个都没有了

再导入一遍

  1. [root@zclinux netfilter]# insmod ip_tables.ko       
  2. [root@zclinux netfilter]# insmod iptable_filter.ko 
  3. #lsmod | grep ip #查看下是否导入成功
  4. iptable_filter          2793  0 
  5. ip_tables              17895  1 ipt

这回我先执行stop

  1. /etc/init.d/iptables stop
  2. iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
  3. iptables: Flushing firewall rules:                         [  OK  ]
  4. iptables: Unloading modules:                               [  OK  ]

good 关闭正常,看到ok瞬间清爽,在开启!

  1.  /etc/init.d/iptables start
  2. iptables: Applying firewall rules: iptables-restore v1.4.7: iptables-restore: unable to initialize table 'filter'
  3. Error occurred at line: 3
  4. Try `iptables-restore -h' or 'iptables-restore --help' for more information.
  5.                                                            [FAILED]

一千万至羊驼从身边踏过!

再来,先看下防火墙状态是否是关闭

  1. /etc/init.d/iptables status
  2. iptables: Firewall is not running.  

ok 已经不显示模块加载失败了,成功仅在咫尺

  1. lsmod | grep ip #查看下模块还在吗
  2. ipv6                  337436  30 

再来一遍

  1. [root@zclinux netfilter]# insmod ip_tables.ko
  2. [root@zclinux netfilter]# insmod iptable_filter.ko 
  3. [root@zclinux netfilter]# lsmod | grep ip
  4. iptable_filter          2793  0 
  5. ip_tables              17895  1 iptable_filter
  6. ipv6                  337436  30

加载成功!
启动防火墙!

  1. [root@zclinux netfilter]# /etc/init.d/iptables start
  2. iptables: Applying firewall rules:                         [  OK  ]

启动防火墙成功!!!!
查看下策略咋样

  1. [root@zclinux netfilter]# iptables -vnL
  2. Chain INPUT (policy ACCEPT 295 packets, 34096 bytes)
  3.  pkts bytes target     prot opt in     out     source               destination         
  4. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  5.  pkts bytes target     prot opt in     out     source               destination         
  6. Chain OUTPUT (policy ACCEPT 270 packets, 41311 bytes)
  7.  pkts bytes target     prot opt in     out     source               destination 

忘了把策略加进去了,再来

  1. [root@zclinux netfilter]# /etc/init.d/iptables  stop
  2. iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
  3. iptables: Flushing firewall rules:                         [  OK  ]
  4. iptables: Unloading modules:                               [  OK  ]
  1. [root@zclinux netfilter]# /etc/init.d/iptables start
  2. iptables: Applying firewall rules: iptables-restore v1.4.7: iptables-restore: unable to initialize table 'filter'
  3. Error occurred at line: 3
  4. Try `iptables-restore -h' or 'iptables-restore --help' for more information.
  5.     
  6.                                                       [FAILED]

再来一遍

  1. [root@zclinux netfilter]# lsmod | grep ip
  2. ipv6                  337436  30 
  3. [root@zclinux netfilter]# insmod ip_tables.ko
  4. [root@zclinux netfilter]# insmod iptable_filter.ko 
  5. [root@zclinux netfilter]# lsmod | grep ip
  6. iptable_filter          2793  0 
  7. ip_tables              17895  1 iptable_filter
  8. ipv6                  337436  30 
  9. [root@zclinux netfilter]# /etc/init.d/iptables start
  10. iptables: Applying firewall rules:                         [  OK  ]
  11. [root@zclinux netfilter]# iptables -vnL

哈哈哈哈哈 规则回来了,都在抓紧写个脚本进行检测

  1. [root@zclinux ~]# vi checkiptables_module.sh
  2. #!/bin/bash
  3. DATE=`date '+%Y-%m-%d %H:%M:%S' `
  4. check=`lsmod | grep ip | grep iptable | awk -F" " '{print $4}'| grep ip`
  5. if [ "$check" -eq 'iptable_filter' ]
  6. then
  7.         echo  $DATE "- iptables is ok ! " >>/tmp/iptables_check.log
  8. else
  9.         /etc/init.d/iptables stop
  10.         cd /lib/modules/`uname -r`/kernel/net/ipv4/netfilter;
  11.         insmod ip_tables.ko;
  12.         insmod iptable_filter.ko;
  13.         /etc/init.d/iptables start;
  14.         echo $DATE "-iptables is restore ! " >> /tmp/iptables_check.log
  15. fi
  16. chmod +x /root/checkiptables_module.sh

把脚本加如到执行计划中

  1. crontab -e
  2. * */1 * * * /root/checkiptables_module.sh

收官,不知道为啥,这个防火墙内核只能这样了,文中所有涉及到服务器的真实信息已被打码修改。

------------------------------分割线---------------------------------------------------------

环境centos7.3

问题: 出现iptables -vnL
iptables v1.4.21: can't initialize iptables table `filter': Table does not exist (do you need to insmod?)
Perhaps iptables or your kernel needs to be upgraded.

和上述问题一直,但细节部分有点不同

还是调用内核模块

  1. lsmod | grep ip

发现里面没有任何显示,这就证明iptables的几个内核有问题

尝试手动加载内核

  1. modprobe ip_tables
  2. modprobe ip_tables

输入完成后,再次查看,还是没有任何内核关于iptables的模块被加载

执行全部内核加载命令:

  1. depmod -a

但是突然报错:

#depmod -a
depmod: ERROR: could not open directory /lib/modules/3.10.0-514.el7.x86_64: No such file or directory
depmod: FATAL: could not search modules: No such file or directory

提示找不到文件

这句很重要,找不到文件的话,就无法加载内核

手动查找下

  1. find / -name 3.10.0-514.el7.x86_64
  2. /usr/lib/modules/3.10.0-514.el7.x86_64

只有这个下面有一个,手动复制一份到/lib下

  1. cp -r /lib/ /lib.bak/
  2. cp -r /usr/lib/* /lib/

再次执行

  1. depmod -a

执行成功

查看模块加载情况:

  1. lsmod | grep ip

手动添加

  1. modprobe ip_tables
  2. modprobe ip_tables

再次查看添加成功

  1. lsmod | grep ip
  2. iptable_filter 12810 0
  3. ip_tables 27115 1 iptable_filter

iptables -vnL正常

在没加载模块之前,防火墙重启后,持续报这个错误:

systemctl  restart iptables  
Job for iptables.service failed because the control process exited with error code. See "systemctl status iptables.service" and "journalctl -xe" for details.

查看状态后:

systemctl  status iptables
? iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2019-01-08 12:33:38 ICT; 38s ago
  Process: 2959 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=1/FAILURE)
 Main PID: 2959 (code=exited, status=1/FAILURE)

Jan 08 12:33:38 manager systemd[1]: Starting IPv4 firewall with iptables...
Jan 08 12:33:38 manager iptables.init[2959]: iptables: Applying firewall rules: iptables-restore v1.4.21: iptabl...lter'
Jan 08 12:33:38 manager iptables.init[2959]: Error occurred at line: 4
Jan 08 12:33:38 manager iptables.init[2959]: Try `iptables-restore -h' or 'iptables-restore --help' for more inf...tion.
Jan 08 12:33:38 manager iptables.init[2959]: [FAILED]
Jan 08 12:33:38 manager systemd[1]: iptables.service: main process exited, code=exited, status=1/FAILURE
Jan 08 12:33:38 manager systemd[1]: Failed to start IPv4 firewall with iptables.
Jan 08 12:33:38 manager systemd[1]: Unit iptables.service entered failed state.
Jan 08 12:33:38 manager systemd[1]: iptables.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
[root@manager ~]# lsmod | grep ipta
[root@manager ~]# lsmod | grep ip
[root@manager ~]# depmod -a
depmod: ERROR: could not open directory /lib/modules/3.10.0-514.el7.x86_64: No such file or directory
depmod: FATAL: could not search modules: No such file or directory

网上很多都说是由于firewalld服务造成的,于是关闭firewalld

systemctl stop firewalld

sysetmctl disable firewalld

但重启还是报错,这个不是问题的根本

于是尝试上述的加载内核成功后,重启iptables,启动也成功

定位问题要快准狠,多看日志多看报错

总结下还是/lib路径下的文件有产生变化,和/usr/lib下的文件不同步造成,手动复制到/lib下,正常启动

【Linux】iptables的内核模块问题大坑!的更多相关文章

  1. linux iptables常用命令之配置生产环境iptables及优化

    在了解iptables的详细原理之前,我们先来看下如何使用iptables,以终为始,有可能会让你对iptables了解更深 所以接下来我们以配置一个生产环境下的iptables为例来讲讲它的常用命令 ...

  2. Linux iptables原理--数据包流向

    Iptable与Netfilter 在上一篇文章 linux iptables常用命令--配置一个生产环境的iptables 我们知道iptables有好几个表,如raw,mangle,nat,fil ...

  3. Linux iptables

    一.简介 http://liaoph.com/iptables/ 二.操作 1)查看规则 iptables -t filter -L -n iptables -t nat -L -n iptables ...

  4. Linux驱动设计—— 内核模块(一)

    Linux内核理论基础 组成Linux内核的5个子系统:进程调度(SCHED)/内存管理(MM)/虚拟文件系统(VFS)/网络接口(NET)/进程间通信(IPC). 进程调度(SCHED) 在设备驱动 ...

  5. Linux iptables 应用控制访问SSH服务

    Title:Linux iptables 应用控制访问SSH服务  --2012-02-23 17:51 今天用到了以前从来没有用到过的,iptables控制访问,只允许外部访问SSH服务(22号端口 ...

  6. Linux iptables 配置规则

    Linux iptables 防火墙配置规则 前言:把网上我感觉不错iptables的访问规则都统一在这里,以后做参考. modprobe ipt_MASQUERADE modprobe ip_con ...

  7. Linux iptables用法与NAT

    1.相关概念 2.iptables相关用法 3.NAT(DNAT与SNAT) 相关概念 防火墙除了软件及硬件的分类,也可对数据封包的取得方式来分类,可分为代理服务器(Proxy)及封包过滤机制(IP ...

  8. [svc]linux iptables实战

    参考: http://blog.51yip.com/linux/1404.html 链和表 参考: https://aliang.org/Linux/iptables.html 配置 作为服务器 用途 ...

  9. 常用的 Linux iptables 规则

    一些常用的 Linux iptables 规则,请根据自己的具体需要再修改. 转载自:http://mp.weixin.qq.com/s/uAPzh9_D4Qk6a3zBh7Jq5A # 1. 删除所 ...

随机推荐

  1. sqli-labs less13-20(各种post型头部注入)

    less-13 POST型双查询注入 less-14 POST型双查询注入 less-15 POST型布尔注入 less-16 POST型布尔注入 less-17 POST型报错注入(updatexm ...

  2. 五、Jmeter的目录结构

    进入安装Jmeter可以看到路径 bin目录 jmeter.bat  windows的启动文件 jmeter.log jmeter运行日志文件 jmeter.sh linux的启动文件 jmeter. ...

  3. 我叫Mongo,干了「索引探索篇」提升我的效率,值得您拥有

    这是mongo第四篇"索引探索",后续会连续更新4篇 mongodb的文章总结上会有一系列的文章,顺序是先学会怎么用,在学会怎么用好,戒急戒躁,循序渐进,跟着我一起来探索交流.通过 ...

  4. vue 属性绑定 v-bind

    属性绑定 v-bind 可以通过v-bind将属性值与数据绑定,这样就可以统一化管理 通过这样我们就可以直接访问跳转到百度页面 同样的这个值我们也可以通过事件进行改变 这样就可以方便我们做一些其它的操 ...

  5. create-react-app 基于TS的项目

    写在前面 最近在用React,发现百度了很多都没有找到基于TS的React项目搭建,很多是老的方法已经属于不成功的了,今天我把最新的搭建基于ts的React的项目分享出来 create-react-a ...

  6. Javascript之Firefox与IE

    IE其实相对来讲并不是规范的遵循者,错怪firefox了. 2020注:IE看来要退出市场了,这些也逐渐成为历史了.:) 1firefox不支持iframe.document, 而IE支持,所以对fi ...

  7. matplotlib的学习16-animation动画

    from matplotlib import pyplot as plt from matplotlib import animation import numpy as np fig, ax = p ...

  8. javaScript继承的几种实现方式?

    js继承总共分成5种,包括构造函数式继承.原型链式继承.组合式继承.寄生式继承和寄生组合式继承. 构造函数式继承 首先来看第一种,构造函数式继承,顾名思义,也就是利用函数去实现继承:构造函数继承,使用 ...

  9. java基础模拟考试三套试卷

    卷1: 一.单选题(15道,共30分) //1.下列关于JDK.JRE.JVM关系描述正确的是 (A) A:JDK是开发工具,包含了JRE.JRE是运行环境,包含了JVM.JVM是虚拟机,可以保证跨平 ...

  10. 简单介绍下各种 JavaScript 解析器

    作者:沧海 各种js解析器是前端工程化的基石,可以说如果没有它,很多工程化都无法正常执行,我们每天用到的babel.webpack.eslint.TypeScript背后都需要一套对应的js解析器,今 ...