大网数据广告活动
服务器运维问答

Change policy with nftables on command line

· 52CE 我爱测

With iptables I can change the for example INPUT policy with iptables -P INPUT DROP to drop. Is there any option to do the same with nft ? Editing /etc/nftables.conf would work of course but that is not what I want.

问题原文

原帖链接 · Hannes · CC BY-SA 4.0

With iptables I can change the for example INPUT policy with iptables -P INPUT DROP to drop. Is there any option to do the same with nft?

Editing /etc/nftables.conf would work of course but that is not what I want.

已采纳解答

原帖链接 · A.B · CC BY-SA 4.0

Yes you can redefine an already existing base chain's policy without changing its content. There's no separate keyword for this, it's still add:

nft add chain family mytable mychain '{ policy drop; }'

Complete example in a namespace:

test.nft:

flush ruleset

table ip t {
    chain c {
        type filter hook output priority 0; policy accept;
        oif lo accept
        counter
    }
}

setup:

# ip netns add test
# ip netns exec test nft -f test.nft

alteration:

# 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
    }
}

The policy was changed, without altering the rules. Using here nft 0.9.5 and kernel 5.7.x . Depending on version behaviour might differ.

There's a kernel commit from 2015 allowing to do only this:

netfilter: nf_tables: allow to change chain policy without hook if it exists

If there's an existing base chain, we have to allow to change the default policy without indicating the hook information.

However, if the chain doesn't exists, we have to enforce the presence of the hook attribute.

Signed-off-by: Pablo Neira Ayuso [email omitted]

Before this (around kernel 4.1), one had to provide again the base chain definition (which can't be changed by the way):

# ip netns exec test nft add 'chain ip t c { type filter hook output priority 0;  policy drop; }'

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-07-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。