使用 mDNS 在局域网中轻松发现系统
mDNS 零配置系统
![](/data/attachment/album/201809/23/220646civ0wdw1z9mt9100.jpg)


mDNS（<ruby> 多播 DNS <rt>  Multicast DNS </rt></ruby>）允许系统在局域网中广播查询其他资源的名称。Fedora 用户经常在没有复杂名称服务的路由器上接有多个 Linux 系统。在这种情况下，mDNS 允许你按名称与多个系统通信 —— 多数情况下不用路由器。你也不必在所有本地系统上同步类似 `/etc/hosts` 之类的文件。本文介绍如何设置它。


mDNS 是一个零配置网络服务，它已经诞生了很长一段时间。Fedora Workstation 带有零配置系统 Avahi（它包含 mDNS）。 （mDNS 也是 Bonjour 的一部分，可在 Mac OS 上找到。）


本文假设你有两个系统运行受支持的 Fedora 版本（27 或 28）。它们的主机名是 castor 和 pollux。


### 安装包


确保系统上安装了 nss-mdns 和 avahi 软件包。你可能是不同的版本，这也没问题：



```
$ rpm -q nss-mdns avahi
nss-mdns-0.14.1-1.fc28.x86_64
avahi-0.7-13.fc28.x86_64
```

Fedora Workstation 默认提供这两个包。如果不存在，请安装它们：



```
$ sudo dnf install nss-mdns avahi
```

确保 `avahi-daemon.service` 单元已启用并正在运行。同样，这是 Fedora Workstation 的默认设置。



```
$ sudo systemctl enable --now avahi-daemon.service
```

虽然是可选的，但你可能还需要安装 avahi-tools 软件包。该软件包包括许多方便的程序，用于检查系统上的零配置服务的工作情况。使用这个 `sudo` 命令：



```
$ sudo dnf install avahi-tools
```

`/etc/nsswitch.conf` 控制系统使用哪个服务用于解析，以及它们的顺序。你应该在那个文件中看到这样的一行：



```
hosts: files mdns4_minimal [NOTFOUND=return] dns myhostname
```

注意命令 `mdns4_minimal [NOTFOUND=return]`。它们告诉你的系统使用多播 DNS 解析程序将主机名解析为 IP 地址。即使该服务有效，如果名称无法解析，也会尝试其余服务。


如果你没有看到与此类似的配置，则可以（以 root 用户身份）对其进行编辑。但是，nss-mdns 包会为你处理此问题。如果你对自己编辑它感到不舒服，请删除并重新安装该软件包以修复该文件。


在**两个系统**中执行同样的步骤 。


### 设置主机名并测试


现在你已完成常见的配置工作，请使用以下方法之一设置每个主机的名称：


