You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Avahi故障:树莓派无法解析自身及局域网主机名

树莓派Avahi无法解析本地主机名(含自身)的排查与解决

问题现象

  • 树莓派上Avahi服务正常运行,但无法解析任何本地mDNS主机名,包括自身的personalbox.local
  • 本机执行ping personalbox.local提示Name or service not known,其他设备也无法通过该主机名访问树莓派

已完成的排查步骤

  • 确认Avahi服务处于活跃运行状态
  • 已安装libnss-mdns包
  • /etc/nsswitch.conf配置符合常规要求
  • 已禁用ufw防火墙

相关配置与状态信息

Avahi服务状态

● avahi-daemon.service - Avahi mDNS/DNS-SD Stack
     Loaded: loaded (/lib/systemd/system/avahi-daemon.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2025-08-31 12:35:42 PDT; 3s ago
TriggeredBy: ● avahi-daemon.socket
   Main PID: 3604 (avahi-daemon)
     Status: "avahi-daemon 0.8 starting up."
      Tasks: 2 (limit: 4915)
        CPU: 18ms
     CGroup: /system.slice/avahi-daemon.service
             ├─3604 avahi-daemon: running [personalbox.local]
             └─3605 avahi-daemon: chroot helper

Aug 31 12:35:42 personalbox avahi-daemon[3604]: Found user 'avahi' (UID 108) and group 'avahi' (GID 113).
Aug 31 12:35:42 personalbox avahi-daemon[3604]: Successfully dropped root privileges.
Aug 31 12:35:42 personalbox avahi-daemon[3604]: avahi-daemon 0.8 starting up.
Aug 31 12:35:42 personalbox systemd[1]: Started Avahi mDNS/DNS-SD Stack.
Aug 31 12:35:42 personalbox avahi-daemon[3604]: Successfully called chroot().
Aug 31 12:35:42 personalbox avahi-daemon[3604]: Successfully dropped remaining capabilities.
Aug 31 12:35:42 personalbox avahi-daemon[3604]: Loading service file /services/multiple.service.
Aug 31 12:35:42 personalbox avahi-daemon[3604]: Network interface enumeration completed.
Aug 31 12:35:42 personalbox avahi-daemon[3604]: Server startup complete. Host name is personalbox.local. Local service cookie is 376431368.
Aug 31 12:35:42 personalbox avahi-daemon[3604]: Service "personalbox" (/services/multiple.service) successfully established.

/etc/nsswitch.conf配置

# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         files
group:          files
shadow:         files
gshadow:        files

hosts:          files mdns_minimal [NOTFOUND=return] dns mdns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis

/etc/hosts配置

127.0.0.1   localhost
::1     localhost ip6-localhost ip6-loopback
127.0.1.1   personalbox

/etc/avahi/avahi-daemon.conf配置

# This file is part of avahi.
#
# avahi is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# avahi is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with avahi; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.

# See avahi-daemon.conf(5) for more information on this configuration
# file!

[server]
#host-name=foo
#domain-name=local
#browse-domains=0pointer.de, zeroconf.org
use-ipv4=yes
use-ipv6=no
allow-interfaces=eth0
allow-interfaces=wlan0
#deny-interfaces=eth1
#check-response-ttl=no
#use-iff-running=no
#enable-dbus=yes
#disallow-other-stacks=no
#allow-point-to-point=no
#cache-entries-max=4096
#clients-max=4096
#objects-per-client-max=1024
#entries-per-entry-group-max=32
ratelimit-interval-usec=1000000
ratelimit-burst=1000

[wide-area]
enable-wide-area=yes

[publish]
#disable-publishing=no
#disable-user-service-publishing=no
#add-service-cookie=no
#publish-addresses=yes
#publish-hinfo=no
publish-workstation=yes
publish-domain=yes
#publish-dns-servers=192.168.50.1, 192.168.50.2
#publish-resolv-conf-dns-servers=yes
#publish-aaaa-on-ipv4=yes
#publish-a-on-ipv6=no

[reflector]
#enable-reflector=no
#reflect-ipv=no
#reflect-filters=_airplay._tcp.local,_raop._tcp.local

[rlimits]
#rlimit-as=
#rlimit-core=0
#rlimit-data=8388608
#rlimit-fsize=0
#rlimit-nofile=768
#rlimit-stack=8388608
#rlimit-nproc=3

端口监听状态

> ss -a | grep avahi
nl    UNCONN 0      0                                            rtnl:avahi-daemon/3604               *
u_str LISTEN 0      0                        /run/avahi-daemon/socket 1717                           * 0

本机ping测试结果

> ping personalbox.local
ping: personalbox.local: Name or service not known

解决方案

1. 修复Avahi网络端口监听问题

从ss输出可见,Avahi未监听mDNS默认的UDP 5353端口,这是核心问题:

  • 执行ip link show eth0 wlan0确认配置的接口是否处于UP状态
  • 若接口未启用,执行sudo ip link set eth0 up或sudo ip link set wlan0 up,之后重启Avahi服务:sudo systemctl restart avahi-daemon
  • 再次执行ss -a | grep 5353,若出现类似以下输出则说明监听正常:
udp    UNCONN 0      0         *:5353                  *:*                    

2. 修正/etc/nsswitch.conf的hosts解析顺序

当前配置中mdns_minimal [NOTFOUND=return]会导致本地文件无法解析时直接返回失败,跳过mDNS解析。修改为:

hosts:          files mdns dns

修改后无需重启,系统会自动加载新配置。

3. 调整Avahi接口绑定策略

若指定接口绑定失败,可注释掉avahi-daemon.conf中的allow-interfaces配置,让Avahi自动绑定所有可用接口:

#allow-interfaces=eth0
#allow-interfaces=wlan0

重启Avahi服务后验证端口监听状态。

4. 验证mDNS解析功能

使用Avahi自带工具测试内部解析:

avahi-resolve -n personalbox.local

若返回正确IP,说明Avahi服务本身正常,问题出在系统nss配置;若仍失败,查看Avahi日志排查接口绑定错误:journalctl -u avahi-daemon


内容的提问来源于stack exchange,提问作者Chichri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 10:54:50