/etc/hosts

更新时间:
2024-03-14
下载文档

/etc/hosts

hosts(the static table lookup for host name) 主机名查询静态表。

/etc/hosts 文件用于系统快速解析IP地址与域名/主机名间对应关系。系统按照先DNS缓存在 /etc/hosts 文件最后DNS服务的顺序对域名进行解析,所以在hosts文件中配置了的域名就不再需要耗时较久的通过网络域名解析协议,能快速获取域名网络地址。再有,如果系统裁剪了DNS服务或DNS服务器地址没有正确配置,而又想通过域名来访问,也可以通过hosts文件文件来实现。

hosts文件包含了IP地址与域名/主机名之间的映射,还包括主机的别名。通过将常用的域名和IP地址映射加入到hosts文件中,实现快速方便的访问。

hosts 文件格式

ip地址   主机名/域名   (主机别名)
ip地址   主机名

每台主机都有一个 IP 地址,它区分开每台主机,并可以根据 ip 进行通讯。但 IP 地址不方便记忆,所以在一个局域网中,每台机器都设置一个不同主机名,用于区分主机,便于相互访问。扩展到互联网,因为主机数太多,就增加了域名来指定公网内的主机。

  • 主机名通常在局域网内使用,通过 hosts 文件,主机名就被解析到对应的 ip。
  • 域名通常在 internet 上使用,但是优先级低于 hosts 文件中内容,因此如果你不想使用 internet 上的域名解析,可以更改自己的 hosts 文件,加入自己的域名解析。

实例

默认情况下 /etc/hosts 文件中只有一条配置 127.0.0.1 localhost ,所以我们可以用 localhost 来替代自回环地址 127.0.0.1 来访问本机。我们也可以换个名字或者加个别名来访问。对 /etc/hosts 文件的修改,无需重启即可生效,也说明系统在每次解析主机名或域名时都要查询一次该文件。

[root@sylixos2:/root]# cat /etc/hosts
127.0.0.1    localhost
[root@sylixos2:/root]# ping localhost
Execute a DNS query...
Pinging localhost [127.0.0.1]

Pinging 127.0.0.1

Reply from 127.0.0.1: bytes=32 time=0.054ms TTL=255
Reply from 127.0.0.1: bytes=32 time=0.016ms TTL=255
Reply from 127.0.0.1: bytes=32 time=0.016ms TTL=255
Reply from 127.0.0.1: bytes=32 time=0.016ms TTL=255

