为linux服务器添加iptables connlimit 模块
为linux服务器添加iptables connlimit 模块为iptables 添加connlimit模块需要为内核打补丁,并需要重新编译内核,网上有文章介绍可以直接将系统版本一致的内核模块直接copy过来就能使用。
rhel 4.4的模块可以直接到wget ftp://ftp.pslib.cz/pub/users/Milan.Kerslager/RHEL-4/stable 下载,下载下来的是rpm包,可以直接安装。
centos 5.2 i386版本可以直接到http://linux.chinaunix.net/bbs/v ... =1032941&extra= 下载
centos 5.2 x86_64位需要自己手工编译安装
centos 5.2 x86_64编译安装iptables connlmit模块
1.下载和操作系统内核版本一致的内核源码文件,可以到http://rpm.pbone.net查询,我这里是kernel-2.6.18-92.1.22.el5.src.rpm
2.安装内核源代码包
rpm -ivh kernel-2.6.18-92.1.22.el5.
src.rpm
我在这里提示错误,error: cannot create %sourcedir /usr/src/redhat/SOURCES
意思是没有/usr/src/redhat/SOURCES目录,手工创建 mkdir -p /usr/src/redhat/SOURCES
3.使用rpmbuild 编译内核rpm包,由于系统是最小化安装会提示没有rpmbuild程序,使用yum安装相关包后,再编译内核rpm包
yum install rpm-build
cd /usr/src/redhat/SPECS
rpmbuild -bp --target=$(uname -m) ./kernel-2.6.spec
4.提示依赖包错误,使用yum安装相关包后,再编译配置
yum install gcc redhat-rpm-config unifdef
5.将内核源文件copy到/usr/src目录
cp -r /usr/src/redhat/BUILD/linux-2.6.18.x86_64/ /usr/src/
修改/usr/src/linux-2.6.18.x86_64下的Makefile文件中的linux内核版本与系统内核版本一致
head -n 4 Makefile
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 18
EXTRAVERSION = -92.1.22.el5
uname -r
2.6.18-92.1.22.el5
6.下载iptables 源文件和patch-o-matic-ng 为内核源码打补丁,添加connlimit模块
下载iptables 时最好是下载老一点的版本,不然可能会提示iptables-1.4.2/ doesn't look like a iptables source code directory to me.错误
目前最新版本为1.4.2版本,在这里我们下载 1.3.8版本。
cd /usr/src
wget http://ftp.netfilter.org/pub/iptables/iptables-1.3.8.tar.bz2
下载最新的patch-o-matic-ng
wget http://ftp.netfilter.org/pub/pat ... ng-20090208.tar.bz2
将iptables-1.3.8.tar.bz2和path-o-matic-ng-20090208.tar.bz2 解压到/usr/src目录下
tar jxvf iptables-1.3.8.tar.bz2
tar jxvf patch-o-matic-ng-20090208.tar.bz2
设置环境patch-o-matic-ng-20090208需要用到的环境变量
export KERNEL_DIR=/usr/src/linux-2.6.18.x86_64/
export KERNEL_SRC=/usr/src/linux-2.6.18.x86_64/
export IPTABLES_SRC=/usr/src/iptables-1.3.8/
export IPTABLES_DIR=/usr/src/iptables-1.3.8/
7.为内核下载补丁,打补丁
cd /usr/src/patch-o-matic-ng-20090208
./runme --download
./runme connlimit
8.编辑内核配置文件,选中新增加的模块
cd linux-2.6.18.x86_64/
make menuconfig
由于是最小化安装系统,在这里会提示
scripts/kconfig/lxdialog/dialog.h:31:20: error: curses.h: No such file or directory
这是由于缺少ncurses软件的原因,使用yum安装ncurses,然后再运行make menuconfig
yum install ncurses-devel ncurses
在内核配置界面选中
Networking --->
Networking options --->
Network packet filtering (replaces ipchains) --->
IP: Netfilter Configuration --->
<M> Connections/IP limit match support
9.编译内核模块
make modules_prepare
修改net/ipv4/netfilter/Makefile,只编译connlimit模块,首先备份net/ipv4/netfilter/Makefile文件
mv net/ipv4/netfilter/Makefile net/ipv4/netfilter/Makefile.bak
新建 net/ipv4/netfilter/Makefile文件,并添加如下内容
vi net/ipv4/netfilter/Makefile
obj-m := ipt_connlimit.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
最后编译内核模块
make M=net/ipv4/netfilter/
10.将编译好的ipt_connlimit.ko内核模块复制到当前内核模块目录下,并加载内核模块
cp net/ipv4/netfilter/ipt_connlimit.ko /lib/modules/2.6.18-92.1.22.el5/kernel/net/ipv4/netfilter/
为内核模块添加可执行权限
chmod +x /lib/modules/2.6.18-92.1.22.el5/kernel/net/ipv4/netfilter/ipt_connlimit.ko
depmod -a
modprobe ipt_connlimit
运行lsmod | grep x_tables出现如下提示,说明内核模块加载成功
x_tables 50377 5 ipt_connlimit,ipt_REJECT,xt_state,xt_tcpudp,ip_tables
11.测试ipt_connlimit模块
iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT 谢谢分享
页:
[1]