Understand the network configuration of Linux virtual machine in one article

Basics of Computer Networks

In 1969, ARPANET(阿帕网)came online, so1969 is known as the first year of the Internet

Network Classification:

  • Local Area Network (LAN) refers to a computer network composed of interconnected computers in office buildings or campuses within a range of several hundred meters to more than ten kilometers.
  • The technology adopted by Metropolitan Area Network (MAN) is basically similar to that of LAN, but the scale is larger. The metropolitan area network can cover not only several office buildings not far away, but also a city.
  • A Wide Area Network (WAN) usually spans a large physical range, such as a country.

In addition to the above divisions,Networks can also be organized by ownerDivided into public network and private network are two Internet access methods. Public network access mode: The IP address obtained by the computer on the Internet is on the Internet, 非保留地址and the computer on the public network and other computers on the Internet can visit each other at will. Private networks are the opposite.

Non-reserved addresses are addresses that are not on the LAN. For example, the 192.168.xx we often see... This is a reserved address. May be similar in different places.

Our server is using the public network. If it is file transfer or mutual access between parts, it will use the private network. Because ip addresses are limited after all, we can use public and private networks together to maximize the usage of ip addresses.

next we talkip address:

IPIt is the abbreviation of Internet Protocol in English, which means "interconnection protocol between networks", that is, a protocol designed for computer networks to communicate with each other.

IP address types are divided into:

  • public address
  • private address

公有地址(It can also be called the external network address) The
public address (Public address) is Inter NIC(Internet Network Information Center因特网信息中心)in charge of. These IP addresses are assigned to organizations that register and apply to the Inter NIC. It provides direct access to the Internet.

私有地址
Private address (Private address) is a non-registered address, specially for internal use of the organization. The reserved internal private addresses are listed below:

  • Class A 10.0.0.0–10.255.255.255
  • Class B 172.16.0.0–172.31.255.255
  • Class C 192.168.0.0–192.168.255.255

The role of private addresses is to disperse our computers so that many computers canshare a public address

But sometimes two computers under the same LAN have different public IP addresses. This is because:

  • The public network egress of the router does not specify a public network IP, but is bound to an address pool, and the public network IP is randomly obtained
  • One or both devices are NATed on the router, and the converted public IPs are different.

It is not difficult for us to infer that the number of hosts that can be accommodated by the 10-segment private address is the largest. The public WIFI of this Class A Mobile Unicom may be used a lot because there are many users. If we use traffic, we can find that our collection ip may be this 10-segment.

The following are types A, B, and C of public addresses:

insert image description here

Next let's look at an example:

We visit various websites every day, such as Taobao, Baidu and so on. I can't help thinking, how do our devices connect to these URLs? To figure out this problem, we must first figure out the connection between the internal network ip and the external network ip.

Suppose we want to visit Baidu. If we are using the campus network, first we need to convert our internal network ip to the external network ip of the campus network through the router of the campus network. Then connect to the gateway of China Telecom through this external network ip, and finally connect to the gateway of Baidu. Baidu returns the information you requested to your campus network gateway, and the campus network gateway then sends the information to you (the entire network is in a mesh structure, it will automatically find a path to Baidu - based on depth-first search or breadth-first search priority search).

This process is similar to online shopping. For example, if you order a book at school, Taobao will start shipping it to you when it receives your order and prepares the items. He found that your delivery address is in Hunan, so it may start from Hangzhou, go to the transit station in Fujian first, and then to the transit station in Jiangxi, and suddenly finds that the transit station from Jiangxi to Hunan is blocked, so it can only go around to The transfer station in Guangdong, and finally to the transfer station in Hunan. These transit stations are equivalent to various gateways on the public network. When you arrive at the Hunan transfer station, the courier brother will deliver the package to your school gate (this is the last level of gateway). At this time, the courier brother left, and the management staff at the school gate took the package to you according to your dormitory information. (The information exchange within the LAN is handled by the gateway of the campus network).

