nftables試してみた 1
恥ずかしながら知らないうちにiptablesの後継が出てきて、先日のConnpassで存在を知りました。
iptablesの問題点等が改善されているということで、後継のnftablesを勉強しました。
サーバの準備
使うサーバ
今回はPC内に転がっていたAlmaLinux9.2 minimal ISOを使用しました。
# uname -r
5.14.0-284.11.1.el9_2.x86_64
# yum updateカーネル4.18~5.4で最新nftablesが利用できるっぽいらしい。
iptablesの確認
インストール時にパスワードsshアクセスのみを許可しています。現状の設定を確認しました。
# iptables --list-rules
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPTfirewalldの確認
firewalldも確認。3つ許可されています。
# firewall-cmd --list-services
cockpit dhcpv6-client sshnftablesの確認
nftablesのインストールをしましたが、すでに入っていました。有効化されていないので、有効化します。
# yum install -y nftables
Last metadata expiration check: 0:53:27 ago on Sat Jan 3 21:25:29 2026.
Package nftables-1:1.0.9-5.el9_7.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
# systemctl status nftables
○ nftables.service - Netfilter Tables
Loaded: loaded (/usr/lib/systemd/system/nftables.service; disabled; preset>
Active: inactive (dead)
Docs: man:nft(8)# systemctl enable nftables
Created symlink /etc/systemd/system/multi-user.target.wants/nftables.service → /usr/lib/systemd/system/nftables.service.
# systemctl start nftables
# systemctl status nftables
● nftables.service - Netfilter Tables
Loaded: loaded (/usr/lib/systemd/system/nftables.service; enabled; preset:>
Active: active (exited) since Sat 2026-01-03 22:20:53 JST; 1s ago
Docs: man:nft(8)
Process: 55165 ExecStart=/sbin/nft -f /etc/sysconfig/nftables.conf (code=ex>
Main PID: 55165 (code=exited, status=0/SUCCESS)
CPU: 12msnftablesの現状の設定を確認しました。firewalldの設定が既に入っています。iptablesの時点で設定がされているのか、nftablesのインストール時に引き継いでいるのかは不明ですが、ここは確認にとどめます。
# nft list tables
table inet firewalld
# nft list ruleset
table inet firewalld {
chain mangle_PREROUTING {
type filter hook prerouting priority mangle + 10; policy accept;
jump mangle_PREROUTING_ZONES
}
......(略)
chain filter_IN_public_allow {
tcp dport 22 accept
ip6 daddr fe80::/64 udp dport 546 accept
tcp dport 9090 accept
}
......(略)
コマンド
基本的なコマンドとパラメータを列挙します。下記以外にもあるので、そのうちまとめたい。
テーブルの作成
nft add table <family名> <table名>family名
ip:ipv4
ip6:ipv6
inet:ipv4,6両方
チェーンの作成
nft add table chain <family名> <table名> <chain名> {type <種類> hook <hookポイント> priority <優先度>}type
filter
nat
hookポイント
input パケット受信時
forward パケット転送時
output パケット送信時
prerouting ルーティング前
postrouting ルーティング後
priority
0 デフォルト 値が小さいほど優先
ルール
nft add rule <table名> <chain名> <条件> <アクション>条件
ip saddr <IP> 送信元アドレス ※CIDR可
ip daddr <IP> 宛先アドレス ※CIDR可
tcp sport <port> 送信元ポート
tcp dport <port> 宛先ポート
アクション
accept
drop
先ほどのfirewalldの例
# nft list tables
table inet firewalldipv4,6両方対応のfirewalldという名前のテーブルを作成しているという意味です。
# nft list ruleset
table inet firewalld {
chain mangle_PREROUTING {
type filter hook prerouting priority mangle + 10; policy accept;
jump mangle_PREROUTING_ZONES
}
......(略)
chain filter_IN_public_allow {
tcp dport 22 accept
ip6 daddr fe80::/64 udp dport 546 accept
tcp dport 9090 accept
}
......(略)チェーン名「filter_IN_public_allow」意味合いとしては外から入ってくる許可ルールかな?
tcp dport 22 accept:宛先22(ssh)を許可。そのほかUDP546番はipv6版DHCPv6等。tcp9090番はRHEL等で使用されるWebコンソールでLinuxサーバを監視できるツール。
参考
# firewall-cmd --list-services
cockpit dhcpv6-client sshこの結果と一致していますね。tcp9090番のサービスは、
# yum -y install cockpit
# systemctl enable --now cockpit.socketでインストールし、ファイアウォールとサービス起動すれば利用できます。せっかくなので、このサービスを対象に実験をします。

触ってみる 基本的なパケットフィルタリング
ipv4検証用テーブルの作成
ipv4用にmy_tableというテーブルを作成しました。
# nft add table ip my_table
# nft list tables
table inet firewalld
table ip my_table検証用テーブルにチェーン作成
# nft add chain ip my_table input '{ type filter hook input priority 0; }'
# nft add chain ip my_table forward '{ type filter hook forward priority 0; }'
# nft add chain ip my_table output '{ type filter hook outputpriority 0; }'コマンドライン上で「{」「}」を入力する場合は「'」でエスケープする。(エラーでハマった)
# nft list ruleset
table ip my_table {
chain input {
type filter hook input priority filter; policy accept;
}
chain forward {
type filter hook forward priority filter; policy accept;
}
chain output {
type filter hook output priority filter; policy accept;
}
}フィルタリングを追加
cockpitの9090番をフィルタリング。firewalldと競合するが果たして。
# nft add rule my_table input tcp dport 9090 drop
# nft list table my_table
table ip my_table {
chain input {
type filter hook input priority filter; policy accept;
tcp dport 9090 drop
}
chain forward {
type filter hook forward priority filter; policy accept;
}
chain output {
type filter hook output priority filter; policy accept;
}
}
無事に?アクセスできませんでした。
ルールの削除
ルール削除にはハンドル番号を指定します。デフォルトでは表示されないので、オプションをつけます。毎回指定はめんどいので、エイリアスで表示させてもいいかな。
# nft --handle --numeric list chain my_table input
table ip my_table {
chain input { # handle 1
type filter hook input priority 0; policy accept;
tcp dport 9090 drop # handle 4
}
}# nft --handle --numeric list chain my_table input
table ip my_table {
chain input { # handle 1
type filter hook input priority 0; policy accept;
}
}
無事に元に戻りました。
いろいろ追加してみる
# nft add rule my_table input ip saddr 192.168.1.0/24 tcp dport 9090 drop
# nft --handle --numeric list chain my_table input
table ip my_table {
chain input { # handle 1
type filter hook input priority 0; policy accept;
ip saddr 192.168.1.0/24 tcp dport 9090 drop # handle 7
}
}こうしたら192.168.1.0/24からのcokpitへのアクセスをフィルタリング。
ロギングを追加
ログ出力も可能です。
nft add rule <family名> <table名> <チェーン名> log prefix "<ログメッセージのプレフィックス>" <アクション>/var/log/syslogや/var/log/messagesに出力される。
## 設定例 ##
# nft add rule ip my_table input log prefix \"INPUT DROP:\" drop※これやるとssh死ぬわ、コンソールにログ出まくるわで大変なことに。
あと「"」もエスケープしないとエラーがでます。
既存のルールに、追加します。replaceコマンドで、ハンドル番号を指定します。
# nft --handle --numeric list chain my_table input
table ip my_table {
chain input { # handle 1
type filter hook input priority 0; policy accept;
ip saddr 192.168.1.0/24 tcp dport 9090 drop # handle 7
}
}
## ルールの更新 ##
# nft replace rule ip my_table input handle 7 ip saddr 192.168.1.0/24 tcp dport 9090 log prefix \"DROP INPUT: \" drop
# nft --handle --numeric list chain my_table input
table ip my_table {
chain input { # handle 1
type filter hook input priority 0; policy accept;
ip saddr 192.168.1.0/24 tcp dport 9090 log prefix "DROP INPUT: " drop # handle 7
}
}# tail -f /var/log/messages
Jan 4 00:56:08 localhost kernel: DROP INPUT: IN=eth0 OUT= MAC=00:15:5d:44:49:05:bc:5f:f4:20:d2:d1:08:00 SRC=192.168.1.8 DST=192.168.1.88 LEN=52 TOS=0x00 PREC=0x00 TTL=128 ID=314 DF PROTO=TCP SPT=49348 DPT=9090 WINDOW=64240 RES=0x00 SYN URGP=0dropアクセスのたびにログが出力されました。
パケットのカウンタ
到達パケットのカウンタも可能です。CPU負荷が上がるのであまりやらない方がいいらしい。
nft add rule ..<ルール等>.. counter# nft replace rule ip my_table input handle 7 ip saddr 192.168.1.0/24 tcp dport 9090 counter
# nft --handle --numeric list chain my_table input
table ip my_table {
chain input { # handle 1
type filter hook input priority 0; policy accept;
ip saddr 192.168.1.0/24 tcp dport 9090 counter packets 0 bytes 0 # handle 7
}
}
## アクセスするとカウントされる ##
# nft --handle --numeric list chain my_table input
table ip my_table {
chain input { # handle 1
type filter hook input priority 0; policy accept;
ip saddr 192.168.1.0/24 tcp dport 9090 counter packets 71 bytes 20828 # handle 7
}
}パケットが0から71、0Bから20828Bに増えました。1パケット293Bってことですかね。
おわりに
とりあえずは超絶基本的なフィルタリングを試しました。NATとかインターフェース毎に制御とか、動的制御とか、ルーティングもやってきます。