荣耀之链论坛

 找回密码
 立即注册
搜索
查看: 1450|回复: 0

CentOS7下firewall的ipset配置使用详解

[复制链接]

1374

主题

2504

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
10697
发表于 2021-2-16 21:12 | 显示全部楼层 |阅读模式
https://blog.csdn.net/github_36904248/article/details/82012579

  1. 一、ipset概述
  2. ipset与iptable
  3. · iptables是在Linux内核中配置防火墙规则的用户空间工具。在内核版本更新到2.4以来,iptable一直作为系统中主要的防火墙解决方案。CentOS7将原来的iptable替换为firewall,而firewall提供了对ipset的支持。
  4. · ipset相当于iptable的扩展,它和iptable 处理方式,iptable通过链表线性存储然后遍历来实现匹配。而ipset通过通过索引的数据结构设计达到快速匹配。这种设计使得当set配置比较庞大的时候,也可以高效地进行匹配。

  5. 适用范围
  6. · 存储多个IP地址或端口号,并在一个swoop中与iptables的集合相匹配;
  7. · 在不影响性能的同时,针对IP地址或端口的变化动态更新iptables规则;
  8. · 性能高,用一个iptables规则来表示复杂的IP地址和端口

  9. 安装
  10. 在CentOS7更新firewalld到最新版本,即可使用ipset,更新安装命令:
  11. 1
  12. yum install firewalld
  13. 1
  14. 二、ipset使用
  15. 利用firewall-cmd对ipset进行操作
  16. ipset的存储路径:/etc/firewalld/ipsets

  17. 命令集合:

  18. 获取指定ipset信息

  19. firewall-cmd --info-ipset=[ipset_name]
  20. 1
  21. 增加ipset

  22. firewall-cmd --permanent --new-ipset=[ipset_name] --type=[type] --option
  23. 1
  24. 其中option可不填,eg:–option=family=inet6指定该ipset使用IPV6地址。

  25. 删除指定ipset

  26. firewall-cmd --permanent --delete-ipset=[ipset_name]
  27. 1
  28. 删除后,在ipset目录下会有一个备份文件,例如:原来的ipset为test_set.xml,则删除后会生成一个test_set.xml.old.

  29. 指定ipset中增加entry

  30. firewall-cmd --permanent --ipset=[ipset_name] --add-entry=[xx.xx.xx.xx]
  31. 1
  32. 指定ipset中删除entry

  33. firewall-cmd --permanent --ipset=[ipset_name] --remove-entry=[xx.xx.xx.xx]
  34. 1
  35. 删除entry
  36. –permanent参数直接决定是否永久生效 需要执行firewall-cmd –reload,使其生效
  37. 不加–permanent是直接生效(runtime environment),不会保存在相应的ipset的XML文件中,重启firewalld服务将会失效

  38. IPSet Options
  39. –get-ipset-types Print the supported ipset types
  40. –new-ipset=<ipset> –type=<ipset type> [–option=<key>[=<value>]]..
  41. Add a new ipset [P only]
  42. –new-ipset-from-file=<filename> [–name=<ipset>]
  43. Add a new ipset from file with optional name [P only]
  44. –delete-ipset=<ipset>
  45. Delete an existing ipset [P only]
  46. –load-ipset-defaults=<ipset>
  47. Load ipset default settings [P only]
  48. –info-ipset=<ipset> Print information about an ipset
  49. –path-ipset=<ipset> Print file path of an ipset [P only]
  50. –get-ipsets Print predefined ipsets
  51. –ipset=<ipset> –set-description=<description>
  52. Set new description to ipset [P only]
  53. –ipset=<ipset> –get-description
  54. Print description for ipset [P only]
  55. –ipset=<ipset> –set-short=<description>
  56. Set new short description to ipset [P only]
  57. –ipset=<ipset> –get-short
  58. Print short description for ipset [P only]
  59. –ipset=<ipset> –add-entry=<entry>
  60. Add a new entry to an ipset [P]
  61. –ipset=<ipset> –remove-entry=<entry>
  62. Remove an entry from an ipset [P]
  63. –ipset=<ipset> –query-entry=<entry>
  64. Return whether ipset has an entry [P]
  65. –ipset=<ipset> –get-entries
  66. List entries of an ipset [P]
  67. –ipset=<ipset> –add-entries-from-file=<entry>
  68. Add a new entries to an ipset [P]
  69. –ipset=<ipset> –remove-entries-from-file=<entry>
  70. Remove entries from an ipset [P]

  71. 三、ipset类型的区别
  72. 1、hash:ip

  73. 2、 hash:ip,mark

  74. 3、hash:ip,port

  75. 4、hash:ip,port,ip

  76. 5、hash:ip,port,net

  77. 6、 hash:mac

  78. 7、hash:net

  79. 8、hash:net,iface

  80. 9、hash:net,net

  81. 10、hash:net,port

  82. 11、hash:net,port,net

  83. 参考文献
  84. 1、https://www.linuxjournal.com/content/advanced-firewall-configurations-ipset
  85. 2、https://firewalld.org/2015/12/ipset-support
  86. 3、https://firewalld.org/documentation/man-pages/firewalld.ipset
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

荣耀之链

GMT+8, 2025-9-10 20:42 , Processed in 0.012003 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表