如何在 Ubuntu 上設定 DNS:Netplan 與 NetworkManager 完整指南

1. 介紹:為何在 Ubuntu 中 DNS 設定很重要

DNS(Domain Name System,域名系統)是將網域名稱轉換為 IP 位址的機制。
每次我們開啟網站時,作業系統都會在背景向 DNS 伺服器查詢。

在使用 Ubuntu 時,您可能會遇到:

  • 網頁感覺「有點慢」
  • 效能比同一網路內的其他裝置慢
  • 偶爾無法存取內部 LAN 網站

這些症狀令人驚訝地常常不是因為「網路品質」不佳,而是 DNS 解析緩慢所致。

在 Ubuntu 中,即使是 22.04 版及之後的版本,也有多種設定 DNS 的方式,這常讓新手感到困惑。主要的兩種方法是:

  • Netplan(常見於沒有圖形介面的伺服器環境)
  • NetworkManager(支援桌面圖形介面的操作)

由於設定會依所使用的方式而異,Ubuntu 的 DNS 教學必須先「偵測環境」,再「引導使用者使用正確的方法」。

DNS 不只是「設定裡的一小部份」,而是 Ubuntu 整個網路系統的入口點。

例如,僅僅切換到 Google Public DNS(8.8.8.8)或 Cloudflare(1.1.1.1)就能明顯提升瀏覽速度。這在 VPS、雲端或海外網路環境中尤其明顯。

以下章節中,我們將明確區分:

  • 如何使用圖形介面設定 DNS
  • 如何透過 Netplan 設定 DNS
  • 如何驗證 DNS 設定

首先,先判斷您的系統使用的是哪種環境。

2. Ubuntu 提供兩大主要的 DNS 設定方式

在 Ubuntu 中,DNS 的設定方式會依所使用的網路管理系統而不同。若忽略此區分,DNS 的變更可能不會生效,或在重新開機後恢復原狀。

在此我們總結 Ubuntu 具備兩套獨立的 DNS 設定系統這一事實。