be careful:

  1. The external network ip is unique worldwide, while the internal network ip is unique only within the LAN. Moreover, the intranet IPs of all computers in a local area network are different from each other, but share one external network IP (this is not absolute, as we said before). As we said earlier, the name of your school is only one in the whole world, but the classroom No. 3 on the 3rd floor of Building A in your school is unique only within your campus. Other schools also have Classroom 3 on the 3rd floor of Building A. You can only tell the courier brother, please help me deliver the package to xx University, but you cannot say please help me deliver the package to Room 3, Floor 3, Building A.

  2. In the local area network, each computer can assign its own IP, but this IP is only valid in the local area network. And if you connect your computer to the Internet, your network provider's server will assign you an IP address, which is your IP on the external network. Two IPs exist at the same time, one internal and one external.

  3. The IP (i.e. external network IP) addresses on the Internet are uniformly managed by an organization called "IANA" (Internet Assigned Numbers Authority, Internet Assigned Numbers Authority). Due to the unreasonable allocation and the limitations of the IPv4 protocol itself, the IP address resources of the Internet are becoming more and more tense. IANA reserves part of class A, B, and C IP addresses for local area network use.

Next, let's look at a few network-related hardware

First of all, the first one is the network card:

insert image description here
A network card is a network component that belongs to the hardware category and is mainly responsible for encapsulating and decapsulating data between computers.

A computer that wants to communicate with other computers (that is, transmit data) must have a network card. The network card will pack the data (the packet capture we usually say is the package of the network card here), and then transmit the data. The transmission medium is wireless or wired (for example, optical fiber is wired). When it is transmitted to another computer, the network card on its body will decapsulate the data packet, and the obtained data will be passed to the operating system, and then handed over to the corresponding software.

A concept related to the network card we also need to know is—MAC地址

MAC address:The physical address of the network card, the serial number of the network card device, which is globally unique by default (hexadecimal).
insert image description here
Differences from IP addresses:

  • Different lengths. The IP address is 32 bits ( we saw the decimal ip address earlier, it should be binary ), and the MAC address is 48 bits.
  • Allocation is based on different.
  • Network addressing is done differently. OSI reference model, ip address is based on the third layer of work (network layer), mac address is the second layer (data link layer)

Then let's seecable:

insert image description here

A network cable is essential for connecting to a LAN. Common network cables in LAN mainly include twisted pair (RJ45 interface), copper axis cable, and optical cable.

In terms of the width of the transmitted data: (from large to small)

  1. optical fiber
  2. copper cable
  3. twisted pair

In terms of data transmission loss: (from small to large)

  1. optical fiber
  2. copper cable
  3. twisted pair

Let's see nextswitch:
insert image description here
Switch (Switch) means "switch", which is a network device used for electrical (optical) signal forwarding. The switch can provide any two network nodes connected to the switch.Exclusiveelectrical signal path.

The main function of the switch is to send the data packets to the correct location.
insert image description here
It is equivalent to the postman. According to the target mac address in the data packet, find its corresponding physical port.
insert image description here
A switch has many ports, and they all have their own numbers. The network card of the computer is connected to the switch through a network cable (or other methods) On the network port, this port is a definite physical address. As long as we know which port the mac address of a certain network card is on, we can send the data packet to him correctly.
There is a mapping relationship table between ports and mac addresses in the switch: the
insert image description here
switch maintains this mapping relationship table. When you want to communicate with a certain mac address, you only need to inquire which port the mac address is on, and then connect to the corresponding port. Just send it out.
insert image description here
Each data packet has two mac addresses, one is the sender's mac called the source mac, and the other is the receiver's mac address called the receiving mac. After the switch receives a packet of data, it first needs to bind the source mac of the packet of data to the receiving port, and then the switch needs to search according to the target mac, and from which port the data packet is sent out. At this time, two situations will occur:

  • If the associated port is queried in the mac address table, it will be sent directly from the associated port
  • If not found. Then broadcast to all ports except the receiving port. This behavior is called flooding.

