Red Hat Training
A Red Hat training course is available for RHEL 8
8.2. 编写和执行 nftables 脚本
使用 nftables`
的框架的主要优势在于,脚本的执行是原子的。这意味着,系统会应用整个脚本,或者在出现错误时防止执行。这样可保证防火墙始终处于一致状态。
另外,使用 nftables
脚本环境时,您可以:
- 添加评论
- 定义变量
- 包括其他规则集文件
安装 nftables
软件包时,Red Hat Enterprise Linux 会在 /etc/nftables/
目录中自动创建 *.nft
脚本。这些脚本包含为不同目的创建表和空链的命令。
8.2.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