Ubuntu 22.04
Sponsored Link

Nftables : サービスを有効化する2022/10/06

 
Nftables の基本操作です。
nftables は、従来の iptables, ip6tables, arptables, ebtables の機能を統合したツールです。
[1] Ubuntu 22.04 では、nftables は UFW のデフォルトのバックエンドとして使用されています。
root@dlp:~#
update-alternatives --config iptables

There are 2 choices for the alternative iptables (providing /usr/sbin/iptables).

  Selection    Path                       Priority   Status
------------------------------------------------------------
* 0            /usr/sbin/iptables-nft      20        auto mode
  1            /usr/sbin/iptables-legacy   10        manual mode
  2            /usr/sbin/iptables-nft      20        manual mode

Press <enter> to keep the current choice[*], or type selection number:
[2] nftables を直接使用する場合は、フロントエンドの UFW サービスは無効化します。
また、システムの再起動時にフィルタリングのルールセットを復元できるように、nftables のサービスを有効化しておきます。
root@dlp:~#
systemctl disable --now ufw

Synchronizing state of ufw.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable ufw
Removed /etc/systemd/system/multi-user.target.wants/ufw.service.

root@dlp:~#
systemctl enable --now nftables

Created symlink /etc/systemd/system/sysinit.target.wants/nftables.service → /lib/systemd/system/nftables.service.

# [nftables.service] は起動時に [/etc/nftables.conf] を読み込んでルールセットを復元するサービス

root@dlp:~#
systemctl cat nftables.service

# /lib/systemd/system/nftables.service
[Unit]
Description=nftables
Documentation=man:nft(8) http://wiki.nftables.org
Wants=network-pre.target
Before=network-pre.target shutdown.target
Conflicts=shutdown.target
DefaultDependencies=no

[Service]
Type=oneshot
RemainAfterExit=yes
StandardInput=null
ProtectSystem=full
ProtectHome=true
ExecStart=/usr/sbin/nft -f /etc/nftables.conf
ExecReload=/usr/sbin/nft -f /etc/nftables.conf
ExecStop=/usr/sbin/nft flush ruleset

[Install]
WantedBy=sysinit.target

# [/etc/nftables.conf] はデフォルトではフィルタリングの設定なし

root@dlp:~#
cat /etc/nftables.conf

#!/usr/sbin/nft -f

flush ruleset

table inet filter {
        chain input {
                type filter hook input priority 0;
        }
        chain forward {
                type filter hook forward priority 0;
        }
        chain output {
                type filter hook output priority 0;
        }
}
[3] UFW に設定したルールセットを引き継いで nftables サービスに切り替えたい合は、以下のように設定します。
# UFW の現在の設定確認 ( ufw サービス稼働中が前提 )

root@dlp:~#
ufw status verbose

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere
80/tcp                     ALLOW IN    Anywhere
443                        ALLOW IN    Anywhere
22/tcp (v6)                ALLOW IN    Anywhere (v6)
80/tcp (v6)                ALLOW IN    Anywhere (v6)
443 (v6)                   ALLOW IN    Anywhere (v6)

# 現在のルールセットを [/etc/nftables.conf] に書き出す

root@dlp:~#
iptables-save > ufw-rules.dump

root@dlp:~#
iptables-restore-translate -f ufw-rules.dump > ruleset.nft

root@dlp:~#
nft flush ruleset

root@dlp:~#
nft -f ruleset.nft

root@dlp:~#
nft list ruleset > /etc/nftables.conf
# ufw サービス停止 & nftables サービス起動

root@dlp:~#
systemctl disable --now ufw

root@dlp:~#
systemctl enable --now nftables
# ルールセット表示

root@dlp:~#
nft list ruleset

table ip filter {
        chain INPUT {
                type filter hook input priority filter; policy drop;
                counter packets 3 bytes 228 jump ufw-before-logging-input
                counter packets 3 bytes 228 jump ufw-before-input
                counter packets 0 bytes 0 jump ufw-after-input
                counter packets 0 bytes 0 jump ufw-after-logging-input
                counter packets 0 bytes 0 jump ufw-reject-input
                counter packets 0 bytes 0 jump ufw-track-input
        }

        chain FORWARD {
                type filter hook forward priority filter; policy drop;
                counter packets 0 bytes 0 jump ufw-before-logging-forward
                counter packets 0 bytes 0 jump ufw-before-forward
                counter packets 0 bytes 0 jump ufw-after-forward
.....
.....

# 例えば [UFW] で許可されていたサービスは以下のように確認可

root@dlp:~#
nft list chain ip filter ufw-user-input

table ip filter {
        chain ufw-user-input {
                tcp dport 22 counter packets 0 bytes 0 accept
                tcp dport 80 counter packets 0 bytes 0 accept
                tcp dport 443 counter packets 0 bytes 0 accept
                udp dport 443 counter packets 0 bytes 0 accept
        }
}
関連コンテンツ