在 /etc/hostname 中,我有myname
.
在 /etc/hosts 中,我有:
127.0.0.1 localhost.localdomain localhost myname
::1 localhost.localdomain localhost myname
Run Code Online (Sandbox Code Playgroud)
我对什么是真正的主机名以及它的用途有一个模糊的理解。
这些更精确的问题可以帮助我更好地理解它:
在上面的例子中我的主机名到底是什么?myname
或者是其他东西?如果我改为myname.domain.com
in /etc/hostname
,那么我真正的主机名会是什么?
在示例中,命令hostname
返回myname
,而命令hostname -f
返回localhost.localdomain
。我记得这/etc/hosts
只是名称和 IP 地址之间的映射。它似乎在这里提供了另一个功能。什么是 fqdn,它的用途是什么?它是如何检索的?为什么localhost.localdomain
?
如果我改为myname.domain.com
in /etc/hostname
,hostname -f
就会返回myname.domain.com
。为什么?
为什么 fqdn 在这些文件中不以点结尾?
是hostname -d
和dnsmydomain
等价的吗?
我可以在什么情况下使用我的主机名 ( myname
) 以及什么时候必须使用我的 fqdn ( localhost.localdomain
)?
Inc*_*rsi 14
/etc/hostname
包含机器的名称,如本地运行的应用程序所知。/etc/hosts
和 DNS 将名称与 IP 地址相关联。myname
可以映射到机器可以访问自己的任何 IP 地址,但映射到它127.0.0.1
是不美观的。/etc/hosts
,但/bin/hostname
与-f
./etc/hosts
可以凌驾于常识之上。小心编辑,不要在临时补丁和实验后留下垃圾。.
TLD.
和 ? .
TLD)都是可以接受的。前者是明确的,而后者是无处不在的。dnsmydomain
,但不太可能。从/etc/hosts
技术上讲,在其中寻找东西并不是“DNS”。localhost.localdomain
)?– 用于从外部访问机器。FQDN 在使用它的地方必须是可解析的。Ell*_*iew 10
/etc/hostname和/etc/hosts很简单,但也没有那么简单...
/etc/hostname
和/etc/hosts
1)编辑/etc/hostname。
有 2 种有效的方法可以进行设置。
要么做到:
machine
,即此处理器的简单单字名称。joe
”可能是您自己选择的机器名称。或者
machine.domain.net
,即完全限定域名 (FQDN)。joe.xyz.com
”。一旦在/ etc /主机名设置,那么下一步是让这些命令hostname --all-fqdn
和dnsdomainname
工作正常,按照这个顺序,如下所示:
2)编辑/etc/hosts。
其中有一行表示该主机的FQDN,例如“machine.domain.net”,前缀为该机器的 IP 地址(可能来自动态 IP 地址服务器),如下所示:
# IP FQDN (CANONICAL_HOSTNAME) ALIASES
#-------------- --------------------------- ------------------------
192.168.1.14 Joe.xyz.com joe
Run Code Online (Sandbox Code Playgroud)
首先,请注意大小写在这里无关紧要。 提示:但是,设置一些上限可以让我们在接下来的步骤中注意到事情的来源。所以,我有资本Ĵ OE。
另请注意,为了简单hostname
(即 /etc/hostname = 'machine')正确生成 FQDN,/etc/hosts 中必须存在名为 'machine'的别名。这就是别名joe出现的原因。(顺便说一句,即使您不使用该别名,也可以使用该别名。)否则不需要也不使用该别名。
另请注意,“domain.net”必须是可解析的,即 DNS 中必须存在 A 记录。这就是所谓的“可解析”(即通过 DNS 解析器)。
3) 现在将这些新设置从 /etc/hostname 和 /etc/hosts 安装到内核中,运行:
$ /etc/init.d/hostname.sh # Or by rebooting
Run Code Online (Sandbox Code Playgroud)
4) 测试主机名
$ hostname # Reports the hostname previously read from in /etc/hostname.
joe
Run Code Online (Sandbox Code Playgroud)
或者
$ hostname
joe.xyz.com
Run Code Online (Sandbox Code Playgroud)
提示:
请注意,这不是 /etc/hostname 当前的内容,就像您现在再次编辑它一样,而是 /etc/init.d/hostname.sh 脚本之前读取的内容,可能是重新启动的结果。
还要注意,'j' 是小写的,所以这表明它来自 /etc/hostname,而不是 /etc/hosts。
5) 测试 FQDN
$ hostname --all-fqdn #reports the FQDN(s) matched in /etc/hosts.
Joe.xyz.com # It searches /etc/hosts for the joe in /etc/hostname.
# Notice that the 'J' is uppercase.
# This suggest it came from /etc/hosts.
Run Code Online (Sandbox Code Playgroud)
6)测试DNS域名
$ dnsdomainname #reports the computed DNS doman name
xyz.com # (i.e. the right most part of the FQDN;
# the part to the right of the first, left most, dot)
# This is coming from Joe.xyz.com in /hosts,
# after 'Joe.' is removed.
Run Code Online (Sandbox Code Playgroud)
现在,这应该可以更轻松地设置 Web 和电子邮件服务器。
我非常讨厌有人告诉我阅读手册,但在这种情况下,请务必查看主机名的手册页,大多数(如果不是全部)问题都应该得到解答。它写得比我能解释的要好得多。
为了提供一些帮助,针对您的问题的一些摘录如下: - 您的主机名是您在 /etc/hosts 中看到的内容。- 返回的 FQDN 基于解析器返回的内容,因此您会看到 /etc/hosts 中的值被返回,因为解析器首先检查主机文件,然后执行任何必要的 DNS 查询。
我不相信主机名手册页中提到的一个问题(基于我自己重新阅读的手册页)是文件中的条目不以点结尾的原因。
处理 DNS 记录时使用该约定。主机名和主机文件实际上并不是 DNS 记录。如果您在绑定中配置这些条目以解析 DNS 查找,那么可以使用点(查看这个好资源:http://www.zytrax.com/books/dns/apa/dot.html)。
另外,请记住,所有这些值都不会从系统中广播。因此,如果您希望另一个系统基于 FQDN 进行访问,则它基于该另一个系统上的主机文件,或者缺少基于外部系统在 FQDN 上执行的 DNS 查找的主机条目,以允许其解析回系统。