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?

  • How did this happen? I ask a question on SE and it has a positive number of votes! And that's a lot of upvotes.
    – user96725
    Mar 24, 2019 at 14:31

2 Answers 2


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, 2019 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, 2019 at 21:52
  • 2
    actually two to be precise - assuming pi3b or 3b+ Jan 7, 2019 at 1:04
  • 7
    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, 2019 at 4:27
  • 1
    A quality DHCP implementation will cache previous MAC address & IP assignments. When it sees a cached MAC address, it will tend to offer the same IP (if it is available) again. It will also resist giving the IP to a different MAC address unless it has no free addresses left. Since this relies on the specific implementation ... and quality of home routers is all over the place ... the behavior will not necessarily be consistent depending on your router model. Mar 15, 2019 at 14:27

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 from b8:27:eb:0e:3c:6f (raspi3) via wlan0
(server) DHCPACK on 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 from b8:27:eb:0e:3c:6f (raspi3) via wlan0
(server) DHCPNAK on to b8:27:eb:0e:3c:6f via wlan0
(client) DHCPDISCOVER from b8:27:eb:0e:3c:6f via wlan0
(server) DHCPOFFER on to b8:27:eb:0e:3c:6f via wlan0
(client) DHCPREQUEST for from b8:27:eb:0e:3c:6f (raspi3) via wlan0
(server) DHCPACK on 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 ...

  • 5
    +1 IMHO this is the correct answer. It is simply the way the DHCP protocol works.
    – Jos
    Jan 8, 2019 at 14:36
  • Thanks, it was really helpful. So, DHCPNAK said that I can't give you back because I've gotten this IP address another host, am I right?
    – M. Rostami
    Jan 13, 2020 at 23:13
  • 1
    @M.Rostami Yes, that's right. And the only reason for all of this is to have ip addresses as stable as possible on the network.
    – Ingo
    Jan 13, 2020 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.