[Linux operating system] Detailed explanation of network configuration: from principle to practice (detailed explanation of DNS)

Introduction: Network configuration is an important task in the Linux system. A reasonable network configuration can ensure the normal communication between the computer and other devices. This article will introduce the principles and practices of Linux network configuration in detail, including network configuration principles, viewing network IP and gateways, testing network connectivity, network environment configuration, setting hostname and hosts mapping, and analyzing the hostname resolution process.

1. Principles of network configuration

Network configuration is realized by configuring the network interface, which includes setting parameters such as IP address, subnet mask, and gateway, so that the computer can communicate with other devices.insert image description here

Linux network configuration involves multiple concepts and components, which are explained below:

  • Internet (Internet) : The Internet is a worldwide computer network, which is connected to each other through standardized communication protocols. The Internet connects all different types of computers and networks, enabling them to communicate and exchange data with each other.

  • Local Area Network (LAN) : A LAN is a computer network within a relatively small geographic area, such as within a home, office, or school. A local area network is usually composed of devices such as routers, switches, and computers, which can realize communication and resource sharing between internal computers.

  • Gateway : A gateway is a node in a computer network that connects two different networks and is responsible for transmitting data packets from one network to the other. A gateway is usually a router with multiple interfaces that are connected to different networks, such as a local area network and the Internet.

  • Wireless Network Interface Card : A wireless network interface card is a hardware device used for wireless network connections that allows a computer to communicate with a wireless router or access point through wireless signals. Wireless cards typically communicate via the Wi-Fi standard and can connect to a wireless local area network (WLAN).

  • IP address (Internet Protocol Address) : An IP address is a unique address used to identify and locate a device on the Internet. An IP address is represented by a 32-bit (IPv4) or 128-bit (IPv6) binary number, usually in dotted decimal notation. Every device is assigned a unique IP address when it connects to the network.

  • Subnet Mask (Subnet Mask) : The subnet mask is used to determine the network part and the host part of the IP address. It is a 32-bit binary number that is logically ANDed with the IP address to determine the range of the network. The function of the subnet mask is to divide the IP address into two parts: the network address and the host address.

  • DHCP (Dynamic Host Configuration Protocol) : DHCP is a network protocol used to automatically assign IP addresses, subnet masks, gateways, and other network configuration information to computers. With DHCP, computers can obtain network configuration automatically without manual configuration.

  • DNS (Domain Name System) : DNS is a system in the Internet for converting domain names into IP addresses. The DNS server stores the mapping relationship between domain names and IP addresses. When a user enters a domain name, the computer will send a query request to the DNS server to obtain the IP address corresponding to the domain name.

The connections between these concepts and components are: Computers are connected to a local area network or wireless network through a network interface, and connected to the Internet through a gateway. By configuring the IP address, subnet mask and gateway, the computer can communicate with other devices. The DHCP protocol can automatically assign IP addresses and other network configuration information, and the DNS system can convert domain names into IP addresses to implement domain name resolution on the network.

2. View network IP and gateway

In Linux, we can use the ifconfig command to view the network interface of the current computer and its corresponding IP address. Open a terminal and enter the following command:

ifconfig

insert image description here

This command will list the network interface information of the current computer, including interface name, MAC address, IP address, etc.
 The command for windows computer is:

ipconfig

3. Test network connectivity

Testing network connectivity is an important step in network configuration to determine if the network is working properly. In Linux, we can use the ping command to test network connectivity. Open a terminal and enter the following command:

ping <目标IP地址>

insert image description here

Wherein, the target IP address may be another computer, server or domain name. If the network is normal, you will receive a response to the ping command.

4. Network environment configuration

Network environment configuration includes static IP configuration and dynamic IP configuration. Static IP configuration is to manually set the IP address, subnet mask and gateway. Open the network configuration file, such as /etc/network/interfaces, and add the following configuration information to the file:

auto eth0
iface eth0 inet static
address <IP地址>
netmask <子网掩码>
gateway <网关地址>

Dynamic IP configuration is to automatically obtain an IP address through a DHCP server. Open the network configuration file, such as /etc/network/interfaces, and add the following configuration information to the file:

auto eth0
iface eth0 inet dhcp

5. Set the hostname

The hostname is set for the convenience of identifying and managing the computer.
Use the hostname command to directly view your own hostname:
insert image description here
We can set the hostname by modifying the /etc/hostname file. Open the file, enter the host name you want to set, save the file and restart the computer to make the configuration take effect.
insert image description here

6. hosts mapping

