OpenWRT 使用 Dropbear 區分內部外部網路判定是否允許密碼登入

🧩 解法思路:使用防火牆 + Dropbear 組合控制登入方式

方法一:限制外部 IP 只能連接特定 Dropbear 埠(只允許金鑰登入)

  1. 啟用兩個 Dropbear 實例

    • 一個埠(例如 22)允許密碼登入,僅開放給內部網路。
    • 一個埠(例如 2222)只允許金鑰登入,開放給外部網路。
  2. 設定 Dropbear 實例

    # 允許密碼登入(內部用)(可能會跟預設的有重複)
    uci add dropbear dropbear
    uci set dropbear.@dropbear[-1].Port='22'
    uci set dropbear.@dropbear[-1].PasswordAuth='on'
    uci set dropbear.@dropbear[-1].RootPasswordAuth='on'
    uci set dropbear.@dropbear[0].Interface='lan'
    
    # 禁止密碼登入(外部用)
    uci add dropbear dropbear
    uci set dropbear.@dropbear[-1].Port='2222'
    uci set dropbear.@dropbear[-1].PasswordAuth='on'
    uci set dropbear.@dropbear[-1].RootPasswordAuth='off'
    uci set dropbear.@dropbear[1].Interface='wan'
    
    uci commit dropbear
    /etc/init.d/dropbear restart
    
    

🔐 Dropbear 支援的設定選項

Dropbear 的 UCI 設定中有兩個相關選項:

   `PasswordAuth`:是否允許密碼登入(針對所有使用者)
   `RootPasswordAuth`:是否允許 root 使用密碼登入
#正確長這樣 有重複的要刪掉
root@OpenWrt:~# cat /etc/config/dropbear

config dropbear 'main'
        option enable '1'
        option PasswordAuth 'on'
        option RootPasswordAuth 'on'
        option Port '22'
        option Interface 'lan'

config dropbear
        option Port '2222'
        option PasswordAuth 'on'
        option RootPasswordAuth 'off'
        option Interface 'wan'

root@OpenWrt:~#
  1. 設定防火牆規則

    • 開放 22 埠給內部網段(例如 192.168.1.0/24)。
    • 開放 2222 埠給外部網路。
    # 內部網路允許連接 22 埠
    uci add firewall rule
    uci set firewall.@rule[-1].name='Allow-SSH-Internal'
    uci set firewall.@rule[-1].src='lan'
    uci set firewall.@rule[-1].dest_port='22'
    uci set firewall.@rule[-1].proto='tcp'
    uci set firewall.@rule[-1].target='ACCEPT'
    
    # 外部網路允許連接 2222 埠
    uci add firewall rule
    uci set firewall.@rule[-1].name='Allow-SSH-External'
    uci set firewall.@rule[-1].src='wan'
    uci set firewall.@rule[-1].dest_port='2222'
    uci set firewall.@rule[-1].proto='tcp'
    uci set firewall.@rule[-1].target='ACCEPT'
    
    uci commit firewall
    /etc/init.d/firewall restart
    
    

✅ 目前設定效果

網路來源 SSH 埠 使用者 密碼登入 金鑰登入
內部網路 22 root / 一般使用者 ✅ 允許 ✅ 允許
外部網路 2222 一般使用者 ✅ 允許 ✅ 允許
外部網路 2222 root ❌ 禁止 ✅ 允許