Jump to content
View in the app

A better way to browse. Learn more.

PHP论坛人

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Debian 13 防火牆建置:使用iptables (nf_tables backend) 與 systemd 開機自動啟動

Featured Replies

Debian 13 防火牆建置:使用iptables (nf_tables backend) 與 systemd 開機自動啟動

------
前言
------

在Debian 13中,iptables預設使用 nf_tables backend (iptables-nft)


這代表:

指令仍是iptables

核心架構實際為nftables

效能與擴充性較舊版iptables-legacy更佳


此方式適合:

想保留 iptables 指令習慣者

需要穩定腳本相容性者



安全提示:錯誤的規則可能導致SSH斷線。建議操作前開啟主機商的VNC/Serial Console,或先將自己的IP加入允許清單



---------------------------------
安裝iptables防火牆並確認後端
---------------------------------


更新套件清單
apt update


安裝iptables
apt install -y iptables


確認後端類型,應顯示 iptables v1.8.11 (nf_tables) ,代表系統正在使用nftables核心架構
iptables --version



確認目前規則狀態 (初始應為空或僅有基本規則)
iptables -L -v




------------------
避免防火牆衝突
------------------

注意:請勿移除 nftables 套件,因為它是Linux kernel 的 Netfilter 框架 + nftables userspace 工具

若有安裝ufw,請將其停用以避免規則衝突

systemctl disable ufw --now





--------------------
配置防火牆腳本
--------------------

將規則寫入腳本,方便管理與自動化


建立腳本目錄 (若目錄不存在)
mkdir -p /usr/local/bin


建立防火牆腳本
vi /usr/local/bin/firewall.sh


請將提供的防火牆腳本 firewall.sh 內容複製貼上

並將 你的管理員IP 替換掉

# 1. 禁止連線的 IP 或網段(空白分隔)
BADIPS="198.108.0.0/16 141.212.0.0/16"


# 2. 不可能出現的私有 IP(RFC 1918)
# 注意:若您的伺服器本身 IP 屬於以下網段,請務必刪除該行!
#   10.0.0.0/8    → 大型企業內網
#   172.16.0.0/12 → 中型企業內網
#   192.168.0.0/16→ 小型辦公室/家庭網路
IMPOSSIBLE_IPS="192.168.0.0/16"


# 3. 允許對內連線的 TCP 通訊埠
# 重要:請務必將「你的管理員IP」換成您當前連線的IP,否則套用 DROP 後將無法連線!
IN_TCP_PORTALLOWED="22,你的管理員IP 888,你的管理員IP 80 443"



ESC儲存並離開
:wq




設定腳本權限 (權限說明:700 表示僅 root 可讀寫執行,提高安全性)
chmod 700 /usr/local/bin/firewall.sh



-----------------
測試防火牆腳本
-----------------

執行腳本 (進入測試模式,7秒後自動清除規則)
/usr/local/bin/firewall.sh


確認規則是否正常載入
iptables -L


若確認規則正確,使用 start 參數永久套用
/usr/local/bin/firewall.sh start




-----------------------------
整合 systemd 開機自動啟動
-----------------------------

為了確保重開機後防火牆自動生效,我們建立一個 systemd 服務檔


建立服務檔
vi /etc/systemd/system/firewall.service


複製貼上以下內容

[Unit]
Description=Custom iptables Firewall Service (nftables backend)
DefaultDependencies=no
Before=network-pre.target
Wants=network-pre.target


[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/firewall.sh start
ExecStop=/usr/local/bin/firewall.sh stop
StandardOutput=journal

[Install]
WantedBy=multi-user.target


ESC儲存並離開
:wq


------------------
啟用並啟動服務
------------------

重新載入 systemd 設定
systemctl daemon-reload


設定開機自動啟動
systemctl enable firewall.service


立即啟動服務
systemctl start firewall.service


----------------
確認服務狀態
----------------

查看服務狀態,應顯示 active (exited)
systemctl status firewall.service


查看啟動日誌
journalctl -u firewall.service



-----------
驗證設定
-----------

重開機
reboot




重開機後確認防火牆規則是否自動載入


確認防火牆規則存在
iptables -L


確認服務狀態,若規則正確顯示,且服務狀態為 active (exited),表示設定成功
systemctl status firewall.service





-------
補充
-------

先允許管理員IP的SSH,再套用DROP預設策略,避免鎖死自己

使用 journalctl 追蹤 systemd 啟動過程,方便除錯

定期備份防火牆腳本,並在更新前先於測試環境驗證


確認哪些連線埠正在對外監聽
ss -tnlp


查看已建立的連線
ss -tnp

Create an account or sign in to comment

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.