The hosts file is a local domain name resolution file, which is used to map domain names to IP addresses. We can add custom domain name resolution by modifying the /etc/hosts file.

6.1 How to set hosts mapping in Linux system:

  1. Open a terminal or command line interface.

  2. Use a text editor (such as vi, nano, etc.) to open the hosts file with administrator privileges, which is located in /etc/hosts.
    insert image description here

  3. Add the host name to be mapped and the corresponding IP address in the hosts file, one mapping per line, in the format: IP address host name.

  4. Save and close the file.

6.2 How to set hosts mapping in Windows system:

  1. Open Notepad or another text editor.

  2. Open the hosts file with administrator privileges, which is located at C:\Windows\System32\drivers\etc\hosts.

  3. Add the host name to be mapped and the corresponding IP address in the hosts file, one mapping per line, in the format: IP address host name.
    insert image description here

  4. Save and close the file.

7. Analysis of the host name resolution process:

Taking Baidu as an example, the following is the analysis of the host name resolution process:
insert image description here

  1. When the user enters Baidu's domain name (www.baidu.com) in the browser, the browser will first check whether there is a corresponding mapping relationship in the local hosts file.

  2. If the corresponding mapping relationship is found in the hosts file, the browser will resolve the host name to the corresponding IP address, and directly send a request to the IP address.

  3. If no corresponding mapping relationship is found in the hosts file, the browser will send a query request to the local DNS cache.

  4. If the corresponding IP address is found in the local DNS cache, the browser will resolve the host name to the corresponding IP address and send a request directly to the IP address.

  5. If the corresponding IP address is not found in the local DNS cache, the local DNS cache will send a query request to the local DNS server.

  6. The local DNS server will check its own cache, and if it finds the corresponding IP address, it will return the result to the local DNS cache.

  7. If the local DNS server does not find the corresponding IP address, it will query step by step from the root domain name server according to the hierarchical structure of the domain name.

  8. The local DNS server will send a query request to the root domain name server, and the root domain name server will return the IP address of the top-level domain name server.

  9. The local DNS server will send a query request to the top-level domain name server, and the top-level domain name server will return the IP address of the second-level domain name server.

  10. The local DNS server will continue to send query requests to the secondary domain name server until it finds the IP address corresponding to the host name.

  11. Once the local DNS server finds the corresponding IP address, it returns the result to the local DNS cache.

  12. The local DNS cache returns the result to the browser, which resolves the hostname to an IP address, and sends the request to that IP address.

  13. After Baidu server receives the request, it will return the corresponding webpage content to the browser, and the browser will display the webpage content to the user.

In this process, from the user's input of the domain name to the final acquisition of the webpage content, multiple components are involved, such as the local hosts file, the local DNS cache, the local DNS server, the root domain name server, the top-level domain name server, and the second-level domain name server. Through this process, the host name can finally be resolved to the corresponding IP address, realizing the communication between the user and the Baidu server.

A popular example:

Suppose you want to send a letter to your friend, but you only know his name, not his specific address. You need to go through a series of steps to find his address in order to send the letter.

First, you'll look in your address book (similar to your computer's hosts file) to see if your friend's address is there. If so, you can just write the address and send the letter.

If your friend's address isn't in your address book, you'll ask your family or friends (similar to a local DNS cache) to see if they know your friend's address. If they know, they will give you the address and you can write the address and send the letter.

If your family or friends don't know your friend's address either, you'll ask the post office (similar to a local DNS server). You tell the post office your friend's name, and the post office will look up his address for you. If the post office knows his address, they will tell you the address, and you can write the address and send the letter.

If the post office doesn't know your friend's address either, the post office will forward your request to a higher level post office (similar to a root domain name server). More advanced post offices will keep checking until they find your friend's address.

Once you have your friend's address, you can send the letter to him. In this way, through a series of inquiries and forwarding, you finally found your friend's address and successfully sent the letter.

In this example, your friend's address is equivalent to Baidu's IP address, your address book, family and friends are equivalent to the computer's hosts file and local DNS cache, the post office is equivalent to the local DNS server, and more advanced post offices It is equivalent to the root domain name server and the top-level domain name server. The whole process is to find your friend's address so that you can send the letter successfully.

epilogue

This article introduces the principles and practices of Linux network configuration in detail, including network configuration principles, viewing network IP and gateways, testing network connectivity, network environment configuration, setting hostname and hosts mapping, and hostname resolution process analysis.

Guess you like

Origin blog.csdn.net/Goforyouqp/article/details/132056689