1. 如果你正在使用 Fedora Workstation，[你可以使用这个步骤](https://fedoramagazine.org/set-hostname-fedora/)。
2. 如果没有，请使用 `hostnamectl` 来做。在第一台机器上这么做：`$ hostnamectl set-hostname castor`。
3. 你还可以编辑 `/etc/avahi/avahi-daemon.conf`，删除主机名设置行上的注释，并在那里设置名称。但是，默认情况下，Avahi 使用系统提供的主机名，因此你**不应该**需要此方法。


接下来，重启 Avahi 守护进程，以便它接收更改：



```
$ sudo systemctl restart avahi-daemon.service
```

然后正确设置另一台机器：



```
$ hostnamectl set-hostname pollux
$ sudo systemctl restart avahi-daemon.service
```

只要你的路由器没有禁止 mDNS 流量，你现在应该能够登录到 castor 并 ping 通另一台机器。你应该使用默认的 .local 域名，以便解析正常工作：



```
$ ping pollux.local
PING pollux.local (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1 (192.168.0.1): icmp_seq=1 ttl=64 time=3.17 ms
64 bytes from 192.168.0.1 (192.168.0.1): icmp_seq=2 ttl=64 time=1.24 ms
...
```

如果你在 pollux `ping castor.local`，同样的技巧也适用。现在在网络中访问你的系统更方便了！


此外，如果你的路由器也支持这个服务，请不要感到惊讶。现代 WiFi 和有线路由器通常提供这些服务，以使消费者的生活更轻松。


此过程适用于大多数系统。但是，如果遇到麻烦，请使用 avahi-browse 和 avahi-tools 软件包中的其他工具来查看可用的服务。




---


via: <https://fedoramagazine.org/find-systems-easily-lan-mdns/>


作者：[Paul W. Frields](https://fedoramagazine.org/author/pfrields/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Multicast DNS, or mDNS, lets systems broadcast queries on a local network to find other resources by name. Fedora users often own multiple Linux systems on a router without sophisticated name services. In that case, mDNS lets you talk to your multiple systems by name — without touching the router in most cases. You also don’t have to keep files like */etc/hosts* in sync on all the local systems. This article shows you how to set it up.


mDNS is a zero-configuration networking service that’s been around for quite a while. Fedora ships Avahi, a zero-configuration stack that includes mDNS, as part of Workstation. (mDNS is also part of Bonjour, found on Mac OS.)

This article assumes you have two systems running supported versions of Fedora (27 or 28). Their host names are meant to be *castor* and *pollux*.

## Installing packages

Make sure the *nss-mdns* and *avahi* packages are installed on your system. You might have a different version, which is fine:

$rpm -q nss-mdns avahinss-mdns-0.14.1-1.fc28.x86_64 avahi-0.7-13.fc28.x86_64

Fedora Workstation provides both of these packages by default. If not present, install them:

$sudo dnf install nss-mdns avahi

Make sure the *avahi-daemon.service* unit is enabled and running. Again, this is the default on Fedora Workstation.

$sudo systemctl enable --now avahi-daemon.service

Although optional, you might also want to install the *avahi-tools* package. This package includes a number of handy utilities for checking how well the zero-configuration services on your system are working. Use this *sudo* command:

$sudo dnf install avahi-tools

The */etc/nsswitch.conf* file controls which services your system uses to resolve services, and in what order. You should see a line like this in that file:

hosts: files mdns4_minimal [NOTFOUND=return] dns myhostname

Notice the commands *mdns4_minimal [NOTFOUND=return].* They tell your system to use the multicast DNS resolver to resolve a hostname to an IP address. Even if that service works, the remaining services are tried if the name doesn’t resolve.

If you don’t see a configuration similar to this, you can edit it (as the *root* user). However, the *nss-mdns* package handles this for you. Remove and reinstall that package to fix the file, if you’re uncomfortable editing it yourself.

Follow the steps above for **both systems**.

## Setting host name and testing

Now that you’ve done the common configuration work, set up each host’s name in one of these ways:

- If you’re using Fedora Workstation,
[you can use this procedure](https://fedoramagazine.org/set-hostname-fedora/). - If not, use
*hostnamectl*to do the honors. Do this for the first box:$

**hostnamectl set-hostname castor** - You can also edit the
*/etc/avahi/avahi-daemon.conf*file, remove the comment on the*host-name*setting line, and set the name there. By default, though, Avahi uses the system provided host name, so you**shouldn’t**need this method.

Next, restart the Avahi daemon so it picks up changes:

$sudo systemctl restart avahi-daemon.service

Then set your other box properly:

$hostnamectl set-hostname pollux$sudo systemctl restart avahi-daemon.service

As long as your network router is not disallowing mDNS traffic, you should now be able to login to *castor* and ping the other box. You should use the default *.local* domain name so resolution works correctly:

$ping pollux.localPING pollux.local (192.168.0.1) 56(84) bytes of data. 64 bytes from 192.168.0.1 (192.168.0.1): icmp_seq=1 ttl=64 time=3.17 ms 64 bytes from 192.168.0.1 (192.168.0.1): icmp_seq=2 ttl=64 time=1.24 ms ...

The same trick should also work from *pollux* if you ping *castor.local*. It’s much more convenient now to access your systems around the network!

Moreover, don’t be surprised if your router advertises services. Modern WiFi and wired routers often provide these services to make life easier for consumers.

This process works for most systems. However, if you run into trouble, use *avahi-browse* and other tools from the *avahi-tools* package to see what services are available.

## Edgar Hoch

You mention that package nss-mdns modifies /etc/nsswitch.conf in scriptlets. Yes, it does, and this is the problem: It conflicts with authselect on Fedora 28, because no other program than authselect should modify /etc/nsswitch.conf. See https://bugzilla.redhat.com/show_bug.cgi?id=1577243 .

## Paul W. Frields

@Edgar: It could equally be said that authselect’s expectation that only it ever touches nsswitch.conf, without reasonable facilities for handling more situations required by modern users, is also the problem. Apparently the upstreams are sorting the situation out, and I look forward to amending this article if/when that happens.

## Edgar Hoch

Paul, yes, I agree. The change from authconfig to authselect in Fedora 28 was to early, authselect is still not ready for all situations, there should be tools to modify configuration files but only to create a complete new profile. I had to do this for nis, because the developers had decided that only sssd and winbind will be neccessary. But sssd doen’t work well with nis. And I did need nscd, because Fedora 28 had decided to set IPAddressDeny=any in systemd-logind.service. Now they have changed there mind and nss_nis adds a file that set IPAddressAllow=any.

I am already following upstream authselect at github. They have created a nis profile, that’s fine. I am not sure if adding file user-nsswitch.conf is a good solution for the problem. But we man see…

Another note: Is it intended that you set hostname in your example to a name without a domain? Shouldn’t it contain at least .localdomain? I think that some programs may have problems (e.g. sendmail may delay startup).

## Paul W. Frields

@Edgar: Indeed, some users may need to name their box e.g. “castor.localdomain” if they’re using certain other services. That’s not often the case for home network users, but it’s a very good point nonetheless — thanks.

## Slavisa

I have been using Fedora 27 for a long time and I am quite happy. Is there any reason for the update?

Thanks in advance!

Kindest regards,

Slavisa

## vortex

avahi is great, except it is unfortunate that it has never properly supported DNS-SD in that registrations over mdns can be reflected in unicast dynamic DNS, unlike the macos equivalent mDNSresponder (or the posix version).

## Andreas WERNER

Are there more infos about using mDNS?

What happens if there are more than one mDNS services/servers are running in a LAN? Are they concurrent?

I have a prof audio device from MOTU which is also using mDNS.

How to see all the MDNS systems running in a LAN? And all *.local domain names announced?

Best regards

Andreas WERNER

## Paul W. Frields

@Andreas: While I’m not an expert at mDNS, the way the service is designed is that each system that implements it advertises itself. No central server is used or expected, and all the services on the LAN are expected to coexist. The .local domain is the default domain for mDNS so it’s expected that services would support it. Check your unit’s manufacturer documentation or website for more information on their implementation if you don’t see expected results.

## Andreas WERNER

@ Paul Frields:

Thank you for your reply!

## RG

There are firewalls that block mDNS traffic. E.g. Freifunk in Germany does such kind because it heavily increases needed bandwith. For home or a virtual environment, mDNS is great.

## vortex

Hi RG,

mDNS consists of a multicast packet transmitted with a TTL of one, it never leaves your local network segment unless specific multicast routing is present on the LAN.

Cheers.

## driverrestore

That’s not often the case for home network users, but it’s a very good point nonetheless — thanks.I have been using Fedora 27 for a long time and I am quite happy.

## dag

Might seem obvious but if you’re trying to setup dual stack ipv4/6 make sure you change the setting from mdns4_minimal to mdns_minimal so that ipv6 addresses are also resolved.