Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure hosts file under linux system

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "linux system how to configure hosts files", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to configure hosts files under the linux system" this article.

Hosts file

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

The hosts file is a file responsible for the rapid resolution of ip addresses and domain names on the Linux system, which is saved in the / etc/ directory in ascii format. The hosts file contains the mapping between the ip address and the host name, as well as the alias of the host. In the absence of a domain name resolution server, all network programs on the system resolve the ip address corresponding to a host name by querying the file, otherwise they need to use dns service programs to solve the problem. Through the commonly used domain name and ip address mapping can be added to the hosts file to achieve fast and convenient access.

1. Noun analysis

Hostname:

Whether on the local area network or on the INTERNET, each host has an IP address to distinguish which machine is currently (in fact, the physical address of the machine is used at the bottom), that is to say, the IP address is the house number of a host, which is the only one that identifies this machine.

Public network:

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.

Local area Network:

Each machine has a hostname that distinguishes the host from the host. We can set the hostname for each machine so that we can access each other in an easy way. For example, we can name each machine according to its function in the local area network.

Purpose: for example, we can log on to other people's computers remotely through the host name and through the ssh command.

2. About / etc/host, hostname and IP configuration file

Hosts-The static table lookup for host name (hostname query static table)

Purpose:

Hosts file is a file responsible for fast resolution of IP address and domain name in Linux system.

Storage:

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

Content:

The hosts file contains the mapping between IP addresses and hostnames, as well as aliases for hostnames.

Function:

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 they need to use DNS service programs to solve the problem. Usually, the commonly used domain name and IP address mapping can be added to the hosts file to achieve fast and convenient access.

3. Priority

Priority: dns cache > hosts > dns service

4. Configure the hosts file

This file can configure the host ip and the corresponding hostname, and its role for server-type linux systems can not be ignored. On a local area network or INTERNET, each host has an IP address, which distinguishes each host and can communicate according to the ip. But the IP address is not easy to remember, so there is a domain name. In a local area network, each machine has a hostname, which is used to distinguish between hosts and facilitate mutual access.

The configuration file for Linux hostnames is / etc/hosts;. This file tells the host which domain names correspond to those ip and which hostnames correspond to which 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, typing http://linumu100 or http://test100 in the page will open the page of 192.168.1.100.

Typically, this file first records the ip and hostname of the machine:

127.0.0.1 localhost.localdomain localhost

This is when we were debugging the web project, we clearly entered localhost in the browser's address bar, he will know that we are local debugging, because he mapped to 127.0.0.1 (that is, native).

5. Description of configuration file format

But this mapping is only a mapping of the local machine, which means that each machine is independent and can only be accessed using the hostname only if it is configured in the hosts file.

In the hosts file, we will see something similar to the following

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4127.0.0.1 localhost.localdomain localhost192.168.1.195 debian.localdomain debian

In general, the content of hosts is about the definition of host name (Hostname). Each behavior is a host, each line is made up of three parts, and each part is separated by spaces. The lines beginning with the # sign (that is, the role of comments) are not explained by the system

Hosts file format:

Ip address hostname / domain name (host alias)

Part I: network IP address

Part II: hostname or domain name

Part III: host name alias

Of course, each line can also be in two parts, the host IP address and the host name; for example, 192.168.1.100 linmu100.

The difference between hostname (hostname) and domain name (Domain):

The hostname is usually used in the local area network. Through the hosts file, the hostname is resolved to the corresponding ip

Domain names are usually used on internet, but if you don't want to use domain name resolution on internet, you can change the hosts file to add your own domain name resolution.

6. The purpose of hosts files

6.1 resolve the problem of slow remote login to linux hosts

Sometimes the client wants to log in to a linux host remotely, but it will wait a long time to enter after entering the password. This is because the linux host needs to resolve the ip when returning information. If the client's ip address is added to the hosts file of the linux host in advance, it will be very fast to log in to linux remotely from the client.

Note: the remote login mentioned here is not only ssh, but also mysql remote login or file sharing query.

6.2 dual-computer interconnection

When two hosts are only connected to each other, both hosts need to set their own ip and add their own ip and hostname to each other's hosts file.

7. Modify the host name

In fact, there is also a special tool for host name modification, that is, hostname.

Hostname-show or set the system's hostname

Show hostname:

# hostnamezhengcanrui

The hostname of this host is zhengcanrui, and it is used to display the hostname of the current host without parameters.

Temporarily set the hostname:

# hostname test100# hostname Note: show hostname test100

Setting the hostname through the hostname tool is only temporary and will not exist the next time the system is rebooted

Display the host IP:

# hostname-i192.168.1.100 and above are all the contents of this article entitled "how to configure hosts files under linux system". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report