If the target mac address is in this network, it must be able to receive the mass-sent data packets. After running for a period of time in this way, all the network card devices in the network can be found through the mac data table of the switch.
From this we know that the switch only cares about the mac address in the network, not the ip address

The predecessor of the switch is 集线器:

The early days of the internet. A dormitory has only one network port, so how can four or five people or even seven or eight friends play StarCraft, CS, Warcraft, download AVI files, copy papers, etc.? The easiest way is to use thisHub, also called HUB(It can be said that he is a layer of switches). Simply put, it is equivalent to dividing one network cable into multiple ones, and allowing the devices connected by these network cables to form a local area network. But the hub has many shortcomings, it works on the first layer of the OSI reference model, that is, the physical layer.

The hub adopts the working method of sharing bandwidth. The more devices there are, the more likely it will cause network congestion. If the boss and the second transfer file occupy the bandwidth, the network experience of other small partners will be poor.

Furthermore, the data transmission of the hub is in non-duplex transmission mode. What concept? You can imagine a single-plank bridge, where only one person can cross at a time. If people on both sides want to cross the bridge, you have to wait for one person to pass first, and then the other, which is very inefficient.

Compared with the way the hub broadcasts data, the switch (layer 2 switch) will send the data directly to the destination address according to the address table in the memory. If not, it will be searched by broadcasting, and after the "handshake" is successful, it will be stored in the address table for easy search and use next time. If you can't find it, put it in the cache first and look for it slowly. In addition, the hub is a shared bandwidth, and the device on each port of the switch can exclusively enjoy all the bandwidth. That is to say, if the boss transfers files to the second child, it takes up 100M bandwidth, and the third transfers files to the fourth child, and can still enjoy 100M bandwidth, which is equivalent to the fact that each of these 8 ports is an independent 100M bandwidth. bandwidth.

Finally, let's look at the router:
insert image description here

A router, also known as a gateway device (Gateway), is used to connect multiple logically separated and relatively independent networks.

Routers have two types of interfaces:

  • LAN port: There can be multiple devices connected to the home network, such as notebooks, mobile phones, and desktops, among which mobile phones and notebooks are devices connected to the router through WIFI:
  • WAN port: only one, used to access the carrier network to connect to the Internet

insert image description here

If you ignore the WAN port of the router and only use the LAN port, the router is actually a switch

insert image description here

What if the WAN port is considered? Here we are going to introduce a concept—网关

So what does the gateway do? Here we must first understand a concept - how is the subnet divided?
We bitwise AND the ip address with the subnet mask. Our commonly used subnet mask is 255.255.255.0, the first three bytes, that is, the first 24 bits are all 1, and the last 8 bits are all 0. So the result of the bitwise AND must be that the first three bytes of the ip address remain unchanged and the last byte is 0:
insert image description here
We consider the two IPs with the same result after comparing the ip address and the subnet mask to be in the same subnet
That is to say, in the following case, the two ip addresses must be in the same subnet:
insert image description here
because the subnet masks are all consecutive 1s and consecutive 0s, we usually use the number of 1s to represent the subnet mask.
insert image description here
We generally use IP/mask to represent the subnet. For example 192.168.1.0/24, there are 255 ip addresses in the subnet in the above figure.
If we want to expand the number of ip addresses in the subnet, we only need to reduce the subnet mask
if We want to reduce the number of ip addresses in the subnet we just need to increase the subnet mask:
insert image description here

After knowing the concept of our subnet, what is the significance of our subnet?
The tcp/ip protocol stipulates that different subnets cannot communicate directly. If communication is required, it needs to be forwarded through a gateway.

There are two network cards on the gateway configured with ip addresses belonging to the two subnets, and data packets can be forwarded between the two networks, so we have a network connecting the two subnets:

insert image description here

Let's take a simple example:
when computer a in subnet 1 sends a data packet, first computer a will check whether the target ip belongs to the same subnet as itself:

  • If it is the same subnet, send it directly from the network card
  • If it is not the same subnet, you need to change the destination MAC address of the data packet to the gateway MAC address, and then send it to the gateway

insert image description here
After the gateway gets the packet of data, it finds out that the packet of data belongs to subnet 2 through the routing table, and the gateway will complete the following two actions at this time:

  • Modify the target MAC address to the MAC address of computer b
  • Modify the source MAC to your own MAC

Then send it from the network card of subnet 2.

The behavior of judging how to send data packets based on the target multiple times above is called routing.

The router has a WAN port to connect to the Internet, and multiple LAN ports to connect to the local network. They belong to two different subnets, so accessing the Internet from the intranet is a cross-network behavior. At this time, the router is required to act as the gateway. Role, his behavior is called routing:
insert image description here

network connection mode

My common network connection modes are as follows:

  • Host-Only (host-only mode)
  • Bridged (bridged mode)
  • NAT (address translation mode)

Open the vmware virtual machine, we can see VMnet0 (bridge mode), VMnet1 (host-only mode), VMnet8 (NAT mode) in the "virtual network editor" under "Edit" in the option bar, so what are these? What is the effect? In fact, the VMnet0 we see now represents the virtual switch used in bridge mode; VMnet1 represents the virtual switch used in host-only mode; VMnet8 represents the virtual switch used in NAT mode.

At the same time, there are two virtual NICs corresponding to VMware Network Adapter VMnet1 and VMware Network Adapter VMnet8 on the host, which work in host-only mode and NAT mode respectively. In "Network Connection", we can see these two virtual network cards. If these two pieces are uninstalled, you can click "Restore Default Settings" in the "Virtual Network Editor" under "Edit" of vmware to re-install them. Virtual NIC restoration.

insert image description here

My friends must have doubts when they see this, why is there no VMware Network Adapter VMnet0 virtual network card on the real machine? So next, let's take a look at why this is?

Bridged (bridged mode)

What is Bridge Mode? The bridge mode is to use the virtual bridge to communicate between the host network card and the virtual network card of the virtual machine. Under the function of bridging, it is similar to virtualizing the physical host as a switch. All the virtual machines set up by bridging are connected to an interface of this switch, and the physical host is also plugged into this switch, so all the network cards and network cards under the bridge are connected. It is in exchange mode and can be accessed without interfering with each other. In bridge mode, the IP address of the virtual machine needs to be on the same network segment as the host. If networking is required, the gateway and DNS need to be consistent with the host network card. Its network structure is shown in the figure below:

insert image description here
Next, let's do the actual operation, how to set the bridge mode.

First, after installing the system, before starting the system, click "Edit virtual machine settings" to set the network card mode.

insert image description here
Click on "Network Adapters", select "Bridged Mode", and then "OK"

insert image description here

Before entering the system, let's confirm the host's ip address, gateway, DNS and other information.

insert image description here
Then, enter the system to edit the network card configuration file, the command isvi /etc/sysconfig/network-scripts/ifcfg-eth0

insert image description here
Add the following:

insert image description here
After editing, save and exit, then restart the network card of the virtual machine, and use the ping command to ping the external network ip to test whether it can be connected to the Internet.

Problems that may be encountered after changing the IP address:

  • The physical machine can ping the virtual machine, but the virtual machine can’t ping the physical machine, usually because of the firewall problem of the physical machine, just turn off the firewall
  • The virtual machine can ping the physical machine, but the virtual machine cannot ping the external network, usually because there is a problem with the DNS setting
  • Virtual machine Ping www.baidu.com displays information such as unknown domain name, generally check whether the GATEWAY and DNS settings are correct
  • If all the above settings are still not working, you need to close the NetworkManager service
    • systemctl stop NetworkManagerclosure
    • systemctl disable NetworkManagerDisable
      If the inspection finds that systemctl status networkthere is a problem that needs to be checkedifcfg-ens33

