18

My router uses DHCP, and whenever I reformat my raspberry pi, even when using a different microSD card, it always has the same LAN ip. I'm not quite sure how IPs are assigned, but I assume the device has a unique id on the network which my router recognises?

36

Your Raspberry Pi has a unique ID, called a MAC address, actually two to be precise. Each network adapter has one of those. So the Wi-Fi adapter has one unique ID and the Ethernet adapter does.

And this is why your Raspberry Pi gets the same IP. For your router it's not important what you are doing with the device exactly. It just recognizes the MAC and provides the same IP as before.

Edit: As Beege pointed out, the IP can still change as the 'D' in DHCP stands for dynamic. But if your device gets the same IP as before via DHCP its because of the MAC that's recognized.

  • 9
    Good explanation. I think it's worth mentioning that the same MAC address will not always get the same IP from the DHCP server -- it is still a "dynamic" service, so it's possible the IP gets taken by another device and your Pi ends up with a different IP assignment. – Beege Jan 6 at 21:48
  • 3
    @Beege oh yes, absolutely, thanks! One can choose to reserve a specific IP for the Pi when a permanent IP is desired, for example when running a server on it. But this is to be enabled specifically with the router (or whatever device else provides DHCP service in the network) – ItsKiddow Jan 6 at 21:52
  • 2
    actually two to be precise - assuming pi3b or 3b+ – Jaromanda X Jan 7 at 1:04
  • 6
    The fact that the IP is the same is NOT because of the MAC. The MAC is an identifier that the router has access. The router can be configured to try to give you the same IP when the lease is renewed, or it can randomly assign you a different one. Your router is configured (by the manufacturer) to do the former. This actually prevents a lot of network problems caused by dynamic IP related issues (IP printing ports comes to mind). The ability to obtain the same IP is due to the MAC, but I can easily setup the DHCP on the router to give a pile of different devices the same IP. – Nelson Jan 7 at 4:27
14

In addition to the already given answers I will give some additional background information.

In general the DHCP protocol is made to reduce dynamic changes as much as possible. It is an aspect of stability. It does not matter much on small home networks but big networks with switches and routers need some time to get into an optimized state. Switches have to learn its neighbors and router have to learn the routes. Heavy changes of ip addresses is not good for this state.

Most people may think the DHCP server just give an ip address to a client and that's it. But it is only half the truth. Here is a typical DHCP handshake:

(client) DHCPREQUEST for 192.168.10.75 from b8:27:eb:0e:3c:6f (raspi3) via wlan0
(server) DHCPACK on 192.168.10.75 to b8:27:eb:0e:3c:6f (raspi3) via wlan0

As you can see the client identified with its mac address b8:27:eb:0e:3c:6f requests a specific ip address it prefers. It knows what ip address it has before, also after a new startup. The DHCP server only confirms it. That what it gives to the client is called a lease. It contains a timeout (together with many other important options) how long the client can use the ip address without requesting again. The timeout depends on the setup and is mostly set to some hours. The DHCP server stores the lease in its cache and will reserve it as long as possible for the same client, also if it is shutdown. So it will just confirm the client for its lease when it bootup again. Only when the server does not have other unused leases to give to clients it will take the used one. The handshake then will look like this:

(client) DHCPREQUEST for 192.168.10.75 from b8:27:eb:0e:3c:6f (raspi3) via wlan0
(server) DHCPNAK on 192.168.10.75 to b8:27:eb:0e:3c:6f via wlan0
(client) DHCPDISCOVER from b8:27:eb:0e:3c:6f via wlan0
(server) DHCPOFFER on 192.168.10.99 to b8:27:eb:0e:3c:6f via wlan0
(client) DHCPREQUEST for 192.168.10.99 from b8:27:eb:0e:3c:6f (raspi3) via wlan0
(server) DHCPACK on 192.168.10.99 to b8:27:eb:0e:3c:6f (raspi3) via wlan0

As you can see the DHCP server rejects (DHCPNAK) the request and offers a new ip address that then in the next step is requested by the client. This additional step is made to give the client the possibility to not accept the offered ip address ...

  • 1
    +1 IMHO this is the correct answer. It is simply the way the DHCP protocol works. – Jos Jan 8 at 14:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.