以下是 `firewall-cmd --new-ipset` 命令所有选项参数的详细解析: ## 命令完整语法 ```bash firewall-cmd --permanent --new-ipset= --type= [选项参数] ``` ## 主要选项参数详解 ### 1. `--type=` (必需参数) 指定 IPset 的数据类型,决定集合中存储的数据格式。 **基本类型:** ```bash # 存储单个 IP 地址 --type=hash:ip # 存储网络段 (CIDR) --type=hash:net # 存储 IP 地址和端口组合 --type=hash:ip,port # 存储网络段和端口组合 --type=hash:net,port # 存储 IP 地址、端口、协议组合 --type=hash:ip,port,ip --type=hash:ip,port,net # 存储 MAC 地址 --type=hash:mac # 存储网络接口名 --type=hash:net,iface ``` ### 2. `--option=` (关键选项参数) 设置 IPset 的各种特性,多个选项用逗号分隔。 #### 网络协议族选项 ```bash # IPv4 地址 (默认) --option=family=inet # IPv6 地址 --option=family=inet6 ``` #### 存储大小选项 ```bash # 初始哈希表大小 (默认 1024) --option=hashsize=2048 # 最大元素数量 --option=maxelem=65536 ``` #### 超时选项 ```bash # 所有条目的默认超时时间(秒) --option=timeout=3600 # 允许单个条目覆盖默认超时 --option=counters ``` #### 网络掩码选项(仅对 hash:net 类型有效) ```bash # 固定网络掩码 --option=netmask=24 # 允许从 0 到 32 的所有掩码(IPv4) --option=netmask=0 ``` ### 3. 完整选项示例 ```bash # 创建 IPv4 黑名单,最大 10000 个条目,默认超时 1 小时 firewall-cmd --permanent \ --new-ipset=blacklist_v4 \ --type=hash:ip \ --option="family=inet,hashsize=1024,maxelem=10000,timeout=3600" # 创建 IPv6 网络段黑名单 firewall-cmd --permanent \ --new-ipset=blacklist_v6_nets \ --type=hash:net \ --option="family=inet6,maxelem=5000" # 创建带端口的 IP 集合(用于服务限制) firewall-cmd --permanent \ --new-ipset=web_servers \ --type=hash:ip,port \ --option="family=inet,maxelem=2000" # 创建永久性 IP 集合(无超时) firewall-cmd --permanent \ --new-ipset=permanent_blocks \ --type=hash:ip \ --option="family=inet,maxelem=50000" ``` ## 高级类型详细说明 ### hash:ip,port 类型 存储 IP 地址和端口号的组合: ```bash # 条目格式:192.168.1.1,tcp:80 # 创建示例: firewall-cmd --new-ipset=service_ips --type=hash:ip,port ``` ### hash:net,port 类型 存储网络段和端口号的组合: ```bash # 条目格式:192.168.0.0/24,udp:53 # 创建示例: firewall-cmd --new-ipset=network_services --type=hash:net,port ``` ### 带协议标识的类型 ```bash # 三元素组合:源IP, 目标IP, 协议/端口 --type=hash:ip,port,ip --type=hash:net,port,net ``` ## 查看和验证选项 创建后可以使用以下命令验证: ```bash # 查看 IPset 配置详情 firewall-cmd --info-ipset= # 查看所有支持的 IPset 类型 firewall-cmd --get-ipset-types # 查看当前所有 IPset firewall-cmd --get-ipsets ``` ## 实际应用案例 ### 案例1:创建动态黑名单 ```bash firewall-cmd --permanent \ --new-ipset=dynamic_blacklist \ --type=hash:ip \ --option="family=inet,maxelem=100000,timeout=86400,hashsize=4096" ``` ### 案例2:创建服务白名单 ```bash firewall-cmd --permanent \ --new-ipset=web_whitelist \ --type=hash:ip,port \ --option="family=inet,maxelem=1000" ``` ### 案例3:创建国家IP段集合 ```bash firewall-cmd --permanent \ --new-ipset=country_blocks \ --type=hash:net \ --option="family=inet,maxelem=50000,netmask=0" ``` ## 重要注意事项 1. **永久性配置**:生产环境务必使用 `--permanent` 参数 2. **重载生效**:修改后执行 `firewall-cmd --reload` 3. **性能考虑**:合理设置 `hashsize` 和 `maxelem` 避免内存浪费 4. **超时设置**:临时性条目使用 `timeout`,永久性条目不要设置超时 5. **类型选择**:根据实际需求选择最合适的类型以减少内存占用 这些选项参数提供了灵活的 IPset 配置能力,可以根据具体的安全需求进行精细化设置。