insert image description here
The external network ip can be pinged, which proves that the bridge mode setting is successful.

The bridge mode is easy to configure, but if your network environment is short of ip resources or the ip management is strict, then the bridge mode is not suitable. If this is the case, how can we solve it? Next, let's get to know another network mode of vmware: NAT mode.

NAT (address translation mode)

As we just said, if your network ip resources are in short supply, but you want your virtual machine to be able to connect to the Internet, then NAT mode is the best choice. The NAT mode uses virtual NAT devices and virtual DHCP servers to enable virtual machines to network. Its network structure is shown in the figure below:
insert image description here

In the NAT mode, the host network card is directly connected to the virtual NAT device, and then the virtual NAT device and the virtual DHCP server are connected to the virtual switch VMnet8, thus realizing the virtual machine networking. Then we will find it very strange, why do we need the virtual network card VMware Network Adapter VMnet8? It turns out that our VMware Network Adapter VMnet8 virtual network card is mainly to realize the communication between the host and the virtual machine. We can verify this in a later setup step.

First, set the NAT mode option in the virtual machine, open vmware, click "Virtual Network Editor" under "Edit", and set the NAT parameters and DHCP parameters.
insert image description here

insert image description here

insert image description here

Change the network connection mode of the virtual machine to NAT mode, and click "Edit virtual machine settings".
insert image description here
Click "Network Adapter" and select "NAT Mode"

insert image description here
Then start the system, edit the network card configuration file, the command isvi /etc/sysconfig/network-scripts/ifcfg-eth0

insert image description here

The specific configuration is as follows:
insert image description here
After editing, save and exit, then restart the virtual machine network card, dynamically obtain the ip address, use the ping command to ping the external network ip, and test whether it can be connected to the Internet.

insert image description here

Before, we talked about the role of the VMware Network Adapter VMnet8 virtual network card, so let's test it now.

Disable the VMware Network Adapter VMnet8 virtual network card, and then ping the external network from the virtual machine to see if it can be pinged?

insert image description here

insert image description here

From this point of view, it is true that the virtual machine can connect to the external network through the VMware Network Adapter VMnet8 virtual network card, so why do we need this virtual network card?

We said before that the role of VMware Network Adapter VMnet8 is the communication between the host and the virtual machine. Next, we will use the remote connection tool to test it.

insert image description here

Then, after enabling VMware Network Adapter VMnet8, I found that the remote tool can connect to the virtual machine.

Then, this is the NAT mode, which uses a virtual NAT device and a virtual DHCP server to connect the virtual machine to the external network, and the VMware Network Adapter VMnet8 virtual network card is used to communicate with the virtual machine.

Host-Only (host-only mode)

The Host-Only mode is actually the NAT mode that removes the virtual NAT device, and then uses the VMware Network Adapter VMnet1 virtual network card to connect to the VMnet1 virtual switch to communicate with the virtual machine. The Host-Only mode separates the virtual machine from the external network, making the virtual machine a An independent system that only communicates with the host computer. Its network structure is shown in the following figure:
insert image description here
From the above figure, we can find that if we want to enable the virtual machine to be networked, we can share the host network card with the VMware Network Adapter VMnet1 network card, so as to achieve the purpose of virtual machine networking. Next, let's test it.

First set the "virtual network editor", you can set the initial range of DHCP.

insert image description here

Set the virtual machine to Host-Only mode.
insert image description here
Start the system, and then set the network card file.
insert image description here
Save and exit, then restart the network card, and use the remote tool to test whether it can communicate with the host.

insert image description here
insert image description here

The host and the virtual machine can communicate. Now set the virtual machine to connect to the external network.

insert image description here
We can see that there is a prompt in the above picture, forcing the ip of VMware Network Adapter VMnet1 to be set to 192.168.137.1, then we will modify the DHCP subnet and start address of the virtual machine, click "Virtual Network editor"

