在 NordVPN 設定 OpenWrt

NordVPN 支援 OpenWrt 嗎?

有消息指出,具備 OpenWRT 韌體的路由器支援 NordVPN 等 VPN。 但請注意,NordVPN 員工尚未測試以下設定,而是由我們出色的客戶分享並測試出來的流程。 NordVPN 在此特別感謝 OpenWRT 社群的活躍成員 Unwind,感謝他們持續協助並提供我們最新的 OpenWRT 說明。

GUI 操作說明

本指南將介紹透過 LuCI 網頁介面,如何使用 OpenWrt 韌體的路由器設定 NordVPN 連線。

  1. 在網際網路瀏覽器輸入 OpenWrt 路由器本機 IP 位址並登入,即可存取其 LuCI 介面。 預設 IP 位址為 192.168.1.1,而使用者名稱為 root



    注意:系統在預設情況下並無設定密碼,因此此欄位可以留白。 不過登入時,您會收到要求設定密碼的系統訊息。


    如欲設定密碼,請點選「系統 (System)」 > 「管理 (Administration)」,在該處即可設定密碼。
  2. 登入後,選擇「系統 (System)」分頁,並選擇「軟體 (Software)」



  3. 點選「更新清單 (Update lists)」按鈕,等待流程完成,並點選「忽略 (Dismiss)」
  4. 「篩選 (Filter)」欄位輸入以下套件的名稱,並點選「安裝… (Install…)」即可。
    1. openvpn-openssl
    2. ip-full
    3. luci-app-openvpn



  5. 點選「儲存並套用 (Save & Apply)」,並重新整理路由器頁面。 您現在應能看到名為「VPN」的新分頁。 點選該分頁,並從下拉式選單中選擇「OpenVPN」
  6. 現在需下載 OpenVPN 用戶端設定檔。 然後,透過 Nord 帳戶即可連上推薦的伺服器。 請按照以下步驟找到手動連線設定的服務憑證
      1. 登入 Nord 帳戶,按一下**「NordVPN」,然後在 「手動設定」中點選「服務憑證 (Service credentials)」。您將可以找到手動連線需要用到的使用者名稱 (Username)** 和密碼 (Password)



      2. 現在必須下載 OpenVPN 用戶端設定檔。 請按照以下步驟找出最適合連線的伺服器:
        1. 登入 Nord 帳戶,然後點選「NordVPN」



        2. 向下捲動至「進階設定 (Advanced Settings)」,並點選「手動設定 NordVPN (Set up NordVPN manually)」



        3. 選擇「推薦伺服器 (Server recommendation)」分頁。 我們的演算法將根據您的位置推薦最適合的伺服器。



        4. 按下「進階篩選條件 (Advanced filters)」,就可以選擇「伺服器類型 (Server type)」「安全協定 (Security protocol)」來自訂推薦的伺服器。






           如欲選擇特定的伺服器,請按照以下步驟操作:
          1. 「手動設定 NordVPN (Set up NordVPN manually)」中,選擇「OpenVPN 設定檔 (OpenVPN configuration files)」



          2. 使用搜尋列或向下捲動,可以找到想要連線的伺服器。 然後,您就可以點選「下載 UDP (Download UDP)」「下載 TCP (Download TCP)」



            本指南將使用 us5104.nordvpn.com 伺服器。
  7. 「OVPN configuration file upload (上傳 OVPN 設定檔)」專區中,於「執行體名稱 (Instance name)」欄位命名 VPN 連線(我們命名為「nordvpn_us)。 然後,點選「選擇檔案 (Choose file)」,尋找已下載好的伺服器檔案,再按下「上傳 (Upload)」



  8. 「OpenVPN 執行體 (OpenVPN instances)」頁面,點選已建立執行體旁邊的「編輯 (Edit)」按鈕。



  9. 在下方欄位中,分行輸入 NordVPN 服務憑證的使用者名稱和密碼。 使用者名稱
    密碼



    您可以在 Nord 帳戶儀表板找到 NordVPN 服務憑證(使用者名稱和密碼)。
  10. 現在,複製包含帳密欄位正上方提供的憑證檔案路徑,並將其貼至上方「設定檔 (config file)」專區中的「auth-user-pass」行旁邊。 畫面應如下所示:auth-user-pass /etc/openvpn/nordvpn_us.auth


  11. 如欲連線至 VPN 伺服器,請勾選「已啟用 (Enabled)」方塊,然後按一下已建立 NordVPN 執行體旁的「開始 (Start)」按鈕。



  12. 點選位於底部的「儲存並套用 (Save & Apply)」按鈕。
  13. 在導覽選單頂部,將滑鼠停在「系統 (System)」,並點選「重新啟動 (Reboot)」。 畫面上將開啟另一個頁面。 點選「執行重啟 (Perform reboot)」按鈕,然後必須重新登入。
  14. 將滑鼠停在頁面頂部的「網路 (Network)」分頁,並選擇「介面 (Interfaces)」:1. 點選「新增新介面 (Add new interface)」
    2. 在
    「名稱 (Name)」專區中,填入 nordvpntun
    3. 點選
    「通訊協定 (Protocol)」,並選擇「未管理 (Unmanaged)」
    4. 在
    「介面 (Interface)」下拉式選單中,於底部的 -- custom -- 欄位中輸入名稱「tun0」
    ,然後按下「確認 (Enter)」鍵。



     
    
  15. 點選「建立介面 (Create interface)」儲存
  16. 選擇位於頂部的「網路 (Network)」,然後前往「防火牆 (Firewall)」專區。
  17. 點選「新增 (Add)」,並按照以下內容調整:```
  18. 將其命名為「vpnfirewall」
2.  將「輸入 (Input)」選項設定為「拒絕 (Reject)」。
3.  將「輸出 (Output)」保持為「接受 (Accept)」「轉發 (Forward)」保持為「拒絕 (Reject)」。
4.  勾選「偽裝 (Masquerading)」選項。
5.  勾選「MSS 箝制 (MSS clamping)」選項。
6.  從「已覆蓋網路 (Covered Networks)」下拉式選單中選擇「nordvpntun」。
7.  在「允許從來源區域轉發 (Allow forward from source zones)」下拉式選單中,選擇「區域網路 (Lan)」。
8.  按一下「儲存 (Save)」按鈕。
```

        ![](https://support.nordvpn.com/hc/article_attachments/36295853345553)

18. 在\*\*「區域 (Zones)」頁面中,找到名為「區域網路 (Lan)」的區域,並點選「編輯 (Edit)」\*\*按鈕。

    ![](https://support.nordvpn.com/hc/article_attachments/36295858970129)  
     

19. 在\*\*「允許轉發至目的地區域 (Allow forward to destination zones)」下拉式選單中,選取「NordVPN」\*\*條目。

    ![](https://support.nordvpn.com/hc/article_attachments/36295858979089)  
     

20. 再次點選位於頁面頂部的\*\*「網路 (Network)」,然後從下拉式選單中選擇「DHCP 與 DNS (DHCP and DNS)」\*\*。
21. 找到位於\*\*「一般設定 (General Settings)」\*\*分頁中的 DNS 轉發 (DNS forwardings) 選項,並輸入 NordVPN 的 DNS 位址。 位址為:103.86.96.100103.86.99.100。

    ![](https://support.nordvpn.com/hc/article_attachments/36295858983697)  
     

22. 前往\*\*「Resolv 與 Host 檔案 (Resolv and Hosts Files)」分頁,選取「忽略 Resolve 檔案 (Ignore resolve file)」方塊,然後點選「儲存並套用 (Save & Apply)」\*\*按鈕。

    ![](https://support.nordvpn.com/hc/article_attachments/36295853354001)  
     

23. 返回\*\*「VPN」\*\* > \*\*「OpenVPN」\*\*分頁。
24. 在\*\*「OpenVPN 執行體 (OpenVPN instances)」頁面中,選取選單中位於 NordVPN 選項旁的「已啟用 (Enabled)」選項,並點選「儲存並套用 (Save & Apply)」\*\*按鈕。

    ![](https://support.nordvpn.com/hc/article_attachments/36295858989585)  
      

25. 再次點選\*\*「開始 (Start)」\*\*按鈕連線至 VPN 伺服器。

## CLI 操作說明

如果您期望看到更進階的教學,請按照本指南進行操作。 為了在 OpenWrt 上享有 VPN 的各項好處,您需要一台同時具備 OpenWrt 韌體且啟用 OpenVPN 用戶端的路由器。 韌體主頁為 [https://openwrt.org/](https://openwrt.org/)。

1. 首先,您必須使用 SSH 的區域網路 (LAN) IP 位址存取路由器。 預設情況下,IP 位址為 192.168.1.1,使用者名稱為 root;若曾變更任一預設值,則 IP 位址可能會有差異。

2. 路由器的預設韌體映像檔中並無 OpenVPN 套件。 如欲安裝此套件,請執行以下指令:
      ```
      opkg update  
      opkg install openvpn-openssl  
      opkg install ip-full
      ```
    此外,您也可以安裝 OpenVPN 設定的 LuCI 元件,不過此元件為選擇性安裝。 執行這串指令即可進行安裝:
      ```
      opkg install luci-app-openvpn
      ```

3. 安裝好 OpenVPN 套件後,若希望套件在路由器開機時自動啟動,可執行以下命令:
      ```
      /etc/init.d/openvpn enable
      ```

4. 接著,您將需要下載伺服器設定檔。 請按照以下步驟操作:
    1. 登入 [Nord 帳戶](https://my.nordaccount.com/zh-tw/),然後點選\*\*「NordVPN」\*\*。

        ![](https://support.nordvpn.com/hc/article_attachments/36295853291537)

    2. 向下捲動至\*\*「進階設定 (Advanced Settings)」,並點選「手動設定 NordVPN (Set up NordVPN manually)」\*\*。

        ![](https://support.nordvpn.com/hc/article_attachments/36295853299089)

    3. 選擇\*\*「推薦伺服器 (Server recommendation)」\*\*分頁。 我們的演算法將根據您的位置來推薦最適合的伺服器。

        ![](https://support.nordvpn.com/hc/article_attachments/36295858945425)

    4. 按下\*\*「進階篩選條件 (Advanced filters)」,就可以選擇「伺服器類型 (Server type)」「安全協定 (Security protocol)」\*\*來自訂推薦的伺服器。

        ![](https://support.nordvpn.com/hc/article_attachments/36295858946193)

        ![](https://support.nordvpn.com/hc/article_attachments/36295853304465)

         如欲選擇特定的伺服器,請按照以下步驟操作:

        1. 在\*\*「手動設定 NordVPN (Set up NordVPN manually)」中,選擇「OpenVPN 設定檔 (OpenVPN configuration files)」\*\*。

            ![](https://support.nordvpn.com/hc/article_attachments/36295858948369)

        2. 使用搜尋列或向下捲動,可以找到想要連線的伺服器。 然後,您就可以點選\*\*「下載 UDP (Download UDP)」「下載 TCP (Download TCP)」\*\*。

            ![](https://support.nordvpn.com/hc/article_attachments/36295853314449)

5. 本指南使用了伺服器\*\*「uk2054.nordvpn.com」\*\*,但您應該使用網站推薦的伺服器。

    若要下載伺服器檔案,請選擇想連線的國家/地區,並點選「顯示可用的通訊協定 (Show available protocols)」,右鍵按一下「OpenVPN TCP」或「OpenVPN UDP」的「下載設定檔 (Download config)」,並選擇「複製連結位址 (Copy link address)」。

    然後返回 SSH 工作階段,並執行以下指令:

      ```
      wget -P /etc/openvpn https://downloads.nordcdn.com/configs/files/ovpn\_udp/servers/uk2054.nordvpn.com.udp.ovpn
      ```

    不過,請記得使用為特定伺服器檔案所複製的連結。 這串指令會將設定檔下載至\*\*「/etc/openvpn」\*\*目錄,方便快速存取。

    也可以將伺服器設定檔下載至不同的裝置上,並使用 SCP 或 SFTP 通訊協定等替代方式,將其轉移至 OpenWrt 路由器。

    _如為舊版 OpenWrt:_\
    您可以在此下載檔案:[https://downloads.nordcdn.com/configs/archives/certificates/servers.zip](https://downloads.nordcdn.com/configs/archives/certificates/servers.zip)。 在下載好的檔案中,您會找到 .crt 和 .key 副檔名的對應檔案。 這些是每個 VPN 伺服器的特有檔案。

6. 每當 OpenVPN 啟動時,NordVPN 的 OpenVPN 設定會要求您輸入 NordVPN 服務憑證的使用者名稱和密碼。 不過,我們將進行一些調整,讓程式之後會自動填入帳密。

    為了讓流程更加簡便,請先執行以下指令來安裝 nano 文字編輯器:

      ```
      opkg install nano
      ```

    或者,您也可以使用內建的 vi 文字編輯器。 更多關於文字編輯器的資訊,請參閱此[文章](https://openwrt.org/docs/guide-user/base-system/user.beginner.cli)。

    現在,請使用 nano 文字編輯器開啟下載好的伺服器設定檔。 在本範例中的指令為:

      ```
      nano /etc/openvpn/uk2054.nordvpn.com.udp.ovpn
      ```

    然後,將單字「secret」(不含引號)添加至字串「auth-user-pass」尾端。 最後整串指令應如下所示:

      ```
      auth-user-pass secret
      ```

    您必須建立新的_機密檔案_來保存 NordVPN 服務憑證。 為此,請執行以下指令:

      ```
      nano /etc/openvpn/secret
      ```

    此操作將使用 nano 文字編輯器建立並開啟新檔案。

    在檔案的第一行中,請輸入 NordVPN 服務使用者名稱,並於第二行輸入 NordVPN 服務密碼。

    您可以在 [Nord 帳戶儀表板](https://my.nordaccount.com/dashboard/nordvpn/)找到 NordVPN 服務憑證(使用者名稱和密碼)。

7. 以下兩種方式均可使用下載好的設定檔設定 OpenVPN:
    1. 將檔案的副檔名從 .ovpn 變更為 .conf,這麼一來,OpenVPN 就能透過副檔名自動找到檔案。

        為此,可以使用 mv 指令:

          ```
          mv /etc/openvpn/uk2054.nordvpn.com.udp.ovpn /etc/openvpn/uk2054.nordvpn.com.udp.conf
          ```


    2. 使用以下的「uci」指令,將檔案名稱指定為「/etc/config/openvpn」:
          ```
          uci set openvpn.nordvpn=openvpn  
          uci set openvpn.nordvpn.enabled='1'  
          uci set openvpn.nordvpn.config='/etc/openvpn/uk2054.nordvpn.com.udp.ovpn'  
          uci commit openvpn
          ```
        之後,「/etc/config/openvpn」的檔案結尾應已新增以下字串:
          ```
          uci set openvpn.nordvpn=openvpn  
          uci set openvpn.nordvpn.enabled='1'  
          uci set openvpn.nordvpn.config='/etc/openvpn/uk2054.nordvpn.com.udp.ovpn'  
          uci commit openvpn
          ```
        如欲進行確認,請執行這串指令:
          ```
          tail /etc/config/openvpn
          ```
        您也可以將檔案的副檔名從 .ovpn 改為 .conf,並於「/etc/config/openvpn」檔案中指定該副檔名。但若是這種情況,OpenVPN 僅會透過該設定檔啟動一次。

8. 請執行以下指令,建立新網路介面:
      ```
      uci set network.nordvpntun=interface  
      uci set network.nordvpntun.proto='none'  
      uci set network.nordvpntun.ifname='tun0'  
      uci commit network
      ```
    「/etc/config/network」檔案的檔案結尾現在應已新增以下字串:
      ```
      config interface 'nordvpntun'  
      option proto 'none'  
      option ifname 'tun0'
      ```
    如欲進行確認,請執行這串指令:
      ```
       tail /etc/config/network 
      ```

9. 透過執行以下指令建立新防火牆區域,並新增從區域網路轉發至 VPN 的規則:
      ```
      uci add firewall zone  
      uci set firewall.@zone\[-1\].name='vpnfirewall'  
      uci set firewall.@zone\[-1\].input='REJECT'  
      uci set firewall.@zone\[-1\].output='ACCEPT'  
      uci set firewall.@zone\[-1\].forward='REJECT'  
      uci set firewall.@zone\[-1\].masq='1'  
      uci set firewall.@zone\[-1\].mtu\_fix='1'  
      uci add\_list firewall.@zone\[-1\].network='nordvpntun'  
      uci add firewall forwarding  
      uci set firewall.@forwarding\[-1\].src='lan'  
      uci set firewall.@forwarding\[-1\].dest='vpnfirewall'  
      uci commit firewall
      ```
    如果步驟執行正確,「/etc/config/firewall」檔案結尾應已新增以下字串:
      ```
      config zone  
      option name 'vpnfirewall'  
      option input 'REJECT'  
      option output 'ACCEPT'  
      option forward 'REJECT'  
      option masq '1'  
      option mtu\_fix '1'  
      list network 'nordvpntun'  
      config forwarding  
      option src 'lan'  
      option dest 'vpnfirewall'
      ```
    如欲進行確認,請執行這串指令:
      ```
      tail -13 /etc/config/firewall
      ```
    這將顯示最後 13 行,其中包含前面的字串。

10. 現在必須設定 DNS 伺服器。 最直接的方法是使用 NordVPN DNS 作為路由器的廣域網路 (WAN) 介面。 若要新增 NordVPN DNS,請執行以下指令:````
        uci set network.wan.peerdns='0'  
        uci del network.wan.dns  
        uci add\_list network.wan.dns='103.86.96.100'  
        uci add\_list network.wan.dns='103.86.99.100'  
        uci commit
        ```
    ````

    注意:_執行 _uci del network.wan.dns_ 指令時,若收到錯誤訊息_「uci: Entry not found」_,請忽略。_  

    「/etc/config/network」檔案應包含「wan」區塊。並於檔案底端新增三條字串:  
    ```
    config interface 'wan'  
    <...>  
    option peerdns '0'  
    list dns '103.86.96.100'  
    list dns '103.86.99.100'
    ```
      
    您可以執行 cat /etc/config/network 指令進行檢查,並於輸出資料中找到「wan」介面:  
    ```
    cat /etc/config/network 
    ```
    也可以執行以下指令來新增不同的 DNS 位址,例如 Google 的 DNS 位址:  
    ```
    uci set network.wan.peerdns='0'  
    uci del network.wan.dns  
    uci add\_list network.wan.dns='8.8.8.8'  
    uci add\_list network.wan.dns='8.8.4.4'  
    uci commit
    ```
    結尾新增的字串應與先前的類似。  
     

### (選用)適用 OpenWRT 22.02 或舊版 (iptables) 的 Kill Switch

在 VPN 通道斷線時,如欲避免流量外洩,請按照以下步驟操作:

1. 使用文字編輯器開啟防火牆檔案:
      ```
      sudo nano /etc/firewall.user
      ```

2. 新增以下內容:
      ```
      \# This file is interpreted as a shell script.  
      \# Put your custom iptables rules here, and they will be executed with each firewall (re-)start  
      \# Internal uci firewall chains are flushed and recreated on reload, so  
      \# put custom rules into the root chains, e.g. INPUT or FORWARD, or into the  
      \# special user chains, e.g. input\_wan\_rule or postrouting\_lan\_rule.  
        
      if (! ip a s tun0 up) && (! iptables -C forwarding\_rule -j REJECT); then  
        
      iptables -I forwarding\_rule -j REJECT  
        
      fi
      ```

3. 執行這串指令,在「/etc/hotplug.d/iface」資料夾建立名為「99-prevent-leak」的檔案:
      ```
      nano /etc/hotplug.d/iface/99-prevent-leak
      ```

4. 將以下內容新增至指令碼:
      ```
      #!/bin/sh  
        
      if \[ "$ACTION" = ifup \] && (ip a s tun0 up) && (iptables -C forwarding\_rule -j REJECT); then  
        
      iptables -D forwarding\_rule -j REJECT  
        
      fi  
        
      if \[ "$ACTION" = ifdown \] && (! ip a s tun0 up) && (! iptables -C forwarding\_rule -j REJECT); then  
        
      iptables -I forwarding\_rule -j REJECT  
        
      fi
      ```

### (選用)適用 OpenWRT 22.03 或新版 (nftables) 的 Kill Switch

如果 VPN 通道無法正常運作,請按照下列步驟操作。

1. 編輯自訂防火牆規則檔案:
      ```
      sudo nano /etc/firewall.user
      ```

2. 寫入此功能:
      ```
      \# when tun0 interface is down and within nftables no chain "forwarding\_rule" exists, then do the following.  
      if (! ip a s tun0 up) && (! nft list chain inet fw4 forwarding\_rule; then  
      \# add new chain named forwarding\_rule  
         nft add chain inet fw4 forwarding\_rule  
      \# add rule within chain forward to jump to chain forwarding\_rule  
         nft add rule inet fw4 forward jump forwarding\_rule  
        
      \# add rule within chain forward\_rule to reject all traffic  
         nft add rule inet fw4 forwarding\_rule reject  
      fi
      ```

3. 接著需要建立自動啟用 Kill Switch 的 hotplug 指令碼:
      ```
      sudo nano /etc/hotplug.d/iface/99-prevent-leak
      ```

4. 將以下內容寫入指令碼:
      ```
      #!/bin/sh  
        
      \# if action ifup is triggered, interface tun0 is up and the nftables chain "forwarding\_rule" contains text "reject", then flush that chain in order to allow traffic.  
      if \[ "$ACTION" = ifup \] && (ip a s tun0 up) && (nft list chain inet fw4 forwarding\_rule | grep -q 'reject'); then  
      nft flush chain inet fw4 forwarding\_rule  
      fi  
        
        
      \# if action ifdown is triggered, interface tun0 is not up and the nftables chain "forwarding\_rule" does not contain text "reject", then add a rule to that chain in order to reject all traffic.  
      if \[ "$ACTION" = ifdown \] && (! ip a s tun0 up) && (! nft list chain inet fw4 forwarding\_rule | grep -q 'reject'); then  
      nft add rule inet fw4 forwarding\_rule reject  
      fi
      ```

### 自動重新連線指令碼

OpenVPN 連線有時可能會當掉,而連線日誌中會出現類似「無法解析主機… (couldn’t resolve host…)」的內容。 VPN 通道在這種情況下仍持續存在,但是 VPN 會斷線。 如欲建立能重新連上 VPN 的指令碼,請按照以下步驟操作:

1. 使用文字編輯器開啟「/etc/rc.local」檔案:
      ```
      sudo nano /etc/rc.local
      ```

2. 新增以下字串:
      ```
      /etc/openvpn/reconnect.sh &
      ```

3. 執行這串指令,在「/etc/openvpn」目錄中建立「reconnect.sh」檔案:
      ```
      sudo nano /etc/openvpn/reconnect.sh
      ```

4. 請在檔案中輸入以下指令碼:
      ```
      #!/bin/sh  
        
      n=10  
        
      while sleep 50; do  
        
      t=$(ping -c $n 8.8.8.8 | grep -o -E '\[0-9\]+ packets r' | grep -o -E '\[0-9\]+')  
        
      if \[ "$t" -eq 0 \]; then  
        
      /etc/init.d/openvpn restart  
        
      fi  
        
      done
      ```

## 連線狀態

按照以上說明操作後,路由器應能透過已完成的連線設定成功連線。 若想查看是否成功連線,請造訪 [NordVPN 的首頁](https://nordvpn.com),位於頁面頂部的狀態應顯示為「受保護 (Protected)」。

按照 GUI 步驟說明操作後,如欲中斷 VPN 連線,請點選位於\*\*「VPN」\*\* > 「OpenVPN」 > \*\*「OpenVPN instances」專區內 NordVPN 選項旁的「停止 (Stop)」\*\*按鈕。 不過,若想中斷 VPN 連線,也可執行以下指令:

```
service openvpn stop
```
本文是否有幫助?

仍遇到問題嗎?

  • 即時聊天

  • 電子郵件表格

聊天功能仰賴 Cookie。 開始聊天,即表示您同意使用 Cookie。 如欲進一步了解,請參閱我們的 Cookie 政策.