使 dnsmasq 僅監聽一個接口

使 dnsmasq 僅監聽一個接口

我在帶有 Raspbian 的樹莓派上執行 dnsmasq。 Raspberry Pi 在 wlan0 上有一個 wifi 棒,並連接到 eth0 上的本地網路/互聯網。我的目標是讓 dnsmasq 將所有主機解析為 pi 的靜態 IP,僅用於 wifi 連線。這是我的 dnsmasq.conf

address=/#/192.168.42.1
interface=wlan0

然而現在,當我通過 eth0 ssh 到 pi 並 ping google.com 時,它也解析為 192.168.42.1。也許是因為這個(來自手冊頁):

使用 --interface 選項時,Dnsmasq 會自動將環回(本機)介面新增至要使用的介面清單中

如果是這樣,我怎麼能讓 dnsmasq 只監聽 wifi 連線?

解決方案

就我的目的而言,邏輯可以是:「如果插入了乙太網路電纜,請關閉dnsmasq。如果拔掉乙太網路電纜,則將其重新打開」。

因此,我習慣於ifplugd管理流程。在文件中,/etc/ifplugd/ifplud.action我新增了service dnsmasq stop「向上」情況和service dnsmasq start「向下」情況。圓滿成功!

答案1

嘗試代替

except-interface=eth0

避免 dnsmasq 監聽 eth0

答案2

你的處理方式是錯誤的。 dnsmasq 根本沒有錯。

Linux 上的大多數 DNS 解析器都使用/etc/resolv.conf.那裡定義的內容是與介面無關的。事實上,它必須是:除非您有數位 IP 位址,否則您不知道要採用哪個介面和路線。

所以基本上你想要的都做不到。

當然,除非它實際上根本不是用於 dnsmasq 主機,而是用於 WiFi 用戶端或其他什麼。在這種情況下,您需要刪除本機 DNS 回應程式resolv.conf並使其直接使用上游(您的路由器、ISP、Google DNS、OpenDNS...)。

使用 Google DNS 時,典型的resolv.conf情況可能如下所示:

nameserver 8.8.8.8
nameserver 8.8.4.4

相關內容