insert image description here

Reconfigure the network card and use the VMware Network Adapter VMnet1 virtual network card as the route of the virtual machine.

insert image description here

Restart the network card, and then use the remote tool to test whether it can connect to the external network and communicate with the host.

insert image description here
The test results prove that the virtual machine can be connected to the external network.

Linux common network related commands

ifconfig: configure network interface

ifconfig :network interfaces configuring network interface configuration

Basic syntax:

ifconfig (功能描述:显示所有网络接口的配置信息)

ping: Test the network connectivity between hosts

Grammar format:

ping 目的主机 (功能描述:测试当前服务器是否可以连接目的主机)

This command can be used across platforms, and can also be used under windows, with the same syntax. (The difference is that it is always sent by default under Linux, and 4 packets are sent by default under Windows)

modify ip address

1) Check the IP configuration file :

[root@hadoop100 桌面]#vim /etc/sysconfig/network-scripts/ifcfg-ens33

insert image description here
The items marked in red below must be modified. If there is a value, it should be modified according to the value below. If there is no such item, it should be added.

TYPE="Ethernet" #网络类型(通常是 Ethemet)
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static" #IP 的配置方法[none|static|bootp|dhcp](引导时不 使用协议|静态分配 IP|BOOTP 协议|DHCP 协议)
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="e83804c1-3257-4584-81bb-660665ac22f6" #随机 id
DEVICE="ens33" #接口名(设备,网卡)
ONBOOT="yes" #系统启动的时候网络接口是否有效(yes/no)
#IP 地址
IPADDR=192.168.1.100
#网关
GATEWAY=192.168.1.2
#域名解析器
DNS1=192.168.1.2

After modification, as shown in the figure below:
insert image description here
After editing, press esc on the keyboard, then enter: wq and press Enter

2) Execute service network restart to restart the network :
insert image description here

Problems that may be encountered after changing the IP address:

  • The physical machine can ping the virtual machine, but the virtual machine can’t ping the physical machine, usually because of the firewall problem of the physical machine, just turn off the firewall
  • The virtual machine can ping the physical machine, but the virtual machine cannot ping the external network, usually because there is a problem with the DNS setting
  • Virtual machine Ping www.baidu.com displays information such as unknown domain name, generally check whether the GATEWAY and DNS settings are correct
  • If all the above settings are still not working, you need to close the NetworkManager service
    • systemctl stop NetworkManagerclosure
    • systemctl disable NetworkManagerDisable
      If the inspection finds that systemctl status networkthere is a problem that needs to be checkedifcfg-ens33

hostname: hostname

Basic syntax:

hostname (功能描述:查看当前服务器的主机名称)

Case practice:
(1) View the current server host name

[root@hadoop100 桌面]# hostname (2)如果感觉此主机名不合适,我们

Can be modified. By editing the /etc/hostnamefile

[root@hadoop100 桌面]# vi /etc/hostname 

After the modification is complete, restart to take effect.

netstat: view network connection information

Grammar format:

netstat  -tnlp		(-t:tcp协议,-n:将字母转化成数字,-l:列出状态为监听,-p:显示进程相关信息)
netstat  -an		(-a:表示全部,-n:将字母转化为数字)

Parameter explanation:

  • -t: tcp protocol
  • -n: Convert letters to numbers
  • -l: List the status as listening
  • -p: Display process related information
  • -a: means all

The TCP/IP protocol needs to use this command

traceroute: Find all gateways between the current host and the target host

The router will send icmp packets to each router along the way, and the router may not respond

This command is not a built-in command and needs to be installed

Grammar format:

traceroute  主机地址

insert image description here
Similar to viewing express's traceroute:
insert image description here

Extension: There is a similar command under windows: tracert host address
insert image description here

arp: address resolution protocol

