Arch/labwc 環境網絡相關設置

Arch 安裝的時候,沒插網線的話,archinstall 會提示你使用 iwd 相關套件連接無線網絡,能滿足最基本的網絡需求。

本文記錄我的常用網絡設置,主要包含 NetworkManager、iwd、Dnsmasq、waybar 相關配置。

1. 安裝 NetworkManager、dnsmasq、iwd

1sudo pacman -S networkmanager dnsmasq iwd

選擇 NetworkManager 功能強大,你能遇到的網絡相關需求都能滿足,畢竟大部分桌面環境預裝,自帶的 nmcli、nmtui 等工具也很好用。

Dnsmasq 在小型網絡中很實用,比如家庭網絡。

iwd 依賴輕,值得信賴。

2. 配置文件

在 NetworkManager 中啓用 dnsmasq

1➜  ~ cat /etc/NetworkManager/conf.d/dns.conf
2[main]
3dns=dnsmasq

設置 dnsmasq 緩存

1➜  ~ cat /etc/NetworkManager/dnsmasq.d/custom-cache.conf
2cache-size=4096

cach-size 的意思是它在內存中緩存 DNS 查詢結果的最大條目數量。

-c, --cache-size= Set the size of dnsmasq's cache. The default is 150 names. Setting the cache size to zero disables caching. Note: huge cache size impacts performance.

雖然 systemd-resolved 也提供類似功能,但 Dnsmasq 還能自定義域名、屏蔽廣告等。另外 systemd 太龐雜了,什麼都要接管的感覺。

自定義域名

示例展示自動代理、路由器各定義了一個域名,比較容易記住。

1➜  ~ cat /etc/NetworkManager/dnsmasq.d/custom-domain.conf
2address=/proxy.local/192.168.1.5
3address=/route.local/192.168.124.1

上游 DNS 設置

實測上海電信 dns 部分時候丟包,騰訊 dns 部分時候延時高。最後,選了阿里雲的。

每個人網絡狀況不一,多測試使用選個適合自己的。

 1➜  ~ cat /etc/NetworkManager/dnsmasq.d/upstream-dns.conf
 2# aliyun
 3server=223.5.5.5
 4server=223.6.6.6
 5
 6# cf
 7server=1.1.1.1
 8
 9# 優化參數(可選,提升解析效率)
10strict-order       # 嚴格按配置順序使用上游DNS(先試第一個,失敗再試第二個)
11no-poll            # 禁用DNS輪詢,減少資源佔用
12proxy-dnssec       # 轉發DNSSEC請求(提升解析安全性)

當然還可設置廣告屏蔽等,常訪問的網站基本沒什麼廣告,也表示理解。

設置啓用 iwd 爲 WiFi 後端

1➜  ~ cat /etc/NetworkManager/conf.d/wifi_backend.conf
2[device]
3wifi.backend=iwd
4wifi.iwd.autoconnect=yes

iwd 依賴很少(請看下面👇Depends On 那一欄),只依賴內核和 C 庫。

 1➜  ~ sudo pacman -Qi iwd
 2Name            : iwd
 3Version         : 3.10-1
 4Description     : Internet Wireless Daemon
 5Architecture    : x86_64
 6URL             : https://git.kernel.org/cgit/network/wireless/iwd.git/
 7Licenses        : LGPL-2.1-or-later
 8Groups          : None
 9Provides        : None
10Depends On      : glibc  readline  libreadline.so=8-64  ell  gcc-libs
11Optional Deps   : qrencode: for displaying QR code after DPP is started [installed]
12Required By     : None
13Optional For    : networkmanager
14Conflicts With  : None
15Replaces        : None
16Installed Size  : 2.18 MiB
17Packager        : Andreas Radke <andyrtr@archlinux.org>
18Build Date      : Fri 26 Sep 2025 01:46:26 PM CST
19Install Date    : Tue 09 Dec 2025 05:06:33 PM CST
20Install Reason  : Explicitly installed
21Install Script  : No
22Validated By    : Signature

不信可以看看官方 wiki

The result is a self-contained environment that only depends on the Linux Kernel and the runtime C library.

iwd 架構圖

iwd architecture

剛好迷你主機帶的 Intel 網卡,iwd 是 Intel 爲 Linux 編寫的,我還買了 Intel 的股票,賭美國會拯救牙膏廠。

1➜  ~ lspci | grep -i network
202:00.0 Network controller: Intel Corporation Wi-Fi 6 AX200 (rev 1a)

這樣一通配置,已經能滿足日常網絡需求。

3. waybar 網絡相關配置

如果你看得上老朽的配置,建議參考倉庫:https://github.com/kmephistoh/dotfiles

配置在 config/waybar 目錄下可直接複用。

1"network": {
2	"format": "{icon} {essid} {signalStrength}%",
3	"format-wifi": "  {ifname} {ipaddr}/{cidr} {signalStrength}%",
4	"format-ethernet": "󰈀 {ipaddr}",
5	"format-disconnected": "󰖪 disconnected",
6	"tooltip-format": "{ifname}: {ipaddr}/{cidr} via {gwaddr}",
7	"interval": 5
8},

