Introduction to DHCP Protocol Workflow
Recently read an article introducing the DHCP protocol workflow, here is a brief summary.
How to configure an IP Address for a network interface card
You can configure an address yourself with the command line. You can use ifconfig or ip addr. After setting it up, use these two commands to card up the network interface and start working.
Use net-tools:
1 | sudo ifconfig eth1 10.0.0.1/24 |
Using iproute2:
1 | sudo ip addr add 10.0.0.1/24 dev eth1 |
You may ask, configuring this degree of freedom yourself is too much, can I configure anything?
What if I configure an address that is not related to anyone? For example, the machines next to me are all 192.168.1.x, and I have to configure a 16.158.23.6, what will happen?
There will be no phenomenon, just that the package cannot be sent out.
Why not send it? For example. 192.168.1.6 is next to your machine, even on the same switch, and you set the address of the machine to 16.158.23.6. On this machine, you try to ping 192.168.1.6, and you think that as long as you send the packet, another machine on the same switch will receive it right away, right? But Linux system is not like this, it is not as smart as you think. You see the machine next to you with the naked eye, and it needs to process according to its own logic.
As long as the packet is running on the network, it is complete, there can be no upper layer, absolutely impossible to have no lower layer. So, you look at it has its own source IP address 16.158.23.6, there is also a destination IP address 192.168.1.6, but the packet can not be sent out, this is because the MAC layer has not been filled.
It’s easy to know your own MAC address. But what should the target MAC fill in? Is 192.168.1.6 fill in the MAC address of this machine? Of course not. Linux will first judge, is the address you want to go to the same network segment as me, or is it the same network segment as one of my network interface cards? ** Only if it is a network segment, it will send an ARP request to obtain the MAC address. What if it is not found? Linux default logic is that if this is a cross-segment call, it does not send the packet directly to the network, but attempts to send the packet to the gateway.
In the same local area network, DHCP requests an IP address for its own MAC, and ARP obtains someone else’s MAC address based on someone else’s IP.
If you configure the gateway, Linux will get the MAC address of the gateway and then send the packet out. For 192.168.1.6 machine, although the packet passing by its door, the target IP is it, but the MAC address is not it, so its network interface card will not receive the packet.
What if the gateway is not configured? The package can’t be sent at all.
If the gateway is configured to 192.168.1.6? Impossible, ** Linux won’t let you configure it successfully, because the gateway needs to be on the same network segment as at least one network interface card of the current network. How can 16.158.23.6 gateway be 192.168.1.6? **
So, when you need to manually configure the network IP of a machine, be sure to ask your network administrator. If you are in the computer room, go to the network administrator to apply and ask him to assign you a correct IP address. Of course, when the real configuration is done, it must not be configured directly with commands, but in a configuration file. The configuration file format of different systems is different, but it is nothing more than CIDR, subnet mask, broadcast address and gateway address.
DHCP
Introduction
The above method of statically configuring IP is more troublesome and requires manual configuration, and it is also necessary to know which IPs have not been assigned, and once there is no manual release, even if this IP is no longer used, if it is not manually released, the IP is still It cannot be used by other computers in need.
So we need to have an automatic configuration protocol, that is, Dynamic Host Configuration Protocol (Dynamic Host Configuration Protocol), referred to as DHCP., when a new device accesses, it can automatically assign an IP Address to it, and when it is no longer using the network, it can take back the IP Address.
With this protocol, network administrators were much more relaxed. He only needed to configure a shared IP address. Each newly connected machine would apply for this shared IP address through the DHCP protocol, and then automatically configure it. Wait for someone to leave, or run out, and return it so that other machines can also use it.
So, if it is a server in the Data center, once the IP is configured, it will basically not change, which is equivalent to buying a house and decorating it yourself. The DHCP method is equivalent to renting a house. You don’t need to decorate, it’s all configured for you. You can use it temporarily and return the lease after use.
Workflow
The new machine sends a broadcast packet using the IP address 0.0.0.0, the destination IP address is 255.255.255.255. The broadcast packet encapsulates UDP, and UDP encapsulates BOOTP. In fact, DHCP is an enhanced version of BOOTP, but if you capture packets, you are likely to see the name of the BOOTP protocol.
In this broadcast package, the new bee shouted: I am new (Boot request), my MAC address is this, I don’t have an IP yet, who can rent me an IP address!
When the new machine sends the broadcast packet, it has not been assigned an IP address, so it uses a reserved address, 0.0.0.0, and it does not know DHCP.
In other words, all machines in the network can receive the broadcast packet and respond, which may pose a certain danger to deceive the machine
If a network administrator configures a DHCP Server in the network, he is equivalent to the administrator of these IPs. He can immediately know that a “new bee” has come. At this time, we can appreciate the unique importance of MAC addresses. When a machine joins a network with its own MAC address, the MAC is its unique identity. If even this is repeated, there is no way to configure it. Only when the MAC is unique, the IP administrator can know that this is a new bee and needs to lease an IP address to it. This process is called DHCP Offer. At the same time, the DHCP Server reserves the IP address provided for this customer, so it will not assign this IP address to other DHCP customers.
The DHCP Offer is formatted like this, with addresses assigned to new bees.
The DHCP Server still uses the broadcast address as the destination address because, at this time, the new bee requesting an IP assignment does not have its own IP. The DHCP Server replied, I have assigned an available IP to you, what do you think? In addition, the server also sends information such as subnet mask, gateway, and IP address lease period.
The new machine was very happy. Its “roar” had been answered, and someone was willing to rent it an IP address, which meant that it could gain a foothold on the Internet.
Of course, even more pleasantly, if there are multiple DHCP Servers, this new machine will receive multiple IP addresses, which is simply flattering. It will select one of the DHCP Offers, usually the one that arrives first, and will send a DHCP Request broadcast data packet to the network, which contains the MAC address of the client, the IP address in the accepted lease, and the DHCP server that provided this lease. Address, etc., and tell all DHCP Servers which server it will accept the IP address provided by, tell other DHCP servers, thank you for your acceptance, and request to revoke the IP address they provided for the next IP lease requester.
When the DHCP Server receives the client’s DHCP request, it will broadcast a DHCP ACK message packet back to the client, indicating that it has accepted the client’s choice, and put the legal lease information and other configuration information of this IP address into the broadcast packet, sent to the client, and welcomed it to the network family. When the final lease is reached, it still needs to be broadcast to let everyone know.