在命令行上使用 nftables 更改策略

Han*_*nes 9 firewall nftables

使用 iptables,我可以将 INPUT 策略更改为iptables -P INPUT DROPdrop。有什么选择可以做同样的事情nft吗?

编辑/etc/nftables.conf当然可以,但这不是我想要的。

A.B*_*A.B 10

是的,您可以重新定义现有基础链的策略,而不更改其内容。没有单独的关键字,它仍然是add

nft add chain family mytable mychain '{ policy drop; }'
Run Code Online (Sandbox Code Playgroud)

命名空间中的完整示例:

test.nft:

flush ruleset

table ip t {
    chain c {
        type filter hook output priority 0; policy accept;
        oif lo accept
        counter
    }
}
Run Code Online (Sandbox Code Playgroud)

设置:

# ip netns add test
# ip netns exec test nft -f test.nft
Run Code Online (Sandbox Code Playgroud)

改造:

# ip netns exec test nft add 'chain ip t c { policy drop; }'
# ip netns exec test nft list ruleset
table ip t {
    chain c {
        type filter hook output priority filter; policy drop;
        oif "lo" accept
        counter packets 0 bytes 0
    }
}
Run Code Online (Sandbox Code Playgroud)

政策发生了变化,但规则没有改变。此处使用 nft 0.9.5 和内核 5.7.x 。根据版本的不同,行为可能会有所不同。

2015 年的内核提交仅允许执行此操作:

netfilter:nf_tables:允许在没有钩子的情况下更改链策略(如果存在)

如果存在现有的基础链,我们必须允许更改默认策略而不指示挂钩信息。

但是,如果链不存在,我们必须强制钩子属性的存在。

签署人:Pablo Neira Ayuso pablo@netfilter.org

在此之前(大约内核 4.1),必须再次提供基本链定义(顺便说一下,它不能更改):

# ip netns exec test nft add 'chain ip t c { type filter hook output priority 0;  policy drop; }'
Run Code Online (Sandbox Code Playgroud)