The relationship between hostname, domain name and IP address in Linux

table of Contents

1. Comparative understanding

1.1 IP address

1.2 Domain name

1.3 Host name

2. How to configure?

2.1 Set the host name

2.2 Set the correspondence between IP and hostname

3. References


      This article explains in detail the relationship between "host name, domain name, and IP address", and further explains how to set the host name and configure the correspondence between IP and host name.

1. Comparative understanding

1.1   IP address

      Suppose a person is used to illustrate the relationship between them (compare a person to a host), they are like some identities/characteristics of people. IP is equivalent to the ID number given by the country, and it is unique, but when someone calls a person, it will not directly call the ID number. For example, it is too difficult to remember when you call 6688xxxxxxxxx (ID number). With domain names and host names.

1.2 Domain name

      The domain name is equivalent to a person's "specific alias", such as: Zhangshan in xx Village xx County, xx Province, or Zhangshan in xx Class of xx Middle School, etc. This alias is unique, and knowing this alias can be uniquely determined A person. (One domain name corresponds to one IP address, but one IP address can correspond to multiple domain names. The domain name and IP are in a many-to-one relationship. Domain names are usually used on the Internet and usually have a corresponding domain name server for resolution. Of course, we can also pass Local configuration domain name resolution).

1.3 Host name

      The host name is equivalent to a person's "popular alias", such as: Zhang Shan, you can directly call Zhang Shan, or Xiao Zhang, or Xiao Shan, etc., but this alias is generally in a small range of familiar (bureau) people (domain) circle ( In the Internet), the person who is called has his own [ name->person ] correspondence table (ie: /etc/hosts). In the same network, generally there will not be two or more machines with the same host name .

2. How to configure?

2.1 Set the host name

(1) hostname: view the host name;

[root@localhost ~]# hostname
localhost.localdomain
[root@localhost ~]#

(2) hostname new-hostname: temporarily modify the host name, the current terminal will not take effect immediately after setting (because the environment variables of the current terminal have been read before setting), and it is only valid for the current system, and it is invalid after restart;

[root@localhost ~]# hostname  new-hostname
[root@localhost ~]# hostname
new-hostname
[root@localhost ~]# cat /etc/hostname 
localhost.localdomain
[root@localhost ~]#

      Although the hostname is reset by hostname new-hostname, the hostname in the current terminal is still localhost (original), and the content of /etc/hostname has not changed, but use the hostname command to view the display has been changed, and then reopen A terminal displays as follows:

[root@new-hostname ~]# hostname
new-hostname
[root@new-hostname ~]#

The host name display of the newly opened terminal has been changed.

(3) Permanently modify the host name;

    Modify by command: hostnamectl set-hostname hostname-new: After setting, the current terminal will not take effect immediately. The file /etc/hostname will be changed accordingly, /etc/hosts will not be changed, hostname can view the current real hostname;

[root@localhost ~]# hostnamectl set-hostname hostname-new 
[root@localhost ~]# hostname
hostname-new
[root@localhost ~]# cat /etc/hostname 
hostname-new
[root@localhost ~]# 

    Modify through the file: vi /etc/hostname, and then modify it to the new hostname, which will take effect after restarting.

[root@localhost-new ~]# cat /etc/hostname 
localhost
[root@localhost-new ~]# 

After restarting:

[root@localhost ~]# hostname
localhost
[root@localhost ~]#

2.2 Set the correspondence between IP and hostname

    The file /etc/hosts is responsible for setting the correspondence between IP and hostname/domain name. Why do you want to set this correspondence? In fact, this file is equivalent to a local domain name resolver. When using a hostname/domain name to access a host or website , The hostname/domain name will be converted to the corresponding IP, so that there is no need to memorize the complicated IP. At the same time, it also speeds up the resolution of domain names and IPs. For example, if you use www.baidu.com to access Baidu, you need to resolve to the corresponding IP through the domain name resolution server, and then access through the IP. If the IP is set locally Corresponding to the domain name, the resolution speed is of course accelerated.

    The format of /etc/hosts:

    Generally, the file contains multiple lines, each line represents a host, each line contains three parts, and each part is separated by a space.

    The first part: IP address;

    The second part: hostname/domain name;

    The third part: host alias;

    Under normal circumstances, generally use the form of "IP address host name/domain name" to configure to meet the requirements, the host name mentioned here is usually used in the local area network, and the domain name usually refers to the internet.

3. References

[1] https://blog.csdn.net/debimeng/article/details/88614513

[2] https://www.cnblogs.com/xiezh-it/p/9761429.html

[3] https://www.cnblogs.com/yinfutao/p/10620035.html

[4] https://www.cnblogs.com/rinack/p/6861893.html

Guess you like

Origin blog.csdn.net/u011074149/article/details/107924701
Recommended