Linux Hosts file detailed and configuration

Source: Internet
Author: User
Tags domain name server

1. Noun analysis

Host Name:

Whether it is on the LAN or the Internet, each host has an IP address to distinguish the current machine (in fact, the bottom is the use of the physical address of the machine), that is, the IP address is a host number, the only indicator of this machine.

Public network:

The IP address is not easy to remember, so there is a domain name. Domain names exist only in the public network (the INTERNET), each domain name corresponds to an IP address, but an IP address can correspond to multiple domain names .

Lan:

Each machine has a hostname for the difference between host and host. We can set the host name for each machine, so that it is easy to access each other in a convenient way. For example, we can be named according to the function of each machine in LAN.

Use: For example, we can log on to someone else's computer remotely via the host name via SSH command.

2, about/etc/host, hostname and IP configuration file

Hosts-the static table lookup for host name (host-Name query-still tables)

Use :

The Hosts file is a file that is responsible for fast parsing of IP addresses and domain names in Linux systems.

Storage :

Saved in ASCII format in the "/etc" Directory , the file name "hosts" (different versions of Linux, this configuration file may also be different.) For example, Debian's counterpart file is/etc/hostname).

content :

The hosts file contains mappings between IP addresses and host names , and also includes aliases for host names.

function :

In the absence of a domain name server, all network programs on the system can query the file to resolve the IP address corresponding to a host name, otherwise it will need to use the DNS service program to resolve. You can often add common domain names and IP address mappings to the Hosts file for quick and easy access.

3. Priority level

Priority: DNS Cache > hosts > DNS Service

4. Configuring the Hosts File

This file can be configured with the host IP and the corresponding hostname, which is not negligible for the server type Linux system. On the LAN or the Internet, each host has an IP address that separates each host and can communicate based on IP. But the IP address is not easy to remember, so there is a domain name. In a local area network, each machine has a host name, which is used to differentiate the host and facilitate mutual access.

The relevant configuration file of the Linux hostname is/etc/hosts; This file tells the host which domain names correspond to those IPs, and which IP addresses those host names:

Like there's a definition in the file.

192.168.1.100 linumu100 test100

Assuming 192.168.1.100 is a Web server, entering http://linumu100 or http://test100 on a Web page will open the 192.168.1.100 Web page.

Typically, this file first records the IP and hostname of the machine:

This is when we debug the Web project, we clearly entered in the browser's address bar is localhost, he will know that we are local debugging, because he mapped to 127.0.0.1 (that is, this machine).

5. configuration file Format description

However, this mapping is only a local machine mapping, that is, each machine is independent, only in the hosts file configured to use the host name to access.

In the Hosts file, we'll see something like the following

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4127.0.0.1    Localhost.localdomain localhost192.168.1.195  debian.localdomain Debian

In general, the content of the hosts is about the hostname (Hostname) definition, each behavior of a host, each line consists of three parts, each part separated by a space. where #号the beginning of the line is a description (that is, the role of the comment ), not interpreted by the system.

Hosts file format:

IP address host name/domain name (host alias)

The first part: Network IP address;

Part Two: hostname or domain name;

The third part: hostname alias;

Of course, each line can also be two parts, that is, the host IP address and host name, such as 192.168.1.100 linmu100.

The difference between host name (hostname) and domain name (domains):

    • Host name is usually used in the LAN, through the Hosts file, the host name is resolved to the corresponding IP;
    • Domain names are typically used on the Internet, but if you don't want to use domain name resolution on the Internet, you can change the hosts file to include your own domain name resolution.

6. The purpose of the Hosts file

6.1 Troubleshooting Slow logons to a Linux host

Sometimes the client wants to telnet to a Linux host, but each login password will wait for a long time to enter, this is because the Linux host in the return of information need to resolve IP, if the hosts in the Linux host file in advance to join the client's IP address, Then telnet to Linux from the client will become very fast.

NOTE: Remote login here is not just ssh, it may be MySQL remote login, or file sharing query.

6.2 Dual-Machine interconnect

When the two hosts are only two-computer interconnection, both hosts need to set their own IP, while in the other's Hosts file to add their own IP and host name.

7. Modify Host Name

In fact, the hostname of the modification also has a special tool, that is hostname

or set the system ' s host name

Display host Name:

# hostname Zhengcanrui

The host name of this host is Zhengcanrui, and no parameter is used to display the host name of the current host;

Temporarily set host name:

# hostname test100 # hostname Note: Display host name test100

Using the Hostname tool to set the hostname is only temporary, the next time you restart the system, the host name will not exist;

Show host IP:

# hostname-i192.168.1.100

Thanks: Thank you for reading!

Linux Hosts file detailed and configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.