Netplan(基於 YAML 的設定)

  • 常見於伺服器環境
  • 在 Ubuntu 18.04 及之後的 LTS 版本中已標準化
  • 設定檔位於 /etc/netplan/*.yaml
  • 與 systemd‑resolved 協同運作

在沒有圖形介面的 VPS 或實體伺服器環境中,幾乎都會使用 Netplan。AWS、Vultr、ConoHa、Oracle Cloud 等雲端環境也通常採用此方式。

NetworkManager(圖形介面)

  • 常見於桌上型電腦環境(Ubuntu Desktop)
  • 可透過 IPv4 / IPv6 設定 DNS
  • 由於採用圖形介面,設定上較易理解

如果您使用的是 Ubuntu Desktop,這最有可能是使用的方式。當您只想「因為瀏覽器感覺慢」而更改 DNS 時,這是最理想的選擇。

如何檢查您使用的是哪一種

最簡單的方式是檢查 /etc/netplan/ 目錄下是否有檔案。

ls /etc/netplan/

如果有 YAML 檔案,表示可能在使用 Netplan。若目錄為空或您使用的是圖形介面,則請檢查 NetworkManager 設定。

3. 使用 Netplan 設定 DNS(適用於伺服器)

Netplan 透過 YAML 檔案定義網路設定。在沒有圖形介面的 Ubuntu Server 或 VPS 環境中,這幾乎是唯一的設定方式。

本節聚焦於設定 DNS 為特定值的最小實作步驟。

開啟 Netplan 設定檔

Netplan 的設定檔儲存在 /etc/netplan/。實際檔名會依環境而異(例如 00-installer-config.yaml)。

先檢查檔案清單:

ls /etc/netplan/

確認檔名後,使用 nano 等編輯器開啟它。例如:

sudo nano /etc/netplan/00-installer-config.yaml

如何在 YAML 中加入 DNS 設定(範例)

以下範例同時指定 Google DNS 與 Cloudflare DNS。

network:
  version: 2
  ethernets:
    ens33:
      dhcp4: true
      nameservers:
        addresses: [8.8.8.8, 1.1.1.1]

註:ens33 會依您的網卡而異,可使用 ip aip link 來確認。

套用設定

Apply changes immediately using:

sudo netplan apply

If errors appear, YAML indentation is the most common cause.
Verify spacing and hierarchy—tabs cannot be used.

DHCP 與手動 DNS 能共存嗎?

Yes, you can obtain an IP via DHCP while manually specifying DNS.
是的,您可以透過 DHCP 取得 IP,同時手動指定 DNS。

Example:

dhcp4: true
nameservers:
  addresses: [9.9.9.9]

This configuration sets “IP = automatic, DNS = manual.”
此設定將「IP = 自動,DNS = 手動」。

4. 在 Ubuntu 桌面上使用 NetworkManager(圖形介面)設定 DNS

If you are using Ubuntu for desktop purposes, you can change DNS without using the terminal.
如果您在桌面上使用 Ubuntu,您可以不透過終端機變更 DNS。
This GUI-based method is the quickest when you simply want to “speed up the browser” or “switch to a public DNS.”
當您只想「加速瀏覽器」或「切換至公共 DNS」時,這種圖形介面方式是最快的。

如何開啟設定視窗

  1. Click the network icon at the top right
    點擊右上角的網路圖示
  2. Open “Settings” or “Network Settings”
    開啟「設定」或「網路設定」
  3. Select the active connection (Wired / Wi‑Fi)
    選取已啟用的連線(有線 / 無線)
  4. Go to the “IPv4” tab
    前往「IPv4」分頁

You will find a DNS input field here.
您會在此看到 DNS 輸入欄位。
Depending on the Ubuntu version, the wording may differ slightly, but you can enter DNS addresses separated by commas.
依 Ubuntu 版本不同,文字說明可能略有差異,但您可以以逗號分隔輸入 DNS 位址。

範例輸入(以 IPv4 指定 DNS)

Example: Using Google DNS and Cloudflare DNS
範例:使用 Google DNS 與 Cloudflare DNS

8.8.8.8, 1.1.1.1

After entering the values, click “Apply” or “Save.” It is recommended to disconnect and reconnect the network to ensure settings are applied.
輸入完畢後,點擊「套用」或「儲存」。建議斷線後再重新連線,以確保設定生效。

若使用 IPv6

The “IPv6” tab has a similar DNS input field.
「IPv6」分頁也有類似的 DNS 輸入欄位。
On dual‑stack networks, setting both IPv4 and IPv6 DNS may be important for stability.
在雙協議堆疊的網路中,同時設定 IPv4 與 IPv6 DNS 可能對穩定性很重要。

同時使用 DHCP 與手動 DNS

You can also set up the GUI to use automatic IP assignment while manually specifying DNS.
您也可以在圖形介面中設定自動取得 IP,同時手動指定 DNS。
This is useful when you don’t want to manually assign a fixed IP on every Wi‑Fi network you use, such as at home or in the office.
當您不想在每個使用的 Wi‑Fi 網路(如家裡或辦公室)都手動設定固定 IP 時,這很有用。

5. 如何確認 DNS 設定已套用

DNS configuration is not complete until you verify that the new settings are actually applied.
在驗證新設定確實生效之前,DNS 設定尚未完成。
Ubuntu offers three reliable methods to check DNS status.
Ubuntu 提供三種可靠的方式檢查 DNS 狀態。

使用 dig 指令

dig google.com

Within the output, look for a line labeled “SERVER: 〜”.
This shows which DNS server your system is currently using.
在輸出中,尋找標示為「SERVER: 〜」的行。這會顯示 系統目前使用的 DNS 伺服器

Example (excerpt):

;; SERVER: 8.8.8.8#53(8.8.8.8)

Verify that it shows 8.8.8.8 (Google) or 1.1.1.1 (Cloudflare), etc.
確認顯示的是 8.8.8.8(Google)或 1.1.1.1(Cloudflare)等。

resolvectl status

This method is accurate when using systemd‑resolved.
在使用 systemd‑resolved 時,此方法相當精確。

resolvectl status

This displays the nameserver currently used by each NIC.
此指令會顯示每張網卡目前使用的名稱伺服器。
On multi‑NIC servers, this is more reliable than dig.
在多網卡伺服器上,這比 dig 更可靠。

為何不應直接編輯 /etc/resolv.conf

cat /etc/resolv.conf

This file shows the final DNS values currently in use.
此檔案顯示目前使用的最終 DNS 值。
However, the file is automatically generated by systemd‑resolved, meaning:
然而,此檔案是由 systemd‑resolved 自動產生,意味著:

It will be overwritten, so editing this file is incorrect.
它會被覆寫,因此直接編輯此檔案是不正確的。

6. 常見 DNS 選項(新手友善)

You do not “create” DNS server addresses yourself.
您不需要自行「建立」DNS 伺服器位址。
In most cases, you choose from widely available public DNS services.
大多數情況下,您可以從廣為人知的公共 DNS 服務中挑選。

If you want safe, stable options, choose from the list below:
若想要安全、穩定的選項,請從以下清單中挑選:

ProviderDNS Address
Google Public DNS8.8.8.8 / 8.8.4.4
Cloudflare1.1.1.1
Quad99.9.9.9
OpenDNS208.67.222.222 / 208.67.220.220

Specifying two DNS servers is recommended for redundancy.
建議指定 兩個 DNS 伺服器 以提升冗餘性。
If one becomes unavailable, the system automatically queries the other.
若其中一個無法使用,系統會自動改查詢另一個。

If you use internal DNS (such as corporate AD), you must specify the internal DNS server instead of public DNS.
若使用內部 DNS(例如企業 AD),必須指定 內部 DNS 伺服器 而非公共 DNS。
Internal name resolution often takes priority over external DNS performance.
內部名稱解析通常優先於外部 DNS 效能。

7. DNS 常成為網路瓶頸

While DNS appears to be a simple configuration item, it has significant impact on perceived network performance.
雖然 DNS 看似簡單的設定項目,卻對使用者感受到的網路效能有重大影響。
DNS issues are especially noticeable in scenarios such as:
DNS 問題在以下情境中特別明顯:

  • The first step of loading a webpage feels unusually slow
    載入網頁的第一步驟感覺異常緩慢
  • Ping is fast but web browsing feels sluggish
    Ping 很快,但瀏覽網頁卻很卡
  • SPA frameworks (React / Vue) have a slow initial load
    SPA 框架(React / Vue)首次載入緩慢

這些情況常常會出現這樣的行為:「同一個 URL 連續載入幾次後會變快,但第一次存取卻異常緩慢。」
這是因為 DNS 是最先進入的環節。

特別是在 VPS 或國際區域(例如 us-east / eu-west)時,公共 DNS 往往比 ISP 預設的 DNS 更快。

DNS 是一個對 網路延遲 極為敏感的點。
不論使用何種作業系統,DNS 調校都是 Web 工程師應該首先進行的優化之一。

FAQ

Q1: 我編輯了 /etc/resolv.conf,但重開機後會被重置。為什麼?
→ Ubuntu 的 systemd-resolved 會自動 產生 /etc/resolv.conf
此檔案不應手動編輯。
請改用 Netplan 或 NetworkManager。

Q2: 我不確定自己在使用 Netplan 還是 NetworkManager。要怎麼檢查?
→ 先檢查:

ls /etc/netplan/

如果有 YAML 檔案,通常表示使用 Netplan。
如果你使用的是圖形介面,則較可能是 NetworkManager。

Q3: 能否自動透過 DHCP 取得 IP,同時手動設定 DNS?
→ 可以。
Netplan 與 NetworkManager 都支援「IP = AUTO,DNS = manual」的設定。

Q4: 更改 DNS 就一定能加速上網嗎?
→ 不一定。
DNS 只影響最初的名稱解析。
它常能加快第一次載入的速度,但若圖片、CDN、API 等其他資源仍然緩慢,整體效能仍會受影響。

Q5: 同樣的步驟能套用在 Ubuntu 的 WSL2 上嗎?
→ 不完全相同。
WSL2 會自動重新產生 resolv.conf,需要額外設定,例如:
generateResolvConf=false
WSL 有其專屬的 DNS 設定方式。

年収訴求