云网牛站
所在位置:首页 > Linux教程 > 在Fedora中采用mDNS在局域网中发现系统

在Fedora中采用mDNS在局域网中发现系统

2018-09-24 15:12:05作者:geekpi稿源:中国LINUX社区

本文介绍在 Fedora 系统中采用 mDNS 在局域网中发现系统的方法,本文假设你有两个系统运行受支持的 Fedora 27 或者 28 版本,它们的主机名是 castor 与 pollux。

 

前言

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

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

 

安装包

确保系统上安装了 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,你可以使用下方 How to set the hostname on 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 软件包中的其他工具来查看可用的服务。

 

附:How to set the hostname on Fedora

A Fedora system has a hostname that helps it identify and distinguish itself on a network. Sometimes this name appears as part of a fully qualified domain name (FQDN). A FQDN includes not just the system’s name, but the Internet domain, separated by periods (.).

1.Hostname conventions

To be valid, a hostname may only contain letters a-z, numerals 0-9, and dashes (-). An example is office-01. The FQDN for that machine might be office-01.example.com, where example.com is the domain.

Each Fedora system also has a special reserved name, localhost, which it sometimes uses to refer to itself. This may sound like overkill, but it’s useful. The localhost lets the system easily access services that it is providing itself. You may also see this reserved name as a FQDN in the form localhost.localdomain.

2.Setting the hostname

To set the name of a single, modern Fedora system such as a home computer that isn’t part of a network, use the hostnamectl command:

hostnamectl set-hostname new-name

3.Getting fancy

The hostnamectl utility distinguishes between three different kinds of names:

The static name used by default at system bootup

The transient name assigned by network configuration

The pretty name which may be more descriptive, like “Mary’s living room laptop”

The pretty name isn’t limited to just the valid characters for static or transient name.

The command above sets all names to the same value. To set only one of them, use one or more of the options –static, –transient, or –pretty.

The static name is stored in the /etc/hostname file for later reference. You can also check the current status of all names with this command:

hostnamectl status

The utility tracks other information, such as icons that may be used to represent the system in graphical interfaces.

4.Using Cockpit

You can also use Cockpit to control the hostname settings in your system, or a remote system.

First, from the dashboard select System. Notice this dashboard refers to localhost, which is your local system itself.

在Fedora中采用mDNS在局域网中发现系统

Select the Host Name to modify the current settings:

在Fedora中采用mDNS在局域网中发现系统

 

相关主题

3种方法更改Linux系统的主机名(hostname)

Redhat7.2修改主机名(hostname)

精选文章
热门文章