Detailed Explanation of linux Server hosts File Configuration

  • 2021-07-24 11:57:31
  • OfStack

linux Server hosts File Configuration

hosts file is a file responsible for fast resolution of IP address and domain name in Linux system, which is stored in "/etc" directory in ASCII format, and the file name is "hosts".

The hosts file contains a mapping between the IP address and the host name, as well as an alias for the host name. In the absence of a domain name server, all network programs on the system resolve the IP address corresponding to a host name by querying the file, otherwise it needs to be solved by using DNS service program. Usually, the commonly used domain name and IP address mapping can be added to the hosts file to achieve fast and convenient access.

Let's introduce the following methods and steps to modify the hosts configuration file:

Tools required: vim/vi

1. Install the vim editor


sudo apt install vim -y # debian

sudo yum install vim -y # readhat

2. Modify the hosts file using the editor

1. sudo vim /etc/hosts

2. Press i to enter edit mode

The format of the hosts file is as follows:

IP Address Hostname/Domain Name

Part 1: Network IP address;

Part 2: Host name or domain name;

Part 3: Host name alias;

For example:


127.0.0.1 localhost.localdomain localhost

192.168.1.100 linmu100.com linmu100

192.168.1.120 ftpserver ftp120

3. Save after modification

Content extension

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 LAN or INTERNET, each host has an IP address, which separates each host and can communicate according to ip. But IP address is inconvenient to remember, so there is a domain name again. In a local area network, each machine has a host name, which is used to distinguish hosts and facilitate mutual access.

The relevant configuration file for the Linux hostname is/etc/hosts; This file tells the host which domain names correspond to those ip and which host names correspond to those ip:

For example, there is such a definition in the file

192.168.1.100 linumu100 test100

Assuming that 192.168. 1.100 is a Web server, entering http://linumu 100 or http://test 100 in the Web page opens the 192.168. 1.100 Web page.

Typically, this file first records the native ip and host name:

127.0.0.1 localhost.localdomain localhost

This is when we debug the web project, when we typed localhost in the address bar of the browser, he will know that we are debugging locally because he maps to 127.0. 0.1 (that is, native).

The above is all the knowledge points introduced this time. Thank you for your study and support for this site.


Related articles: