---
title: "升级一下iptables"
url: "https://yjyj.net/learn/centos/3208.html"
author: "小 虾 (生猛大蝦编辑部)"
published_at: "2013-07-04T05:26:14+08:00"
updated_at: "2013-07-04T05:37:35+08:00"
categories: "CentOS 学习"
tags: "CentOS教程, Linux运维, 服务器安全"
summary: "本文围绕 CentOS 服务器在高并发下因连接数异常导致爆内存、自动重启的排查与修复展开，重点记录了 iptables 的 connlimit-saddr 规则报错、版本过低的判断，以及升级编译安装的完整折腾过程，并补充了 OOM 自动重启的 sysctl 方案与风险提醒。"
source: "生猛大蝦 (https://yjyj.net)"
---

# 升级一下iptables

> 来源：[生猛大蝦 (yjyj.net)](https://yjyj.net/learn/centos/3208.html) · 作者：小 虾 · 发布时间：2013-07-04

## 30 秒核心看点 (TL;DR)

- iptables 的 connlimit-saddr 规则在旧版本里报错，核心怀疑是版本太老。
- 作者尝试 yum 卸载旧包、源码编译升级 iptables，但问题最终仍未解决。
- 文末补充 OOM 自动重启思路：开启 panic_on_oom 和 kernel.panic，但要警惕数据损坏。

不知道为啥最近服务器总是爆内存死掉
多方求助下东哥给了一段流弊的解决方案:

 
 

```
iptables -A INPUT -p tcp --dport 80 --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above 10 --connlimit-mask 32 --connlimit-saddr -j REJECT --reject-with tcp-reset

iptables-save >/etc/sysconfig/iptables
```

 

但是我在运行的时候却提示 connlimit-saddr 有问题.

查了一番估计可能是iptables版本过低,赶紧升级一下:

 

```
yum update 
rpm -e --nodeps iptables-1.4.7-9.el6.i686
rpm -e --nodeps iptables-ipv6-1.4.7-9.el6.i686
yum groupinstall 'Development Tools'
wget http://www.netfilter.org/projects/iptables/files/iptables-1.4.19.tar.bz2
tar jxvf iptables-1.4.19.tar.bz2
cd iptables-1.4.19
LDFLAGS="-L$PWD/libiptc/.libs" ./configure --prefix=/usr --exec-prefix= --bindir=/usr/bin --with-xtlibdir=/lib/xtables --with-pkgconfigdir=/usr/lib/pkgconfig --enable-libipq --enable-devel
make
make install
```

 


不幸的是折腾一番后问题还是未解决...
明天继续吧.悲囧



linode关于爆内存重启的解决办法:
https://www.linode.com/wiki/index.php/Rebooting_on_OOM



Rebooting on OOM

In some situations, it may be beneficial to have your Linode automatically reboot if it runs out of memory. You can leverage a couple kernel settings and Lassie to make this happen.
NOTE: This is an inherently unclean reboot. You will encounter disk corruption or other tragedies if this occurs at a bad time. Additionally, if you end up in a situation where you run out of memory right after boot, you'll enter an unclean reboot loop which will increase your chances of having a tragic day. The most important rule is to always ensure you have up-to-date, available, off-site backups of your Linode's data. In the event non-backed-up data is lost, please send a self-addressed, stamped envelope to World's Smallest Violin Giveaway, Box 17821, Pound Ridge Station.


Enable Lassie

Lassie, the Linode Autonomous System Shutdown Intelligent rEbooter, will issue a boot command should it find your kernel not running. You can ensure this is enabled from the Dashboard at Settings & Utilities -> Shutdown Watchdog.


Edit /etc/sysctl.conf

Adding the following two lines to your /etc/sysctl.conf will cause it to reboot after running out of memory:

 

```
vm.panic_on_oom=1
kernel.panic=10
```

 


The vm.panic_on_oom=1 line enables panic on OOM; the kernel.panic=10 line tells the kernel to reboot ten seconds after panicking.
[edit]Make the change live

You can make the change live now (vs. on a reboot) with:


 

```
echo 1 > /proc/sys/vm/panic_on_oom
echo 10 > /proc/sys/kernel/panic
```

## 延伸问答与排错指南 (FAQ)

### iptables 提示 connlimit-saddr 有问题怎么办？
先判断是不是 iptables 版本过低或模块不完整。本文做法是先卸载旧版，再源码编译升级到较新的 iptables 版本；如果仍报错，就要继续检查内核模块、规则语法和编译参数是否匹配当前系统。

### 服务器爆内存后自动重启靠谱吗？
可以作为兜底手段，但不算安全方案。文中提到通过 vm.panic_on_oom=1 和 kernel.panic=10 让系统在 OOM 后自动重启，不过这属于非正常重启，可能带来文件损坏和数据丢失，必须先做好备份。

### 这篇文章的实操重点是什么？
重点不是单纯升级软件，而是排查“规则报错—版本兼容—系统级兜底”这条链路。先确认 iptables 是否支持所需参数，再决定升级或重编译；如果目标是防止死机，还要结合 OOM 自动重启策略一起看。

---
更多 Linux 运维、WordPress 性能优化、前端工程化、Shopify 独立站与极客折腾指南：[生猛大蝦全站文章归档 (https://yjyj.net/archive)](https://yjyj.net/archive)
