13.2. 运行 nftables 脚本
您可以通过将脚本传递给 nft
实用程序或直接执行脚本来运行 nftables
脚本。
流程
要通过将其传给
nft
工具来运行nftables
脚本,请输入:nft -f /etc/nftables/<example_firewall_script>.nft
# nft -f /etc/nftables/<example_firewall_script>.nft
Copy to Clipboard Copied! 要直接运行
nftables
脚本:在进行这个时间时:
确保脚本以以下 shebang 序列开头:
#!/usr/sbin/nft -f
#!/usr/sbin/nft -f
Copy to Clipboard Copied! 重要如果省略了
-f
参数,nft
工具不会读取脚本,并显示Error: syntax error, unexpected newline, expecting string
。可选:将脚本的所有者设为
root
:chown root /etc/nftables/<example_firewall_script>.nft
# chown root /etc/nftables/<example_firewall_script>.nft
Copy to Clipboard Copied! 使脚本可以被其所有者执行:
chmod u+x /etc/nftables/<example_firewall_script>.nft
# chmod u+x /etc/nftables/<example_firewall_script>.nft
Copy to Clipboard Copied!
运行脚本:
/etc/nftables/<example_firewall_script>.nft
# /etc/nftables/<example_firewall_script>.nft
Copy to Clipboard Copied! 如果没有输出结果,系统将成功执行该脚本。
重要即使
nft
成功地执行了脚本,在脚本中错误放置的规则、缺失的参数或其他问题都可能会导致防火墙的行为不符合预期。
详情请查看您系统中的 chown (1)
和 chmod (1)
手册页。