A Raspberry Pi is configured as a fully functional DHCP server using the ISC-DHCP-server package, covering installation, lease time settings, subnet configuration, and interface binding. The setup is verified end-to-end by confirming a Windows client successfully receives an IP address from the defined pool.
Pi as DHCP Server
We're going to set up and configure a Pi so that it can be a DHCP server. I'm going to be installing the DHCP services using command line, so I'm going to open up a terminal window. Once it's open here, I'm going to make it a little bit larger so we can make sure we can see it.
Then I will type in sudo, because we need elevated privileges for this. I'm going to start by just making sure that all the package information is up to date on here, so I'm going to do apt-get update and make sure that it's got all the most recent information that it's working off of. So it's going through the process of reading these packages and making sure it's all up to date.
Next, we'll actually install the DHCP server. We're going to type in sudo, because this is going to be elevated permissions, and then apt-get, and since we're installing, we'll type install. The service that we're going to install is isc-dhcp-server. Then we'll hit enter, and I'll be asked, "Do you want to continue?" Yes, we do.
That took a couple of minutes to install. We also see that it tried to start it but failed. We just need to configure this before it starts up.
The next step is to configure this. To edit these configurations, we'll use nano to edit the config file. We'll use elevated permissions, we'll use nano to edit this, and we want to go d conf, and then we'll get into the file to edit it. I made the terminal window a little bit larger so we can see it.
As I'm scrolling through these options here in this configuration file, the first thing that I run across that I want to talk about is this default lease time. The default lease time says 600, so that is in seconds. If I wanted to figure out how many minutes that was, I could just take 600 divided by 60 seconds in a minute, and so this is for 10 minutes. The default lease time on here is 10 minutes, which is pretty short. And the max lease time is 7,200; divided by 60 is 120.
I've worked on networks that are pretty large, and for all of this communication to be eaten up by those DHCP servers is probably unnecessary. So I like to extend these out a little bit longer, as long as you have enough IP addresses. If you have short lease times, then it will cycle through a little faster, so if machines are getting off of your network constantly, then you can reuse those IP addresses. But then it gets a little bit more chatty on the network, because you have a lot of machines that continue to check in with it. Versus if you have longer lease times, then you'll have IP addresses out there that have been assigned that stay open a little bit longer, which if you're short on IP addresses can be a problem. So you want to kind of vary this depending on what type of equipment you have, whether you have people that are jumping on and off your network consistently, and how many IP addresses you have available to you.
The default lease time is this: if the client doesn't ask for a certain amount of lease time, then it's going to get the default lease time. And the max lease time is the maximum amount of lease time that this server will hand out, given that if a client asks for more time then they could be given more time.
So let's say I want the default lease time to be 12 hours. The reason why I do 12 hours is that it kind of gives a working window during the day of 8 hours plus or minus a few hours on each end. So if somebody comes in, they're going to work 8 to 10 hours, and then when they leave, that IP address will be freed up. I feel like that's a pretty good one. So let's do it good for 12 hours. I'm going to multiply that by there's 60 minutes in an hour, multiplied by there is 60 seconds in a minute. So I want a default lease time of 8,640. So I'm going to change this to 8,640.
The next thing that I run across is the max lease time, and so I'm going to calculate the max lease time here. This maybe I don't care as much about. Maybe I want it to be good for 2 days. So if I want to calculate that 2 days, there's 24 hours in a day, and there's 60 minutes in an hour, and then there is 60 seconds in a minute. So 172,800. So 172,000 is what I have for that. And that gives them a much longer lease time.
I've made my terminal window a little bit larger to make sure that it's visible and you can see what's in this dhcpd.conf file. So let's take a look at what's inside of it. I'm going to scroll down, and the first thing I'm going to talk about is the default lease time, which is the default time that it's going to hand out to the clients, versus the max lease time, which is the max that it will hand out to the client. The difference is that a lot of clients are really not going to request a time frame in which they want a lease given to them, and so they're just going to get the default lease time. So we can specify a default lease time. But some clients may request a specific lease time, and they can get up to whatever the max lease time is that we're going to allow.
First of all, this is 600. The default lease time is 600, and that is actually pretty short. I'm going to bring up a calculator here, and 600 seconds divided by 60, because there's 60 seconds in a minute, and we come up with 10 minutes. So this is saying the default lease time is 10 minutes, which might be a little short. That means that every 10 minutes a client is checking in. Actually, it's a little bit shorter than that, because they start checking in about the halfway mark and say, "Hey, can I still keep this IP address? Hey, can I still keep this IP address?" So that's a little short. And if you have a very large network, that could end up being a lot of traffic that you generate.
Of course, if we extend things out, let's say we have it for a month and we hand out IP addresses for a month, and you have a lot of people that are getting on your network and then they get off two hours later. I worked at a college where I managed their network at this college, and there was constantly people coming in and out. So if you were to hand out those IP addresses and they had it for a month, then we would eat up all of those IP addresses, because there's always new people that were coming on, and then this DHCP server couldn't hand out that IP address for that whole month. What we needed to do in that case is we need to bring the lease times down. In some cases you want to bring the lease times up, like if you have the same people that are reporting in. It also depends on how many IP addresses you have. If you have a ton of IP addresses, way more than how many clients you have, then why do you need to make them check in more often? You could have them check in less.
So in this case, I really like to set the default lease time to about 12 hours. The reason is because people are going to come onto campus, or come on to the business, and plug in their laptop. They're going to work for eight to 10 hours and then they're going to jump back off, and then that frees that up, and maybe they're working remotely the next day. So it frees up that address.
So if I were to figure that out, and I want to make sure this is clear, I want this default lease time to be 12 hours. So 12 hours: there are 60 minutes in an hour, so I'm going to multiply that by 60, and then there is 60 seconds in a minute. So now I come up with 43,200. So I'm going to change this default lease time to 43,200.
And then as far as max lease time, let's say a couple days, maybe a week. I'm just going to do a couple days. That's 48 hours is a couple of days. So 48, and there's 60 minutes in an hour, so multiply that by 60, and then there is 60 seconds in a minute, so multiply that. And now I've got my lease times set in the configuration file.
As we scroll further down in this configuration file, we see that there's this authoritative right here. Some of your networks will have multiple DHCP servers, or you'll have DHCP that's tied in to some other services. In either case, a stray DHCP server is not good on your network. You don't want to have any stray DHCP servers. So this is just kind of a check to make sure that there's no other DHCP servers on your network. There's not. This is part of a network that there is not another DHCP server, so this is going to be the authoritative server for the network. So I'm going to delete the comment on there to make sure that this is authoritative.
Scrolling through the rest of this configuration file, I'm going to see several scenarios here. This is just a very basic declaration here of the range that you want to hand out. I want something with a few more options here. Here's the BOOTP, which is the older version of DHCP, so I don't need that. And then here we have for internal networks. This has a lot of the features that I'm looking for, where I want to hand out a small range, I want to possibly specify a domain name server, and whether I want to define a router. There's different things I can define with this. So this is the one that I'm looking for.
I'm going to uncomment first of all the subnet, and then what am I going to define within this? What I want to define is, first of all, the range: zero zero zero, and we'll do a later part of the range. So maybe let's do 150 to 160. I'm just going to do this small range for now. And then the subnet: within here I need to make sure that I specify the subnet that this is going to be on, which is going to be 10.1.0.0.
Domain, I don't really have a domain server on there, so I'm not going to specify that. The router, I don't have a router at this point in time, so I'm not going to specify that. I don't need a broadcast address, I just want the default broadcast address.
Then I have the default lease time and the max lease time. This is if I want to override the configuration that I had earlier. So what I can actually do is I can use this Pi to be a DHCP server for multiple networks that are out there. I could specify multiple ranges within this, and I can then copy this and paste it, so that way I can configure these different networks. And so I can actually specify a different lease time and a max lease time for each one of those networks. And if I scroll down, there's also ways that I can actually specify different groups or different host names. There's a lot of options within here.
The next step in this process is to tell the device which interfaces it's going to be listening for those DHCP discoveries on. What we're going to do is get into this file right here: sudo nano and then /etc/default/isc-dhcp-server. When we get into there, we see that I've actually already set in here the interfaces version 4 set to eth0. So now it's going to be listening on eth0.
It will be responding to that, and it's going to associate. It knows the IP address scope that it's going to associate with this eth0. It's going to associate the two, because the static IP address that we set on this machine is going to be associated with the pool that we created in that dhcpd.conf file.
So now I've got this all set up so that it's listening. I'm going to exit out of here. At this point in time, I could start up the service, or I can restart the server. If I wanted to restart the server, what I could do is I could type in this command right here: sudo service isc-dhcp-server start. And then I hit the start button, and then that will get it running. So I'm going to go ahead and do this.
Then we'll jump over to the Windows box and see what it looks like on the Windows box. Time to see if our Pi configurations took effect. What I'm going to do is get onto that network interface card, so we will open this up. I am going to configure this card, and I'm actually going to go through the adapter options here and go into the properties of the adapter options. We are going to choose IP version 4, and then I'm going to say obtain IP address automatically. We're going to click OK and close out of this.
Then you can see that it's identifying, so it's in the process to try to get a DHCP address here. Once I've given this machine enough time to get a DHCP address, I can open up a command window and I'm going to type in ipconfig. And if we look at this ipconfig, it has 150. So it is now getting one of those host addresses. It's the first host address that we defined in our pool.
TechKnowSurge builds IT and cybersecurity professionals through hands-on, concept-first training built around real understanding — not memorization. Free interactive tools, structured programs, and 25+ years of real-world experience, all in one place.
Explore free tools and programs →