第 13 章 编写和执行 nftables 脚本
使用 nftables 框架的主要优点是脚本的执行是原子的。这意味着,系统会应用整个脚本,或者在出现错误时防止执行。这样可保证防火墙始终处于一致状态。
另外,使用 nftables 脚本环境时,您可以:
- 添加评论
- 定义变量
- 包含其他规则集文件
安装 nftables 软件包时,RHEL 会自动在 /etc/nftables/ 目录中创建 *.nft 脚本。这些脚本包含为不同目的创建表和空链的命令。
13.1. 支持的 nftables 脚本格式 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
您可以使用以下格式在 nftables 脚本环境中编写脚本:
与
nft list ruleset命令相同的格式显示规则集:#!/usr/sbin/nft -f # Flush the rule set flush ruleset table inet example_table { chain example_chain { # Chain for incoming packets that drops all packets that # are not explicitly allowed by any rule in this chain type filter hook input priority 0; policy drop; # Accept connections to port 22 (ssh) tcp dport ssh accept } }与
nft命令的语法相同:#!/usr/sbin/nft -f # Flush the rule set flush ruleset # Create a table add table inet example_table # Create a chain for incoming packets that drops all packets # that are not explicitly allowed by any rule in this chain add chain inet example_table example_chain { type filter hook input priority 0 ; policy drop ; } # Add a rule that accepts connections to port 22 (ssh) add rule inet example_table example_chain tcp dport ssh accept