搜索

5.13. 使用 iptables设置和控制 IP 集

download PDF
firewalldiptables (和 ip6tables)服务之间的基本区别是:
  • iptables 服务将 配置存储在 /etc/sysconfig/iptables/etc/sysconfig/ip6tables 中,而 firewalld 将其存储在 /usr/lib/firewalld/ 和 / etc/firewalld/ 的不同 XML 文件中。请注意,/etc/sysconfig/iptables 文件不存在,因为默认情况下在 Red Hat Enterprise Linux 中安装 firewalld
  • 使用 iptables 服务 时,每个更改都意味着清除所有旧规则,并从 /etc/sysconfig/iptables 读取所有新规则,而 firewalld 不会重新创建所有规则。仅应用不同之处。因此,firewalld 可以在运行时更改设置,而不会丢失现有连接。
两者都使用 iptables 工具 与内核数据包过滤。
要使用 iptablesip6tables 服务而不是 firewalld,首先以 root 用户身份运行以下命令来禁用 firewalld
~]# systemctl disable firewalld
~]# systemctl stop firewalld
然后以 root 用户身份输入以下命令安装 iptables-services 软件包:
~]# yum install iptables-services
iptables-services 软件包包含 iptables 服务和 ip6tables 服务。
然后,要启动 iptablesip6tables 服务,请以 root 用户身份输入以下命令:
~]# systemctl start iptables
~]# systemctl start ip6tables
要启用服务在每次系统启动时启动,请输入以下命令:
~]# systemctl enable iptables
~]# systemctl enable ip6tables
ipset 工具用于管理 Linux 内核中的 IP 集。IP 集是用于存储 IP 地址、端口号、IP 和 MAC 地址对的框架,或者 IP 地址和端口号对。集合的索引方式可以针对集合进行快速匹配,即使集合非常大。IP 集启用更简单且更易于管理的配置,以及使用 iptables 时提供性能优势。iptables 匹配和目标,创建保护内核中给定集合的引用。当存在指向它的单一引用时,无法销毁集合。
使用 ipset 可启用 iptables 命令(如下面的命令)被一个集合替代:
~]# iptables -A INPUT -s 10.0.0.0/8 -j DROP
~]# iptables -A INPUT -s 172.16.0.0/12 -j DROP
~]# iptables -A INPUT -s 192.168.0.0/16 -j DROP
该集合创建如下:
~]# ipset create my-block-set hash:net
~]# ipset add my-block-set 10.0.0.0/8
~]# ipset add my-block-set 172.16.0.0/12
~]# ipset add my-block-set 192.168.0.0/16
然后,在 iptables 命令中引用该集合,如下所示:
~]# iptables -A INPUT -m set --set my-block-set src -j DROP
如果设置被多次使用,则进行保存配置时间。如果集合包含多个在处理时间保存的条目。
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.