Address Resolution Protocol, or ARP (Address Resolution Protocol), is a protocol for obtaining (MAC) physical addresses based on IP addresses.

insert image description here
When a host sends data, first check whether there is the MAC address of the target host in the local MAC address cache, and use the result in the cache if there is; if not, the ARP protocol will send a broadcast packet, which requires querying the target The MAC address corresponding to the IP address of the host, the host with the IP address will send a response, which includes the MAC address of the target host, so that the sender can get the MAC address of the target host. If the target host is not in the local subnet, the MAC address resolved by ARP is the MAC address of the default gateway.

Common syntax:

arp -a		查看本地缓存mac表
arp -d 主机地址			删除指定的缓存记录

insert image description here

This command is also applicable under windows.

tcpdump: capture packets, capture data tables

Common syntax:

tcpdump 协议 port 端口
tcpdump 协议 port 端口 host 地址
tcpdump -i 网卡设备名

View the data packets on port 22 (ssh):
insert image description here
Interpretation of the data format:

  • 00:09:17.xxxx : The hour, minute, and second of monitoring data
  • IP: the type of protocol used
  • 192.168.21.1: one direction of the packet (from)
  • >: flow of data
  • 192.168.21.136: The other direction of the packet (arrival)

Attachment: How Computers Communicate

In life, we want to write a letter to a person. In addition to the content of the letter, we also need to fill in the envelope. The envelope has two important information:

  • Shipping Address
  • recipient

The delivery address indicates where the letter is to be sent, and the addressee indicates who the letter is to be sent to.

The same reason in the online world, we want to send information to others. You also need to know the recipient address and recipient, which are the MAC address and IP address:

  • MAC address —> recipient
  • IP address —> recipient address

We know that MAC address is globally unique, so why don't we use it for communication?

  • First of all, the MAC address is modifiable
  • Furthermore, for example, if you order a courier in Sichuan, your name and phone number are the mac address, and the place where you live is the ip address. If you only know your mac address, there is no way to deliver the item to you. The biggest function of the network is to realize resource sharing and data transmission. The mac address can help small-scale data transmission. When the network scale expands to tens of millions, you need the ip address to locate your location.

In a word, the Mac address indicates who I am, and the IP address indicates my location in the network.

To send information in the computer network, we only need to fill in the content and the IP address of the other party, and the operating system will automatically query the arp table according to the target IP to obtain the MAC address of the other party to complete the letter, and then send it from the network card:
insert image description here

After our network card is connected to the network, if we want to communicate, we need to configure another IP address for him, but there is no process of configuring an IP address in our life, so why can it be used?

This is because there is a DHCP protocol that automatically configures for us

When the computer is plugged into the Internet cable or the mobile phone is connected to WIFI, the network protocol stack of the operating system will automatically send a packet of DHCP request to the outside, requesting to assign an IP address to it. After the router obtains the DHCP request, it will assign an IP address to it, and pass The DHCP reply message is sent back, and the operating system configures the assigned IP address to the network card after receiving the DHCP reply message:
insert image description here

Note that IP addresses are unique within a LAN. The router will not assign duplicate IP addresses to different devices

Of course, we can also manually turn off DHCP and manually configure the IP address of a network card.

We mentioned above that the MAC address is filled by the operating system, so how does the operating system know the MAC address of the other party?

In fact, this step is done using the arp protocol

When computer a wants to send a message to computer b, the operating system will not send it immediately. It will first send a packet of arp broadcast message to ask what is the target MAC address. At this time, all devices in the network have received the message. This packet of request message, except the target device, other devices will discard this packet of request message, only the target device will reply what is its own MAC address, computer a receives the reply and knows the MAC address of computer b, he First, the MAC address of computer b will be cached for the next use, and then the letter will be completed and sent from the network card. The switch finds the port where computer b is located according to the destination MAC address in the data packet, and the data is sent out from this port, and the data is received by computer b

Guess you like

Origin blog.csdn.net/zyb18507175502/article/details/127446001