dnsmasq 無法將 .local 位址指向 127.0.0.1

dnsmasq 無法將 .local 位址指向 127.0.0.1

我想獲取.local指向的地址(例如 test.local),127.0.0.1但它不起作用。我使用的是最新的 Ubuntu。在 /etc/NetworkManager/NetworkManager.conf 中我註解掉了dns=dnsmasq然後做了sudo service network-manager restart

然後我安裝了 dnsmasq 並將其新增address=/local/127.0.0.1至 /etc/dnsmasq.d/mycustomfile 然後執行了sudo service dnsmasq restart。除了我提到的之外,我沒有做其他改變。

但是當我訪問 test.local 時,它無法解析為 127.0.0.1,對該 .local 位址執行 ping 操作也不會成功。也許內容/etc/resolv.conf是相關的,這只是預設的我沒有改變它:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 222.11.22.36
nameserver 222.11.22.37

這是我不以守護程式執行 dnsmasq 時的輸出:

$ sudo dnsmasq --no-daemon     
dnsmasq: started, version 2.68 cachesize 150
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth
dnsmasq: reading /etc/resolv.conf
dnsmasq: using nameserver 222.11.22.37#53
dnsmasq: using nameserver 222.11.22.36#53
dnsmasq: read /etc/hosts - 9 addresses

當 dnsmasq 運行時,/etc/resolv.conf 會自動更新(並在 dnsmasq 停止時恢復到上面的條目):

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.0.1

和這個:

$ sudo nslookup test.local 127.0.0.1
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   test.local
Address: 127.0.0.1

但仍然無法 ping 本機。

我需要做什麼才能讓 dnsmasq 解析 .local 位址?

答案1

local似乎是某種保留關鍵字,至少在 Ubuntu 上是如此:

  • address=/local/127.0.0.1——不起作用。
  • address=/loc/127.0.0.1——有效。

答案2

看起來 dnsmasq 已正確啟動。您應該測試 dnsmasq 是否可以正確使用nslookup test.local 127.0.0.1.

為了確保系統正在使用 dnsmasq,您必須更新以/etc/resolv.conf用作nameserver 127.0.0.1第一個選項。

您也可以在 dnsmasq 上使用 custo 上游解析器設定--resolv-file=/etc/resolv.conf.dnsmasq

答案3

avahi-daemon 可能正在使用網域「local」。看:

https://unix.stackexchange.com/questions/352237/avahi-daemon-and-local-domain-issues

答案4

如果你想要的只是指向你自己的機器,而不是嘗試使用 dnsmasq 來控制其他計算機的 dns 解析,那麼你想要的實際上只是在/etc/hosts.我建議您撤銷對 dnsmasq 及其設定的所有更改,並確保/etc/hosts頂部有以下內容:

127.0.0.1 localhost
127.0.1.1 mycomputername mycomputername.local anothername.local

之後,ping mycomputername.local 將指向您自己(實際上,將指向在 127.0.1.1 上執行的 dnsmasq 伺服器,然後指向 localhost)。您甚至可以為網路上的其他電腦添加條目,以便在您看來,在這台電腦上,電腦可以被引用為computer1.local computer2.local等。

192.168.1.31 computer1.local
192.168.1.32 computer2.local

如果您似乎無法撤銷使用 dnsmasq 所做的更改,那麼您可以在 後面新增這些附加條目localhost,但不要刪除或更改localhost條目本身。像這樣:

127.0.0.1 localhost mycomputername mycomputername.local anothername.local

順便一提:在 Ubuntu 桌面上修改 resolv.conf 的正確方法是使用工作列中的網路管理員。 Ubuntu 伺服器中的正確方法是dns-nameserver在網路設備下新增項目,/etc/network/interfaces然後重新啟動或執行ifdown eth0 && ifup eth0。正如狀態中的警告/etc/resolv.conf,不要直接修改它,它是根據/etc/network/interfaces設定動態寫入的。

相關內容