服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - linux中ipset命令的使用方法详解

linux中ipset命令的使用方法详解

2022-02-21 17:37lijiaocn Linux

ipset是linux kernel的一个功能,可以将ip等组合成一个ipset,在iptables中可以直接指定ipset。下面这篇文章主要给大家介绍了关于linux中ipset命令的使用方法,文中介绍的非常详细,需要的朋友们下面来一起学习学习吧。

ipset介绍

iptables是在linux内核里配置防火墙规则的用户空间工具,它实际上是netfilter框架的一部分.可能因为iptables是netfilter框架里最常见的部分,所以这个框架通常被称为iptables,iptables是linux从2.4版本引入的防火墙解决方案.

ipset是iptables的扩展,它允许你创建 匹配整个地址sets(地址集合) 的规则。而不像普通的iptables链是线性的存储和过滤,ip集合存储在带索引的数据结构中,这种结构即时集合比较大也可以进行高效的查找.

除了一些常用的情况,比如阻止一些危险主机访问本机,从而减少系统资源占用或网络拥塞,IPsets也具备一些新防火墙设计方法,并简化了配置.

官网:http://ipset.netfilter.org/

安装

?
1
2
3
4
5
6
rpm -ivh libmnl-devel-1.0.2-3.el6.x86_64.rpm libmnl-1.0.2-3.el6.x86_64.rpm
tar xvf ipset-6.24.tar.bz2
cd ipset-6.24
./configure
make
make install

#注意:

如果在centos6.6或其他情况下安装时候,configure报错如下

?
1
configure: error: Invalid kernel source directory /lib/modules/2.6.32-358.el6.x86_64/source

解决:需要安装内核源码包kernel-devel-2.6.32-358.el6.x86_64.rpm

创建ipset

ipset -n或者ipset create:

?
1
n, create SETNAME TYPENAME [ CREATE-OPTIONS ]

SETNAME是创建的ipset的名称,TYPENAME是ipset的类型:

?
1
TYPENAME := method:datatype[,datatype[,datatype]]

method指定ipset中的entry存放的方式,随后的datatype约定了每个entry的格式。

可以使用的method:

?
1
bitmap, hash, list

可以使用的datatype:

?
1
ip, net, mac, port, iface

添加记录

ipset add用于在ipset中添加记录:

?
1
add SETNAME ADD-ENTRY [ ADD-OPTIONS ]

向ipset中添加entry的时候,加入的entry的格式必须与创建ipset是指定的格式匹配。

?
1
2
3
4
5
6
7
8
9
10
11
12
$ipset creat foo hash:ip,port,ip
$ipset add foo ipaddr,portnum,ipaddr
 
$ipset list foo
Name: foo
Type: hash:ip,port,ip
Revision: 2
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 16584
References: 0
Members:
192.168.1.2,tcp:80,192.168.1.3

删除记录

ipset del用于从ipset中删除记录:

?
1
del SETNAME DEL-ENTRY [ DEL-OPTIONS ]

查询记录

ipset test可以检查目标entry是否在ipset中:

?
1
test SETNAME TEST-ENTRY [ TEST-OPTIONS ]

ipset list可以查看ipset的所有内容:

?
1
list [ SETNAME ] [ OPTIONS ]

导出导入

ipset save可以导出所有的ipset:

?
1
save [ SETNAME ]

ipset restore则用于将导出的内容导入。

其它

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
flush [ SETNAME ]
  Flush all entries from the specified set or flush all sets if none is given.
 
e, rename SETNAME-FROM SETNAME-TO
  Rename a set. Set identified by SETNAME-TO must not exist.
 
w, swap SETNAME-FROM SETNAME-TO
  Swap the content of two sets, or in another words, exchange the name of two sets. The referred sets must exist and identical type of sets can be swapped only.
 
help [ TYPENAME ]
  Print help and set type specific help if TYPENAME is specified.
 
version
  Print program version.
 
-  If a dash is specified as command, then ipset enters a simple interactive mode and the commands are read from the standard input. The interactive mode can be finished by entering the
  pseudo-command quit.

