Detailed explanation of hosts file under Linux

1. Host name:

 

No matter on the local area network or the Internet, each host has an IP address, which is to distinguish this host from the other host, that is to say, the IP address is the house number of the host.

 

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

 

Local area network: Each machine has a host name, which is used to distinguish between the host and the host. The host name can be set for each machine to facilitate mutual access in an easy-to-remember method. For example, we can name each machine according to its function in the local area network.

 

Hostname related configuration files: /etc/hosts 

 

2. Hostname configuration file:

 

The configuration file for the host name is mostly /etc/hosts 

 

The hosts configuration file is the method used to map host names to IP addresses. This method is relatively simple. But this mapping is only the mapping of the local machine, that is to say, each machine is independent, and all computers cannot access each other through Hostname.

 

E.g:

 

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

 

127.0.0.1   localhost.localdomain localhost

 

192.168.1.195  debian.localdomain debian

 

Under normal circumstances, the content of hosts is about the definition of the host name (Hostname), each line is a host, and each line consists of three parts, each part is separated by a space. The lines starting with # are used for description and are not interpreted by the system.

 

The first part: the network IP address.

 

The second part: hostname.domain name, note that there is a half-width dot between the hostname and the domain name.

 

The second part: hostname (hostname alias), which is actually the hostname.

 

Of course, each line can also have two parts, that is, the host IP address and host name; for example, 192.168.1.195 debian

 

Then we have three hosts, each of which does different things, one for the MAIL server, one for the FTP server, and one for the SMB server, so we can design the Hostname in this way;

 

127.0.0.1 localhost.localdomain localhost

 

192.168.1.2 ftp.localdomain ftp

 

192.168.1.3 mail.localdomain mail

 

192.168.1.4 smb.localdomin smb

 

Write the content of the above configuration file into the /etc/hosts content of each machine, so that the three LAN machines can be accessed through Hostname.

 

3. The difference between hostname and domain name

 

The host name is the name of the machine itself, and the domain name is used to resolve to the IP. But it is worth mentioning that in the local area network, the host name can also be resolved to the IP; such as the example we mentioned earlier;

 

 Show hostname:

 

# Hostname

 

root1

 

The host name of this host is root1, and it is used to display the host name of the current operating host without parameters.

 

Temporarily set the hostname:

 

We can use Hostname followed by the hostname, so that we can set the hostname of the host currently operating, for example, we want to set the hostname to root2;

 

# Hostname root2

 

root2

 

Setting the hostname through the Hostname tool is only temporary, and the hostname will not exist when the system is restarted next time; so if you want to modify the hostname, if you want it to be valid all the time, you should use the above-mentioned modification of the hostname configuration file /etc/hosts ;

 

Show host IP:

 

[root@Linuxsir01 ~]# Hostname -i

 

192.168.10.3

 

Reprinted from: http://www.cnblogs.com/xiaoit/p/3989026.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327032714&siteId=291194637