Linux - (hostname) mapping between hostname and ip

1. What is a hostname

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. But the IP address is 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. Domain name type linuxsir.org like this;

What is the hostname used for?

A: In a local area network, each machine has a host name, which is used to distinguish the host from 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. Temporarily modify the host name

The #hostname xxx command can temporarily modify the machine name, but the original value will be restored after the machine is restarted.

#hostname View hostname

#hostname -i View the ip address corresponding to the machine name

 

3. The modification of the host name and the description of the configuration file for mapping with ip

The file vim /etc/hosts is where we usually configure the hostname and the mapping between the hostname and ip.

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.

  1. The first part: network IP address;
  2. The second part: hostname.domain name, note that there is a half-width dot between the hostname and the domain name, such as mast.org;
  3. The second part: hostname (hostname alias), in fact, this is the real hostname;

For example my configuration is as follows:

 

127.0.0.1   localhost localhost.localdomain
192.168.238.133 Mast01 mast01

 instruction manual:

 

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

 

127.0.0.1 is the loopback address. For example, if we don't want other machines on the LAN to see the network program we are testing, we can use the loopback address to test. 

Why do you need to define the mapping between ip and hostname?

Answer: In fact, the understanding is also simple. For example, 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.

 

Tips:

There are two places to configure the host name: /etc/hosts and /etc/sysconfig/network, but when configuring, I usually only modify the hosts file;

Let the default host name in /etc/sysconfig/network be used as the loopback address, which is the phrase in hosts. Experienced people use it like this. We up-and-comers should follow the mainstream!

127.0.0.1   localhost.localdomain localhost

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326356253&siteId=291194637