CentOS Stream 10
Sponsored Link

Nftables : サービスを有効化する
2025/01/03
 

Nftables の基本操作です。

nftables は、従来の iptables, ip6tables, arptables, ebtables の機能を統合したツールです。

[1] RHEL CentOS Stream では、nftables は Firewalld のデフォルトのバックエンドとして使用されています。
[root@dlp ~]#
grep ^FirewallBackend /etc/firewalld/firewalld.conf

FirewallBackend=nftables
[2] nftables を使用する場合は、firewalld サービスは無効化します。
また、システムの再起動時にフィルタリングのルールセットを復元できるように、nftables のサービスを有効化しておきます。
[root@dlp ~]#
systemctl disable --now firewalld

Removed '/etc/systemd/system/multi-user.target.wants/firewalld.service'.
Removed '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'.

[root@dlp ~]#
systemctl enable --now nftables

Created symlink '/etc/systemd/system/multi-user.target.wants/nftables.service' → '/usr/lib/systemd/system/nftables.service'.

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

[root@dlp ~]#
systemctl cat nftables.service

# /usr/lib/systemd/system/nftables.service
[Unit]
Description=Netfilter Tables
Documentation=man:nft(8)
Wants=network-pre.target
Before=network-pre.target

[Service]
Type=oneshot
ProtectSystem=full
ProtectHome=true
ExecStart=/sbin/nft -f /etc/sysconfig/nftables.conf
ExecReload=/sbin/nft 'flush ruleset; include "/etc/sysconfig/nftables.conf";'
ExecStop=/sbin/nft flush ruleset
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

# [/etc/sysconfig/nftables.conf] はデフォルトでは設定なし

[root@dlp ~]#
cat /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'.
[3] Firewalld に設定したルールセットを引き継いで nftables サービスに切り替えたい合は、以下のように設定します。
# Firewalld の現在の設定確認 ( firewalld サービス稼働中が前提 )

[root@dlp ~]#
firewall-cmd --list-all

public (default, active)
  target: default
  ingress-priority: 0
  egress-priority: 0
  icmp-block-inversion: no
  interfaces: enp1s0
  sources:
  services: cockpit dhcpv6-client ssh
  ports:
  protocols:
  forward: yes
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

# Firewalld バックエンドの nftables の現在のルールセットを全て表示

[root@dlp ~]#
nft list ruleset

table inet firewalld { # progname firewalld
        flags owner,persist

        chain mangle_PREROUTING {
                type filter hook prerouting priority mangle + 10; policy accept;
                jump mangle_PREROUTING_POLICIES
        }

        chain mangle_PREROUTING_POLICIES {
                iifname "enp1s0" jump mangle_PRE_policy_allow-host-ipv6
                iifname "enp1s0" jump mangle_PRE_public
                iifname "enp1s0" return
                jump mangle_PRE_policy_allow-host-ipv6
                jump mangle_PRE_public
                return
        }
.....
.....

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

[root@dlp ~]#
nft list ruleset > /etc/sysconfig/nftables.conf
# firewalld サービス停止 & nftables サービス起動

[root@dlp ~]#
systemctl disable --now firewalld

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

[root@dlp ~]#
nft list ruleset

table inet firewalld {
        flags persist

        chain mangle_PREROUTING {
                type filter hook prerouting priority mangle + 10; policy accept;
                jump mangle_PREROUTING_POLICIES
        }
.....
.....

# 例えば [firewalld] で許可されていたサービス [services: cockpit dhcpv6-client ssh] は以下のように確認可

[root@dlp ~]#
nft list chain inet firewalld filter_IN_public_allow

table inet firewalld {
        chain filter_IN_public_allow {
                tcp dport 22 accept
                ip6 daddr fe80::/64 udp dport 546 accept
                tcp dport 9090 accept
        }
}
関連コンテンツ