Ping statistics for 127.0.0.1:
    Packets: Send = 4, Received = 4, Lost = 0(0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0.016ms, Maximum = 0.054ms, Average = 0.025ms

[root@sylixos2:/root]#
[root@sylixos2:/root]# vi /etc/hosts
[root@sylixos2:/root]# cat /etc/hosts
127.0.0.1    localhost2 inet
[root@sylixos2:/root]# ping localhost2
Execute a DNS query...
Pinging localhost2 [127.0.0.1]

Pinging 127.0.0.1

Reply from 127.0.0.1: bytes=32 time=0.051ms TTL=255
Reply from 127.0.0.1: bytes=32 time=0.016ms TTL=255
Reply from 127.0.0.1: bytes=32 time=0.016ms TTL=255
Reply from 127.0.0.1: bytes=32 time=0.015ms TTL=255

Ping statistics for 127.0.0.1:
    Packets: Send = 4, Received = 4, Lost = 0(0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0.015ms, Maximum = 0.051ms, Average = 0.024ms

[root@sylixos2:/root]# ping inet      
Execute a DNS query...
Pinging inet [127.0.0.1]

Pinging 127.0.0.1

Reply from 127.0.0.1: bytes=32 time=0.890ms TTL=255
Reply from 127.0.0.1: bytes=32 time=0.044ms TTL=255
Reply from 127.0.0.1: bytes=32 time=0.016ms TTL=255
Reply from 127.0.0.1: bytes=32 time=0.016ms TTL=255

Ping statistics for 127.0.0.1:
    Packets: Send = 4, Received = 4, Lost = 0(0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0.016ms, Maximum = 0.890ms, Average = 0.241ms

[root@sylixos2:/root]# 

不配置DNS服务器,设备是无法访问到 www.baidu.com ,此时可以配置 /etc/hosts 文件为百度配置域名和别名地址,即可正常访问。

[root@sylixos2:/root]# 
[root@sylixos2:/root]# ifconfig          
en1       Link encap: Ethernet HWaddr: f6:4f:aa:4c:69:51
          Dev: e1000_0 Ifidx: 2 DHCP: D4 D6 Spd: 1000 Mbps
          inet addr: 192.168.196.89 netmask: 255.255.255.0
          gateway: 192.168.196.1 broadcast: 192.168.196.255
          inet6 addr: fe80::f44f:aaff:fe4c:6951 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          collisions:0 txqueue:0 tcpaf:2 tcpwnd:65535
          RX ucast packets:13 nucast packets:571087 dropped:0
          TX ucast packets:10 nucast packets:8 dropped:0
          RX bytes:38021006 (36.2 MB)  TX bytes:1296 (1.2 KB)

lo0       Link encap: Local Loopback
          Dev: N/A Ifidx: 1 DHCP: D4 D6 Spd: N/A
          inet addr: 127.0.0.1 netmask: 255.0.0.0
          P-to-P: 127.0.0.1 broadcast: N/A
          inet6 addr: ::1 Scope:Loopback
          UP LOOPBACK RUNNING  MTU:0  Metric:1
          collisions:0 txqueue:0 tcpaf:2 tcpwnd:262140
          RX ucast packets:75 nucast packets:0 dropped:0
          TX ucast packets:75 nucast packets:0 dropped:0
          RX bytes:4488 (4.3 KB)  TX bytes:4488 (4.3 KB)

dns0: 0.0.0.0
dns1: 0.0.0.0
dns2: 0.0.0.0
dns3: 0.0.0.0
default device is: en1
list net interface: 2
[root@sylixos2:/root]# ping www.baidu.com
Execute a DNS query...
Pinging request could not find host www.baidu.com .Please check the name and try again.

parameter(s) error.
[root@sylixos2:/root]#
[root@sylixos2:/root]# vi /etc/hosts
[root@sylixos2:/root]# cat /etc/hosts
127.0.0.1    localhost2 inet
180.101.50.188  www.baidu.com   baidu

[root@sylixos2:/root]# ping www.baidu.com
Execute a DNS query...
Pinging www.baidu.com [180.101.50.188]

Pinging 180.101.50.188

Reply from 180.101.50.188: bytes=32 time=19.369ms TTL=48
Reply from 180.101.50.188: bytes=32 time=19.327ms TTL=48
Reply from 180.101.50.188: bytes=32 time=19.059ms TTL=48
Reply from 180.101.50.188: bytes=32 time=18.921ms TTL=48

Ping statistics for 180.101.50.188:
    Packets: Send = 4, Received = 4, Lost = 0(0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 18.921ms, Maximum = 19.369ms, Average = 19.169ms

[root@sylixos2:/root]# ping baidu        
Execute a DNS query...
Pinging baidu [180.101.50.188]

Pinging 180.101.50.188

Reply from 180.101.50.188: bytes=32 time=18.952ms TTL=48
Reply from 180.101.50.188: bytes=32 time=19.110ms TTL=48
Reply from 180.101.50.188: bytes=32 time=18.973ms TTL=48
Reply from 180.101.50.188: bytes=32 time=18.931ms TTL=48

Ping statistics for 180.101.50.188:
    Packets: Send = 4, Received = 4, Lost = 0(0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 18.931ms, Maximum = 19.110ms, Average = 18.991ms

[root@sylixos2:/root]# 
文档内容是否对您有所帮助?
有帮助
没帮助