在iptables中使用ipset

在iptables中可以使用-m set启用ipset模块,例如。

?
1
-A POSTROUTING -m set --match-set felix-masq-ipam-pools src -m set ! --match-set felix-all-ipam-pools dst -j MASQUERADE

iptables的set模块:

?
1
2
3
4
5
6
7
8
set
This module matches IP sets which can be defined by ipset(8).
 
[!] --match-set setname flag[,flag]...
  where flags are the comma separated list of src and/or dst specifications and there can be no more than six of them. Hence the command
 
  iptables -A FORWARD -m set --match-set test src,dst
...

在TARGET中也可以操作ipset:

?
1
2
3
4
5
6
7
8
9
10
11
SET
This module adds and/or deletes entries from IP sets which can be defined by ipset(8).
 
--add-set setname flag[,flag...]
  add the address(es)/port(s) of the packet to the set
 
--del-set setname flag[,flag...]
  delete the address(es)/port(s) of the packet from the set
 
  where flag(s) are src and/or dst specifications and there can be no more than six of them.
...

man iptables-extensions中可以找到set moduleSET TARGET的所有选项。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://www.lijiaocn.com/%E6%8A%80%E5%B7%A7/2017/08/06/linux-net-ipset.html

延伸 · 阅读

精彩推荐
  • LinuxLinux lnmp下无法使用mail发邮件的两种解决方法

    Linux lnmp下无法使用mail发邮件的两种解决方法

    在配置了lnmp环境后,出现了mail函数不能发送邮件的问题,其实有两种方法,一是使用sendmail组件,而是使用postfix。下面为大家一一介绍下 ...

    Linux之家4042019-09-17
  • LinuxLinux中环境变量配置的步骤详解

    Linux中环境变量配置的步骤详解

    Linux中环境变量包括系统级和用户级,系统级的环境变量是每个登录到系统的用户都要读取的系统变量,而用户级的环境变量则是该用户使用系统时加载的...

    Myths7882022-02-10
  • Linuxlinux中rmdir命令使用详解(删除空目录)

    linux中rmdir命令使用详解(删除空目录)

    今天学习一下linux中命令: rmdir命令。rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的 ...

    linux命令大全5372019-11-19
  • LinuxLinux上设置用户通过SFTP访问目录的权限的方法

    Linux上设置用户通过SFTP访问目录的权限的方法

    这篇文章主要介绍了Linux上设置用户通过SFTP访问目录的权限的方法,SFTP可以理解为使用SSH协议进行FTP传输的协议,因而同时要对OpenSSH进行相关设置,需要的朋...

    OSChina10022019-06-19
  • LinuxLinux常用的日志文件和常用命令

    Linux常用的日志文件和常用命令

    成功地管理任何系统的关键之一,是要知道系统中正在发生什么事。 Linux 中提供了异常日志,并且日志的细节是可配置的。Linux 日志都以明文形式存储,所...

    Linux教程网2632020-04-18
  • Linux手把手教您在 Linux 上使用 GPG 加解密文件

    手把手教您在 Linux 上使用 GPG 加解密文件

    在本教程中,我将告诉你如何用 GPG 加密和解密文件。这是一个简单的教程,你可以在你的 Linux 系统上尝试所有的练习。这将帮助你练习 GPG 命令,并在你...

    Linux中国6962021-12-15
  • Linux确保Linux系统安全的前提条件 漏洞防护

    确保Linux系统安全的前提条件 漏洞防护

    Linux 作为开放式的操作系统受到很多程序员的喜爱,很多高级程序员都喜欢编写Linux操作系统的相关软件。这使得Linux操作系统有着丰富的软件支持,还有无...

    Linux之家2642020-04-11
  • Linux详解Linux系统下PXE服务器的部署过程

    详解Linux系统下PXE服务器的部署过程

    这篇文章主要介绍了Linux系统下PXE服务器的部署过程,包括对PXE的API架构作了一个基本的简介,需要的朋友可以参考下...

    运维之道9812019-07-04