Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Ubuntu UFW 防火牆簡單設定完整教學(啟用、預設拒絕、開放埠口、IP 白名單)

編輯:關於 Linux

UFW (Uncomplicated Firewall) 是 Ubuntu 預設的 iptables 前端工具,語法簡單易上手。本文整理從安裝、啟用、預設策略、開放/關閉埠口、IP 白名單、服務名稱控制到推薦基礎配置的完整操作指南。

1. 安裝與啟用

sudo apt-get install ufw
sudo ufw enable
sudo ufw default deny
# 作用:開啟防火牆、隨系統啟動、預設拒絕所有外部連入(外出允許)

2. 關閉防火牆

sudo ufw disable

3. 查看狀態

sudo ufw status
# 詳細模式:
sudo ufw status verbose
# 編號模式(便於刪除):
sudo ufw status numbered

4. 開放/關閉埠口與服務

操作指令範例
開放 TCP 80sudo ufw allow 80/tcp
關閉 TCP 80sudo ufw delete allow 80/tcp
允許特定 IP 存取所有埠sudo ufw allow from 192.168.1.1
禁止 SMTP 服務sudo ufw deny smtp
刪除規則(按編號)sudo ufw delete 3
拒絕特定網段存取 SSHsudo ufw deny proto tcp from 10.0.0.0/8 to any port 22

5. 允許 RFC1918 私有網段(內網/無線網)全通

sudo ufw allow from 10.0.0.0/8
sudo ufw allow from 172.16.0.0/12
sudo ufw allow from 192.168.0.0/16

6. 推薦基礎配置(適用大多數桌面/伺服器)

sudo apt-get install ufw
sudo ufw enable
sudo ufw default deny
# 僅開放必要服務,例如:
sudo ufw allow 22/tcp   # SSH
sudo ufw allow 80/tcp   # HTTP
sudo ufw allow 443/tcp  # HTTPS
# 內網全通(視需求)
sudo ufw allow from 192.168.0.0/16

UFW 狀態顯示截圖

圖形介面替代方案

sudo apt-get install gufw
# 在選單中搜尋 "Firewall Configuration" 或 "Gufw" 啟動

常見問題

  • 規則不生效:確認 ufw enable 已執行,且 iptables -L 可見規則。
  • Docker 衝突:Docker 直接操作 iptables 可能繞過 UFW,需配置 /etc/default/ufw 設定 DEFAULT_FORWARD_POLICY="ACCEPT" 或使用 ufw-docker 腳本。
  • IPv6:編輯 /etc/default/ufw 設定 IPV6=yes 後重啟 UFW。
Copyright © Windows教程網 All Rights Reserved