14

Can a computer running a DHCP server assign its own IP address, or does it have to be set statically? If this is a question which depends on the DHCP software, then my configuration is a Debian server running PiHole.

  • 1
  • 1
    If it is possible (e.g. using APIPA), it would normally not make any sense: If the DHCP server does not use random data when assigning IP addresses, it would always assign itself the same IP address via DHCP... – Martin Rosenau Jan 13 at 18:51
  • 2
    What's the difference between these two things? When you have a statically-configured IP address, you are assigning yourself that address. You aren't asking anyone else your address but are using one that is locally determined. – David Schwartz Jan 14 at 7:09
56

The DHCP server must have a static IP - this is because of a chicken-egg problem:

  • During the DHCP process, the server communicates its IP address to the client
  • If the server is also the client, this step can't succeed (as there is not yet an address to communicate).
  • 3
    @Tvde1 - I said "the server communicates its IP address", for me this would express, that we are talking of the server address – Eugen Rieck Jan 13 at 9:35
  • 3
    The server is free to use a different address per interface when serving multiple interfaces. So a connection from the local machine over the loopback interface could use 127.0.0.1. Not that it would be a good idea, and I'm not sure if the response would have meaning (since the request was over loopback, the response should only apply to loopback), but I'm sure something could be made to work. – Joel Coehoorn Jan 13 at 19:50
  • 1
    @JoelCoehoorn - DHCP replies always apply to the interface on which they are received. So a localhost DHCP procedure could set an alternative IP for loopback, but not for an outward-looking interface. This is why I called it a chicken-egg problem. – Eugen Rieck Jan 13 at 23:46
19

Technically, by the standards this could happen, but no network admin in his right mind will count on it working.

Most operating systems come up with an APIPA IP (169.254.1.0 to 169.254.254.255) when starting up. Therefore, the DHCP server might be able to run and possibly even answer requests.

In theory, if on the server the DHCP client daemon stays running and keeps on trying, it should in time be answered by the DHCP server daemon running on the same server. Then it should be able to set its own proper IP address, while continuing to answer DHCP requests from other computers.

This actually working depends a lot on the implementation of the DHCP client and server software of the operating system of the server computer.

  • If the DNS server was auto-configured with a link-local IP, would it still be able to serve addresses from an RFC1918 subnet? – JW0914 Jan 12 at 22:15
  • 3
    @JW0914 DNS is completely independent from DHCP. It is fairly common to host both services on the same machine, but not a requirement at all. – Tonny Jan 12 at 23:12
  • I meant DHCP =] (am used to using software that combines both DNS and DHCP functionality, like dnsmasq) – JW0914 Jan 12 at 23:53
  • 1
    @JW0914: No reason for it not working. DHCP works by broadcast and MAC addresses, not by IP (Layer 2 not 3). When any client computer broadcasts its own request for any listening DHCP server, it also has a link-local IP and this doesn't block communication. In fact, if it receives an answer by another computer saying this same link-local IP is already taken, it will choose for itself another link-local IP and try again. – harrymc Jan 13 at 7:03
  • This is the correct answer. Your DHCP server will - depending on the software - give itself an IP address and clients will find this DHCP server by broadcasting within the network. Some servers will need an IP address to work correctly, but since the localhost, which is usually 127.0.0.1, is also an IP address, there are workarounds that can be used to skip over this issue. (Source: I worked on router software for two and a half years.) – John Hamilton Jan 13 at 8:52
2

I feel the other answers overlook a couple of things.

Can a computer running a DHCP server assign its own IP address

A DHCP server will give out IPs.

Before continuing - a DHCP server is a program running on a system that may also be running other things (it's not equivalent to a device). A DHCP server follows a process and expects things at certain times and in certain formats, that's described in the DHCP protocol.

A DHCP server isn't responsible for taking an offered IP and changing network configuration on a system - that's the responsibility of a DHCP client.

So the technical answer to this is no, because DHCP servers are not also DHCP clients.

Also: please understand that systems or servers don't really have IP addresses - it's network interfaces on those systems (or the systems servers run on) that do, and many systems have multiple interfaces. Your laptop has a wired Ethernet and wireless LAN interface most likely for example, and each can have its own distinct IP - so saying "What's the IP of my laptop" is really a misnomer - you should say "What's the IP of my wireless network adapter" to be 100% correct.

Now ...

  • Can you run a DHCP client on the same system as a DHCP server

Absolutely.

And in the case of most home routers, this is already happening.

However, the router's DHCP server is listening on the LAN+WLAN interfaces, and the router's DHCP client is listening on the WAN or Internet interface.

This way, your router gets an IP from your ISP, and your router gives out private IPs to hosts on the LAN. Since client and server are listening on different interfaces, they don't interfere with one another.

  • Can you make a DHCP server listen on an interface without an IP? This should work because it listens for broadcast addresses, right?

No. When the DHCP responds to the client, it has to set a source IP in the packet it's sending. IK will get the request from someone, but since the requestor has no IP, there's no source IP to set.

Setting that source IP to broadcast would allow any computer to respond to the DHCP offer and break DHCP.

So I imagine most DHCP server software won't listen on an interface without an IP.

So you can't have a DHCP server assign an IP to itself.

1

Yes, it is possible.

Computer, that is the host, and network address, is not the same. A host may have several physical network interfaces or one physical with a couple of virtual network interfaces.

The DHCP server has to have a static IP address. It is theoretically possible to have the DHCP server running on one interface with a static IP address, and have a DHCP client requesting a dynamic IP address on another interface on the same computer.

Say the host has two interfaces, enp2s0 and enp3s0. enp2s0 gets a static IP address for the DHCP server. A DHCP client configures enp3s0 with a dynamic IP address which it receives from the DHCP server listening on enp2s0 on the same host.

  • 1
    I don't understand why this is useful? – user3728501 Jan 13 at 16:43
  • 6
    Your statements "Yes, it is possible" and "The DHCP server has to have a static IP address" are in conflict with each other. – FKEinternet Jan 13 at 21:35
  • "Computer running XYZ" usually refers to the virtual partition when virtualized systems are involved... – rackandboneman Jan 13 at 22:04
  • @FKEinternet no, they don't conflict. I edited my post for explanation. – rexkogitans Jan 14 at 8:22
  • 3
    Removing three extraneous words, the OP's question is completely unambiguous: "Can ... a DHCP server assign its own IP address?" The answer to that question is quite clearly "no" – FKEinternet Jan 14 at 20:09
0

Probably not. Although it is technically possible to configure a DHCP server to listen on an autoconfigured address (169.254.xxx.xxx), most DHCP server software will fail with the message no subnet declaration for eth0 or something similar and never start handing out IP addresses. So a static IP is pretty much required for DHCP servers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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