記得安裝 nerd 字體,不然圖標部分會顯示方塊:

1sudo pacman -S ttf-jetbrains-mono-nerd

waybar 效果截圖:

waybar-network

能看到網卡名、ip 信息、無線網絡信號強度等,當然顯示 ssid 也是可以的,改下配置即可。

4. 其它

按照上述配置,如果足夠細心,你可能會有點疑惑🤔:

1➜  ~ ps aux |grep dnsmasq
2nobody       786  0.0  0.0  14900  7680 ?        S    19:05   0:00 /usr/bin/dnsmasq --no-resolv --keep-in-foreground --no-hosts --bind-interfaces --pid-file=/run/NetworkManager/dnsmasq.pid --listen-address=127.0.0.1 --cache-size=400 --clear-on-reload --conf-file=/dev/null --enable-dbus=org.freedesktop.NetworkManager.dnsmasq --conf-dir=/etc/NetworkManager/dnsmasq.d

明明我寫的 cache-size=4096,爲什麼上面啓動項參數裏面還是--cache-size=400? 如何確定當前 cache-size 是生效的?

其實是生效的,驗證方法是給 dnsmasq 的進程發送 SIGUSR1 信號,當 dnsmasq 收到 SIGUSR1 信號時,它會將統計信息寫入系統日誌。

示例如下,先使用 pgrep 命令找到 pid,再用 kill 命令發送信號(注意同時打開日誌查看 journalctl -u NetworkManager -f)

1➜  ~ pgrep dnsmasq
2786
3➜  ~ sudo kill -USR1 786

同時查看日誌輸出

 1➜ ~ journalctl -u NetworkManager -f
 2Dec 23 19:29:54 minipc dnsmasq[786]: time 1766488194
 3Dec 23 19:29:54 minipc dnsmasq[786]: cache size 4096, 0/298 cache insertions re-used unexpired cache entries.
 4Dec 23 19:29:54 minipc dnsmasq[786]: queries forwarded 126, queries answered locally 81
 5Dec 23 19:29:54 minipc dnsmasq[786]: queries for authoritative zones 0
 6Dec 23 19:29:54 minipc dnsmasq[786]: DNSSEC per-query subqueries HWM 0
 7Dec 23 19:29:54 minipc dnsmasq[786]: DNSSEC per-query crypto work HWM 0
 8Dec 23 19:29:54 minipc dnsmasq[786]: DNSSEC per-RRSet signature fails HWM 0
 9Dec 23 19:29:54 minipc dnsmasq[786]: pool memory in use 720, max 1776, allocated 2400
10Dec 23 19:29:54 minipc dnsmasq[786]: child processes for TCP requests: in use 0, highest since last SIGUSR1 0, max allowed 20.
11Dec 23 19:29:54 minipc dnsmasq[786]: server 223.5.5.5#53: queries sent 126, retried 0, failed 0, nxdomain replies 0, avg. latency 21ms
12Dec 23 19:29:54 minipc dnsmasq[786]: server 223.6.6.6#53: queries sent 0, retried 0, failed 0, nxdomain replies 0, avg. latency 0ms
13Dec 23 19:29:54 minipc dnsmasq[786]: server 1.1.1.1#53: queries sent 0, retried 0, failed 0, nxdomain replies 0, avg. latency 0ms
14Dec 23 19:29:54 minipc dnsmasq[786]: server 192.168.124.1#53: queries sent 0, retried 0, failed 0, nxdomain replies 0, avg. latency 0ms

上面日誌的輸出中,包含 cache size 4096, 0/298 cache insertions re-used unexpired cache entries.,表明當前生效的的確是 4096

如果長期不關電腦、或者是路由器等,dns 緩存還是能加快網絡訪問速度的。

最後,萬一網絡掉線了,如何重連網絡呢?

首先,在家裏我一年到頭都遇不到幾次掉線。如果真的掉線,labwc 環境下,我偏向命令行操作。

  • 命令行:nmcli device wifi connect ssid password your_wifi_password
  • tui:nmtui-connect 按界面指示操作,很簡單的。

還可以用 iwctl, iwd 自動的命令行工具。

1iwctl # 進入無線工具
2device list # 查無線網卡名(如 wlan0)
3station wlan0 scan # 掃描WiFi
4station wlan0 get-networks # 列WiFi列表
5station wlan0 connect "WiFi名" # 輸入密碼連接
6exit # 退出工具
7ping archlinux.org # 驗證聯網

推薦使用 nmtui-connect,畢竟誰也不想記住那麼長的命令,當然還有 NetworkManager 的 各種 GUI 可供選擇。

都用窗口管理器了,我看沒有那個必要。

一般來說,WiFi 網絡連接信息,Linux 系統是自動保存在配置文件中的,開機啓動會自動連接,斷線的概率很低。

板載的無線網卡,在夏天散熱條件不佳的情況下,可能會遇到。

最後修改於: Tuesday, December 23, 2025

相關文章:

翻譯: