New comers to the Linux generally asks for what is the purpose and usage of the /etc/hosts
file. /etc
directory holds system and service related configuration. hosts
file is used to store host name or DNS to IP address resolution. It provides easy way to match a host or DNS name with an IP address.
Linux的新手通常会询问/etc/hosts
文件的目的和用途是什么。 /etc
目录包含与系统和服务相关的配置。 hosts
文件用于存储主机名或DNS到IP地址的解析。 它提供了一种简单的方法来将主机名或DNS名称与IP地址进行匹配。
列出当前主机或DNS到IP地址的分辨率 (List Current Host or DNS to IP Address Resolutions)
We can easily read and print current content of the /etc/hosts
file. Its structure is very easy to read as we will examine it below.
我们可以轻松读取和打印/etc/hosts
文件的当前内容。 它的结构很容易阅读,我们将在下面进行检查。
$ cat /etc/hosts
![List Current Host or DNS to IP Address Resolutions](https://i-blog.csdnimg.cn/blog_migrate/5b6e93b887332031ba3fddfb9372f323.png)
主机名(Hostname)
FQDN is structured naming of hosts in computer networks. You can read following tutorial to get more details about FQDN.
FQDN是计算机网络中主机的结构化命名。 您可以阅读以下教程,以获取有关FQDN的更多详细信息。
What is FQDN (Fully Qualified Domain Name) with Examples?
We can use FQDN is hosts
file but providing host and domain name like below. In this case we add host test
which is under lab.com
domain with IP address 192.168.1.100
我们可以使用FQDN是hosts
文件,但提供如下所示的主机名和域名。 在这种情况下,我们添加在IP地址为192.168.1.100
lab.com
域下的主机test
192.168.1.100 test.lab.com
别名 (Alias)
As a system administrator we generally prefer to use practical ways. If the host is in the same domain with us or domain is not important we can just provide the alias like below. In this example we will provide alias test
to the IP address 192.168.1.100
作为系统管理员,我们通常更喜欢使用实用的方法。 如果主机与我们位于同一域中,或者域并不重要,我们可以提供如下别名。 在此示例中,我们将为IP地址192.168.1.100
提供别名test
192.168.1.100 test
主机名别名 (Alias with Hostname)
hosts
file also provides the ability to use both single alias
or hostname. We will add them for the same IP address. Syntax is like below.
hosts
文件还提供了使用单个alias
或主机名的功能。 我们将为相同的IP地址添加它们。 语法如下。
IPADDRESS HOSTNAME ALIAS
In this example the IP address is 192.168.1.100
alias is test
and hostname is test.lab.com
在此示例中,IP地址为192.168.1.100
别名为test
,主机名为test.lab.com
#IPADDRESS HOSTNAME ALIAS
192.168.1.100 test.lab.com test
翻译自: https://www.poftut.com/what-is-etc-hosts-file-and-how-to-create-and-edit-it/