Linux system under the Hosts file detailed and configuration _linux shell

Source: Internet
Author: User
Tags domain name server ssh in domain

Hosts file

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

The Hosts file is the last file in the Linux system responsible for fast resolution of IP addresses and domain names, saved in ASCII format under the/etc/directory. The Hosts file contains a mapping between the IP address and the host name, as well as the host alias. In the absence of a domain name resolution server, all network programs on the system query the file to resolve the IP address corresponding to a host name, or you will need to use the DNS service program to resolve it. By adding common domain names and IP address mappings to the Hosts file, you can quickly and easily access them.

1. Noun analysis

Host Name:

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

Public network:

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

Lan:

Each machine has a host name, which is used to distinguish between hosts and hosts. We can set the hostname for each machine so that it is easy to access each other. For example, we can in the local area network according to each machine function to name it.

Use: For example, we can use the host name, through SSH command remote access to someone else's computer.

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

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

Use:

Hosts file is a Linux system in charge of IP address and domain name Fast resolution of the file,

Store:

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

Content:

The Hosts file contains a mapping between the IP address and host name, and also includes the alias of the hostname.

Role:

In the absence of a domain name server, all network programs on the system query the file to resolve the IP address corresponding to a host name, or you will need to use the DNS service program to resolve it. Commonly used domain names and IP address mappings can be added to the hosts file to achieve fast and convenient access.

3. Priority level

Priority: DNS Cache > Hosts > DNS services

4, configure the Hosts file

This file can configure the host IP and the corresponding host name, for the server type of Linux system its role is not negligible. On the LAN or Internet, each host has an IP address, which separates each host and can communicate according to IP. But the IP address is not convenient memory, so there is a domain name. In a local area network, each machine has a host name that is used to differentiate between hosts for easy access.

The configuration file for the Linux hostname is/etc/hosts; This file tells the host which domain names correspond to those IP, which IP is the host name:

such as the definition in a file

192.168.1.100 linumu100 test100

Assuming that 192.168.1.100 is a Web server, entering http://linumu100 or http://test100 in a Web page opens 192.168.1.100 's Web page.

Typically this file first records the IP and host name of the computer:

127.0.0.1 localhost.localdomain localhost

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 debugging locally, because he mapped to the 127.0.0.1 (that is, the native).

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.localdomain4 127.0.0.1
localhost
192.168.1.195 debian.localdomain Debian

In general, the content of hosts in the host name (Hostname) of the definition, each behavior of a host, each row consists of three parts, each part by a space separated. The line at the beginning of the # number is described (the function of the annotation) and is not interpreted by the system.

Hosts file format:

IP address host name/domain name (host alias)

The first part: Network IP address;

Part II: Host name or domain name;

Part III: host name 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.

Difference between host name (hostname) and domain name (in domain):

Host name is usually used in the LAN, through the Hosts file, the host name is resolved to the corresponding IP;
Domain names are usually used on the Internet, but if this computer does not want to use the domain name resolution on the Internet, then you can change the Hosts file and join your own domain name resolution.

6, the use of the Hosts file

6.1 Troubleshooting remote logins to the Linux host slow problem

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

Note: The remote logins mentioned here are not only ssh, but may also be MySQL remote logins, or file-sharing queries.

6.2 Dual-Machine interconnection

When two hosts are only two-machine interconnection, when both hosts need to set their own IP, at the same time in each other's Hosts file to add their own IP and host name.

7, modify the host name

In fact, the host name of the modification also has a dedicated tool, is hostname

Hostname-show 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;

Set host name temporarily:

# hostname test100
# hostname Note: Display host name
test100

Setting the hostname by the hostname tool is temporary and will not exist the next time the system is restarted;

Show host IP:

# hostname-i
192.168.1.100

The above is a small series to introduce the Linux system under the Hosts file details and configuration, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.