RHEL/CentOS 現在要在系統啟動時在firewalld 中新增 nftable 規則嗎?

RHEL/CentOS 現在要在系統啟動時在firewalld 中新增 nftable 規則嗎?

我在 RHEL 8 上使用firewalld,還需要加入一些 nftable 法則。

(nftable 規則是基於以下問題的答案CentOS 8作為帶有nft和firewalld的NAT路由器-如何讓它通過TFTP?

在正在運行的防火牆中,這可以與 nft -f 命令配合使用。

但是,重新啟動後該資訊就會遺失。

紅帽文檔(在付費牆後面)建議使用 nftables.service 服務在重新啟動時載入規則,但這不能與firewalld一起使用。這兩個服務被列為衝突,即使不衝突,firewalld 也可能會刷新 nftable 規則。

還有另一種方法可以讓 nftable 規則在重新啟動時載入嗎?

答案1

防火牆實用程序,當使用nftables後端,不會刷新不屬於它的表

只刷新firewalld的規則

由於 nftables 允許命名空間(透過表)firewalld 不再完全刷新防火牆規則。它只會刷新規則 防火牆桌子。這可以避免在防火牆重新啟動或重新載入時自訂使用者規則或其他工具安裝的規則被意外清除的情況。

管理其他表格時也需要這樣做。

實際上在之前的答案中,它已經完成了:nftables規則冪等刪除僅有的他們自己的表:handletftp

nftables.service可悲的是,情況並非如此停止行動:

ExecStop=/sbin/nft flush ruleset

我們必須確保 systemd 服務的停止部分在仍在執行工作的同時不會直接刷新所有規則。這項工作將委託給專門的nftables的規則停止行動。

所以這裡有一個實用的方法:複製(例如systemctl cat nftables.services:)並更改為要放入的nftables.service實例化版本:[email protected]/etc/systemd/system/[email protected]

[Unit]
Description=Idempotent nftables rules for %I
Wants=network-pre.target
Before=network-pre.target

[Service]
Type=oneshot
ProtectSystem=full
ProtectHome=true
ExecStart=/sbin/nft -f /etc/nftables/idempotent/%I.nft
# As the rules are idempotent, ExecReload is same as ExecStart
ExecReload=/sbin/nft -f /etc/nftables/idempotent/%I.nft
# The stop rules should only have the first boilerplate parts
ExecStop=/sbin/nft -f /etc/nftables/idempotent/stop-%I.nft
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

建立上面使用的專用配置目錄:

mkdir -p /etc/nftables/idempotent

對於定義的每個表,放置規則總是這樣開始,因此載入規則獨立於其他表,並且冪等的ip foo(帶有表格和 的範例bridge bar):

table ip foo
delete table ip foo

table bridge bar
delete table bridge bar

table ip foo {
    ...
}

table bridge bar {
    ....
}

或每個文件只使用一張表。該flush ruleset聲明是被禁止的,因為它是全球性的。

創建、刪除和重新創建表的原因是為了獲得冪等的結果:雖然刪除不存在的表是一個錯誤,並且會原子地使整個加載失敗,但聲明一個現有表而不定義它(通過添加一個空表)永遠不會失敗並且什麼都不做除了將其創建為空之外如果它以前不存在的話。在這兩種情況下(啟動時不存在,重新載入時存在)刪除現在可以工作了,留下真正定義表格的地方。所有這些都發生在同一個事務中,並且仍然是原子的:不會評估丟失的資料包ip 富表在此之前是否存在。

準備一個停止上面的版本只會刪除(這裡空聲明不是嚴格需要的,如果只有一張表,則可以刪除,但如果有多個表,則應保留:失敗是針對整個事務的):

table ip foo
delete table ip foo

table bridge bar
delete table bridge bar

並將所有東西放在它們的位置:

/etc/nftables/idempotent/foobar.nft
/etc/nftables/idempotent/stop-foobar.nft

現在可以透過以下方式啟動:

systemctl enable --now local-idempotent-nft@foobar

之前OP問題的範例:

/etc/nftables/idempotent/handletftp.nft

table ip handletftp
delete table ip handletftp

table ip handletftp {
    ct helper helper-tftp {
        type "tftp" protocol udp
    }

    chain sethelper {
        type filter hook forward priority 0; policy accept;
        ip saddr 192.168.1.0/24 ip daddr 10.0.10.10 udp dport 69 ct helper set "helper-tftp"
    }
}

/etc/nftables/idempotent/stop-handletftp.nft

table ip handletftp
delete table ip handletftp

啟用並啟動它:

systemctl enable --now local-idempotent-nft@handletftp

停止它:

systemctl stop local-idempotent-nft@handletftp

這將離開防火牆的規則到位。同樣,停止或重新啟動防火牆將保留這些規則。

可能還需要改進:

  • nftables有一個包括可以以某種方式使用該語句來避免重複樣板。
  • nf_nat_tftp關於 TFTP 的具體範例依賴於不會自動完成的載入(nf_conntrack_tftp與規則中的引用自動載入相反或與防火牆這將nf_nat_tftp顯式加載)。如此相關但不嚴格nftables應該記住配置(這一設定可以簡單地放入/etc/modules-load.d/)。

相關內容