r/AskComputerScience • u/Able_Standard9937 • Apr 24 '25
Can anyone explain how ip address is assigned to a device in detail
Now I am learning networks , here I have a doubt like how IP is assigned to a device ,I got answer like using DHCP protocol / manual configuration but how that works
11
Upvotes
14
u/ghjm MSCS, CS Pro (20+) Apr 24 '25
If the device is statically configured, then it doesn't need to talk to the network at all to know its own IP address. It just retrieves it from some internal storage location, like a file on disk.
If the device is configured to use DHCP, then when it boots up, it sends a broadcast message called DHCPDISCOVER. This message is seen by a DHCP server on the network, which picks an IP address from its available pool and replies with a DHCPOFFER. In the original DHCP protocol, this was also sent by broadcast, because the client device doesn't have an IP address yet, but in modern DHCP it's usually sent by layer 2 unicast to the client's MAC address. If the client decides to use the offered address (which it might not, particularly if there are multiple offers received), it sends a DHCPREQUEST, and finally the server sends a DHCPACK.
Part of the content of these messages is a "lease time," the amount of time the client is allowed to use the IP address. A typical value is a week, but it could be as little as a few minutes or as long as a year or more. When the client gets close to its expiration time, it sends another DHCPREQUEST, asking for the existing lease to be extended. The server typically replies with a DCHPACK, but in rare cases when there's some reason the client cannot retain its current address, the server might reply with a DHCPNAK (negative acknowledgement). In this case the client must start the process over again, sending a new DHCPDISCOVER.
5
u/MasterGeekMX BSCS Apr 24 '25
Manual configuration is as it sounds: you go into the settings of the network device and tell it explicitly which IP addess it should use. Whether that is a valid IP address on the network or an alreay used one is another topic.
DHCP works by running a server program in some computer on the network (usually the router, but can be anyone). Remember MAC addresses? well, the address with only ones (FF:FF:FF:FF:FF:FF:FF:FF) is reserved to be the broadcast address, meaning that every message sent to that "device" is in fact sent to everyone on the local network.
When a device running a DHCP client asks for an IP address, it shouts into the broadcast MAC address "who's here giving IP addresses?". The computer running the DHCP server catches that message, then looks up on it's configuration which IP addresses is allowed to lease to clients, and selects one. Then it answers back to the client "hey, it is me!. Here is an IP address you could use for this amount of time". The client then shouts back into the broadcast address "hey, DHCP server gave me this IP address. Anyone is using it?". If no oner answers back, then the client know that IP address is available, and starts using it. If someone answers to that call, then the client asks back to the DHCP server for another IP addres. Rinse and repeat until an available IP address is assigned.
1
u/aagee Apr 24 '25
Here is one level of detail, which maybe a good place to start discovering more.
TCP/IP componentin the stack. Linux will let you set it using the commandsiporifconfig. But both Windows and Linux have GUI and command line versions of this. Underneath, they all make the samesystem callsto set the IP address of an interface.You can dig into more details by using the context provided above. Or come back with more questions.
-1
u/cbf1232 Apr 24 '25
this is exactly the sort of thing that you can ask chatgpt or gemini.