我正在用 Raspberry Pi 4 构建 WLAN 路由器。内部 WLAN 适配器用作热点接入点,内部以太网将连接到网络。
我在 Ubuntu 24.04 上使用 NetworkManager 和 Netplan 来进行设置,几乎一切都运行良好。
我唯一遇到的问题是,我无法让连接到 WLAN 的客户端进行 DNS 名称解析,从而解析路由器本身的主机名。这是相关的,因为路由器还托管一个小型 HTTP 前端,我希望客户端能够访问。
所以,我有以下网络计划:
network:
version: 2
renderer: NetworkManager
ethernets:
eth0:
dhcp4: true
optional: true
wifis:
wlan0:
dhcp4: true
addresses:
- 192.168.200.1/24
access-points:
"MyHotSpotSSID":
password: "MyHotSpotPassword"
mode: ap
此外,我已将路由器主机添加到我的
/etc/cloud/templates/hosts.debian.tmpl
,以便重新启动后我的/etc/hosts
显示:
# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
# a.) make changes to the master file in /etc/cloud/templates/hosts.debian.tmpl
# b.) change or remove the value of 'manage_etc_hosts' in
# /etc/cloud/cloud.cfg or cloud-config from user-data
#
192.168.200.1 myrouter
127.0.1.1 myrouter myrouter
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
我现在可以将 WLAN 客户端连接到
MyHotSpotSSID
的 myrouter
-WLAN 并访问互联网。太棒了!
但是,从我的客户那里,我无法
ssh
、ping
或 nslookup
路由器 myrouter
本身! 建议的解决方法是什么?
就上下文而言,路由器似乎确实知道自己的主机名:
me@myrouter:~$ hostnamectl
Static hostname: myrouter
Icon name: computer
Machine ID: 391dffd2af51405487cd6c8011e68776
Boot ID: 62c61f761043491c9e669deb19400d0c
Operating System: Ubuntu 24.04 LTS
Kernel: Linux 6.8.0-1009-raspi
Architecture: arm64
在客户端,似乎确实知道将 DNS 请求定向到我的路由器:
me@myclient:~$ nmcli device show wlx3c33320101af
GENERAL.DEVICE: wlx3c33320101af
GENERAL.TYPE: wifi
GENERAL.HWADDR: 3C:33:32:01:01:AF
GENERAL.MTU: 1500
GENERAL.STATE: 100 (verbunden)
GENERAL.CONNECTION: MyHotSpotSSID
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/5
IP4.ADDRESS[1]: 192.168.200.131/24
IP4.GATEWAY: 192.168.200.1
IP4.ROUTE[1]: dst = 192.168.200.0/24, nh = 0.0.0.0, mt = 600
IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 192.168.200.1, mt = 600
IP4.DNS[1]: 192.168.200.1
IP6.ADDRESS[1]: fe80::d47:75ec:5a98:6ce/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 1024
当我通过客户端 DNS 缓存询问时,nslookup
失败:
me@myclient:~$ nslookup myrouter
;; Got SERVFAIL reply from 127.0.0.53
Server: 127.0.0.53
Address: 127.0.0.53#53
** server can't find myrouter: SERVFAIL
路由器本身似乎确实提供了良好的 DNS 回复(尽管顺序并不好兆头):
meg@myclient:~$ nslookup myrouter 192.168.200.1
Server: 192.168.200.1
Address: 192.168.200.1#53
Name: myrouter
Address: 127.0.1.1
Name: myrouter
Address: 192.168.200.1
有人可以帮忙吗?先谢谢各位朋友了!
TL;DR:使用
.local
域
这是我经过一些研究和来自 @grawity_u1686 的评论的提示后有些不完整的答案:
我连接的客户端使用
systemd-resolved
作为本地 DNS 解析器(标准 Ubuntu 24.04 桌面)。正如 systemd-resolved 手册页中所述,它根据所查询的主机名类型提供不同的行为:
.local
域的主机名使用 MulticastDNS 进行解析,这对我来说确实是开箱即用的。所以,我的第一次尝试是使用选项 3. 并为
myrouter
分配一个域,这可以通过修改其 /etc/cloud/cloud.cfg
来完成,如下所示:
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false
# ==> new entries <==
hostname: myrouter
fqdn: myrouter.mydomain.org
prefer_fqdn_over_hostname: true
有了这个,
myrouter.mydomain.org
从我的客户那里解决得很好
但是,在阅读了为您的家庭网络使用哪些域名之后,我明白,发明自己的域名是不好的做法。
因此,我最终选择使用上面的
myrouter.local
和解析器选项 2。