nftables アクセス遮断
#linux #nftables
前提
nftablesがインストールされ、サービスが有効になっていること
Debian / Ubuntu など
$ sudo apt update
$ sudo apt install nftables
ufwが無効であることを確認
$ sudo ufw status
nftablesサービスを有効化
$ sudo systemctl enable --now nftables
Red Hat / CentOS / AlmaLinux など
firewalldがデフォルトで有効なため、直接nftablesを操作するにはfirewalldを停止・無効化
firewalldを停止・無効化
$ sudo systemctl stop firewalld
$ sudo systemctl disable firewalld
$ sudo systemctl mask firewalld
nftablesをインストール(通常はインストール済み)
$ sudo yum install nftables
nftablesサービスを有効化
$ sudo systemctl enable --now nftables
注意
これらのコマンドは実行後に即時反映されるが、再起動で消えるため注意
反映のために設定の永続化欄を参照
SSHポート(22)を遮断すると接続できなくなる(すぐに切断される)ので注意
rootユーザ、またはiptablesに関してroot権限を有する設定のあるsudoで実行する必要がある
以下コマンド例はrootユーザ想定
nftablesの利用前提
/icons/hr.icon
nftablesでは、テーブル(table)とチェイン(chain)を定義する必要がある
※iptablesのfilterテーブルやINPUTチェインに相当するものを自分で作成する
/etc/nftables.confに基本設定を記述
code:nft
#!/usr/sbin/nft -f
flush ruleset
# テーブルの定義('inet'はIPv4とIPv6を両方扱うファミリー)
table inet filter {
# チェインの定義
chain input {
type filter hook input priority 0;
policy accept;
}
chain forward {
type filter hook forward priority 0;
policy accept;
}
chain output {
type filter hook output priority 0;
policy accept;
}
}
table inet filter: inetファミリー(IPv4/v6共通)のfilterという名前のテーブルを作成
chain input: filterテーブル内にinputチェインを作成。policy acceptがデフォルト許可設定
コマンド例
/icons/hr.icon
以下のコマンド例は、上記の設定が適用されていることを前提
nftablesのコマンドはnft add rule [ファミリー] [テーブル名] [チェイン名] [ルール]という形式が基本
inetファミリーを使えば、1つのテーブル・チェインでIPv4とIPv6の両方を扱える
特定IPのインバウンドを遮断(IPv4)
code:bash
$ nft add rule inet filter input ip saddr 203.0.113.10 drop
特定IPのインバウンドを遮断(IPv6)
code:bash
$ nft add rule inet filter input ip6 saddr 2001:db8::/32 drop
特定IPのアウトバウンドを遮断(IPv4)
code:bash
$ nft add rule inet filter output ip daddr 203.0.113.10 drop
特定IPのアウトバウンドを遮断(IPv6)
code:bash
$ nft add rule inet filter output ip6 daddr 2001:db8::/32 drop
特定IPの転送を遮断(IPv4)
code:bash
$ nft add rule inet filter forward ip saddr 203.0.113.10 drop
特定IPの転送を遮断(IPv6)
code:bash
$ nft add rule inet filter forward ip6 saddr 2001:db8::/32 drop
ポート単位で遮断
code:bash
$ nft add rule inet filter input ip saddr 203.0.113.0/24 tcp dport 22 drop
設定の永続化
/icons/hr.icon
nftablesでは
現在有効なルールセットを/etc/nftables.confファイルに保存
サービスの起動時に読み込ませる
設定ファイルの場所
/icons/hr.icon
Linuxディストリビューションによって異なる
Red Hat / CentOS / AlmaLinux など
code:/etc/sysconfig/nftables.conf
# Uncomment the include statement here to load the default config sample
# in /etc/nftables for nftables service.
#include "/etc/nftables/main.nft"
# To customize, either edit the samples in /etc/nftables, append further
# commands to the end of this file or overwrite it after first service
# start by calling: 'nft list ruleset >/etc/sysconfig/nftables.conf'.
最初にバックアップの取得を推奨
code:bash
$ nft list ruleset > </path/to/dir>.$(date +%Y%m%d).conf
設定の保存
/icons/hr.icon
現在有効なルールセットをnft list rulesetで表示し、設定ファイルに上書き保存
$ nft list ruleset > /etc/nftables.conf
systemctl restart nftablesなどでサービスを再起動すれば、次回以降もこの設定が読み込まれる
復元
/icons/hr.icon
バックアップしておいたファイルから設定を復元するには、-fオプションを使います。
code:bash
$ nft -f </path/to/dir>.20250909.conf
このコマンドは、ファイルの内容で現在のルールセットを完全に置き換えます。
基本コマンド解説
nftablesの構文はより自然言語に近い
add rule: ルールを追加
table inet filter: inetファミリーのfilterテーブルを指す
chain input: inputチェインを指す
ip saddr: IPv4の送信元アドレス (source address)
ip daddr: IPv4の宛先アドレス (destination address)
ip6 saddr: IPv6の送信元アドレス
ip6 daddr: IPv6の宛先アドレス
tcp dport: TCPの宛先ポート (destination port)
drop, reject, accept: パケットが一致した場合の動作
意味はiptablesと同じ
ルールセット全体を表示するには nft list ruleset を実行
関連
iptables
iptables アクセス遮断