什么是/ etc / hosts文件以及如何创建和编辑它?

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
List Current Host or DNS to IP Address Resolutions
列出当前主机或DNS到IP地址的分辨率

主机名(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?

什么是带有示例的FQDN(完全合格域名)?

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
LEARN MORE  How to Find Detailed Domain Information with whois Command?
了解更多信息如何使用whois命令查找详细的域信息?

翻译自: https://www.poftut.com/what-is-etc-hosts-file-and-how-to-create-and-edit-it/

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/etc/hosts 文件是一个系统级别的文件,通常位于 Linux 和 Unix-like 操作系统的根目录下。这个文件用于存储 IP 地址与主机名或域名之间的映射关系,这样当计算机尝试解析一个地址时,如果没有 DNS 服务器的响应,系统可以直接使用 hosts 文件中的映射。 修改 /etc/hosts 文件的步骤如下: 1. **打开终端**:在大多数 Linux 发行版中,你可以通过在终端中输入 `nano /etc/hosts` 或者 `vi /etc/hosts`(如果你更熟悉 vi 编辑器)来打开该文件。对于 Windows 用户,可能需要借助管理员权限通过命令提示符或 PowerShell 打开。 2. **添加或编辑条目**:在打开的文本编辑器中,你可以在文件的末尾或需要修改的位置添加新的条目,格式通常是: ``` IP地址 域名/主机名 ``` 例如,将本地 IP 127.0.0.1 映射到 localhost 或某个特定网站的 IP(如 8.8.8.8): ``` 127.0.0.1 localhost 8.8.8.8 google.com ``` 3. **保存并关闭**:在编辑完内容后,保存并关闭编辑器。如果你使用的是 nano 编辑器,按 `Ctrl+X`,然后选择 `Y` 保存,再按 `Enter` 关闭。在 vi 编辑器中,先输入 `:wq` 保存并退出。 4. **刷新缓存**:修改 hosts 文件后,有时需要手动刷新系统缓存使其生效。在终端里输入 `sudo hostname -R` 或 `sudo ipconfig /flushdns`(Windows 中是 `ipconfig /release` 和 `ipconfig /renew`)。 请注意,直接修改 hosts 文件可能会对网络行为产生影响,如阻止 DNS 解析、影响某些服务的正常运行等。因此,在进行修改前最好确保了解其影响。如果你不确定某条记录是否应该添加,建议先备份原始文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值