您的位置:首页 > 运维架构 > Linux

ipset-linux防火墙的扩展实现对指定ip的限制访问

2015-09-11 20:31 1816 查看
参考:http://blog.csdn.net/dog250/article/details/41123469 于Linux-2.6.32内核上编译ipset-6.23的坎坷经历 http://netsecurity.51cto.com/art/201501/463157.htm 如何在Linux上高效阻止恶意IP地址? http://blog.csdn.net/opensure/article/details/46047931 ipset - linux防火墙的扩展 http://www.xitongzhijia.net/xtjc/20150106/34147_2.html Linux拒绝国外IP访问 http://blog.chinaunix.net/uid-24683784-id-5020892.html 用ipset配置linux防火墙

安装:
官方网站:http://ipset.netfilter.org/install.html
最简单的方法就是yum安装,但是该方法版本比较低,缺少一些使用的模块参数等,所以不大推荐;
yum install ipset -y

编译安装:

1. 依赖环境: yum install libmnl libmnl-devel kernel-devel libtool-devel -y
(新版本的安装方法:git pull git://git.netfilter.org/libmnl.git 运行./autogen.sh)
=======================================================================================
(备注:如果只安装libmnl时,会出现下面的报错:
checking for libmnl... configure: error: Package requirements (libmnl >= 1) were not met:

No package 'libmnl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables libmnl_CFLAGS
and libmnl_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
)
========================================================================================
在编译的时候可能提示找不到/lib/modules/2.6.32-431.el6.x86_64/source
经过排查发现这个软连接/lib/modules/2.6.32-431.el6.x86_64/build -->/usr/src/kernels/2.6.32-431.el6.x86_64 不存在
解决办法:重新建立软连接
ln -sb /usr/src/kernels/2.6.32-573.3.1.el6.x86_64 /lib/modules/2.6.32-431.el6.x86_64/build
========================================================================================
在运行 ./autogen.sh时报错:
找不到 /usr/share/libtool/
解决办法:安装libtool-devel工具包即可 yum install libtool-devel
========================================================================================
2.编译安装ipset (linux kernel source code (version >= 2.6.32))
wget -P /usr/local/src http://ipset.netfilter.org/ipset-6.26.tar.bz2 cd /usr/local/src && tar xf ipset-6.26.tar.bz2 && cd ipset-6.26
./autogen.sh
./configure --prefix=/usr/local/ipset
make
make modules 不要执行make modules 和make module_install命令,否则在设置iptables规则的时候会卡死内核
make install
make modules_install

附注:linux kernel source code (version >= 2.6.16 or >= 2.4.36)
编译安装:
wget -P /usr/local/src http://ipset.netfilter.org/ipset-4.5.tar.bz2 cd /usr/local/src && tar xf ipset-4.5.tar.bz2 && cd ipset-4.5
make KERNEL_DIR=/lib/modules/$(shell uname -r)/build #$(shell uname -r)使用shell命令获取
make KERNEL_DIR=/lib/modules/$(shell uname -r)/build install
常用使用命令:
ipset list 查看ip集列表信息
ipset create Dos hash:ip maxelem 1000000 创建一个IP集Dos,指定类型为hash:ip,设置ip集最多存储IP数为1000000
ipset add Dos X.X.X.X 增加一个ip地址到IP集Dos中去
ipset add Dos X.X.X.X/24 增加一个网段到IP集Dos中去
ipset dell Dos X.X.X.X 删除IP集中指定的IP地址

ipset save Dos -f Dos.txt 将IP集Dos中的信息保存到当前文件目录下面的文件Dos.txt中
ipset destroy Dos 删除指定的IP集Dos
ipset restore Dos -f Dos.txt 将保存的Dos.txt文件中的IP集信息重新导入到ipset中

其他命令参考 ipset --help

iptable命令参考:
iptables -A INPUT -m set --match-set Dos src -p tcp --destination-port 80 -j DROP
#拒绝ipset IP集Dos中的地址访问服务器的80端口
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息