Creating a DHCP scope to assign addresses to computers

In the Configuring a combination Domain Controller, DNS server, and DHCP server recipe, we installed the DHCP role onto a server called DC01. Without some configuration, however, that role isn't doing anything. In most companies that I work with, all the servers have statically assigned IP addresses, which are IPs entered by hand into the NIC properties. This way, those servers always retain the same IP address. But what about client machines that might move around or even move in and out of the network? What about phones, tablets, and laptops that join your Wi-Fi networks? DHCP is a mechanism that clients use in order to obtain IP addressing information for the network that they are currently plugged into.

This way, users or admins don't have to worry about configuring IP settings on the client machine as they are configured automatically by the DHCP server. In order for our DHCP server to hand out IP addresses, we need to configure a scope to use.

Getting ready

We have a Server 2019 machine online with the DHCP role installed. We will also be testing using a Windows 10 client machine to ensure that it is able to acquire IP address information properly from the server.

It is important that you do not do this recipe if you are simply testing this out and are connected to a production network! If you are using a virtual machine (VM), make sure it is not connected to your work or home network – if you can isolate it to a lab network, that would be ideal. Doing this as a test while on a production network could upset many other services on your network.

How to do it…

Perform the following steps to create and configure a DHCP scope to assign addresses to client computers:

  1. Drop down the Tools menu inside Server Manager and click on DHCP. This opens the DHCP management console.
  2. Expand the left-hand pane, where the name of your DHCP server is listed. You will see sections for IPv4 and IPv6. For our network, we are sticking with IPv4, so right-click on that and choose the option for New Scope…:

    Figure 2.20 – The New Scope option in the DHCP Manager

  3. Start the New Scope Wizard screen by creating a name for your scope. This can be anything you like.
  4. Enter a range of IP addresses that you would like the DHCP server to hand out to computers. Typically, you would choose a range that is big enough to encompass all the client machines you expect, but small enough to allow room for static IP address either side. Often, the first 50 or 100 IP addresses in the network are set aside for static IP addresses and the following 200 or 150 addresses are used for DHCP. The Subnet mask field will likely populate automatically, but Windows often doesn't get this correct – just double-check to make sure it is accurate:

    Figure 2.21 – The basic configuration options for a new DHCP range

  5. On the Add Exclusions and Delay screen, if there are any IP addresses within the scope you just defined that you do not want handed out, specify them here. For example, if you are going to use .50 through .250, as in our example, but you already have a print server running on .75, you could exclude .75 on this screen so that DHCP doesn't try to hand out the .75 address to a client computer.
  6. Now, set a time in your Lease Duration field. This is the amount of time in-between DHCP refreshes for a client computer. If a particular computer leaves the network and comes back within its lease duration, it will be given the same IP address that it had last time. The default is 8 days, which is probably fine for a corporate network of desktops and laptops but is far too large for a guest Wi-Fi network, which could see hundreds of machines coming and going throughout a given week. I generally drop this to 5 days for corporate networks and 2 hours for guest Wi-Fi networks.
  7. Next, we will populate the rest of the IP information that the client computers need to receive on our network. Fill out the fields for Router (Default Gateway), Domain Name and DNS Servers, and WINS Servers, if necessary. Make sure your Default Gateway is configured correctly for your network as DHCP clients will not get internet access correct for this. The DNS portion is equally important as without this, clients will not be able to look up any domain names (thus breaking most of the internet) and will not be able to access any of your company's resources! You should set this to be the IP addresses of your domain controllers. You probably do not have any WINS servers and can, in most likelihood, leave this blank.
  8. The last item to choose is Yes, I want to activate this scope now. Now, we're in business!
  9. As a quick test, let's boot a client computer onto this network whose NIC has not been configured with a static IP. If we take a look at its IP configuration, we can see that it has successfully received IP addressing information from our DHCP server automatically:

Figure 2.22 – Example ipconfig output

Configuring DHCP with PowerShell is slightly more complicated than the one-liner Powershell codes we've seen so far. To repeat these steps in PowerShell, you will need to import the DHCPServer module and then use the Add-DhcpServerv4Scope and Set-DhcpServerV4OptionValue cmdlets:

Import-Module DHCPServer

Add-DhcpServerv4Scope -Name Cookbook -StartRange 172.16.97.50 -EndRange 172.16.97.250 -SubnetMask 255.255.255.0 -LeaseDuration 5.0:0:0

Set-DhcpServerv4OptionValue -ScopeId 172.16.97.50 -DnsDomain ad.cookbook.packt.com -DnsServer 172.16.97.2,172.16.97.3 -Router 172.16.97.1

If that looks complicated, it's probably just because of the sheer number of IP addresses we've had to type in – but given that DHCP deals with IP addresses, that's not surprising. Ultimately, there's two parts to this: first, we created the DHCP scope, and then we had to configure the optional DHCP components of the DNS Server in a second command.

Tip

You do not need to keep running the Import-Module command. I'm including it here for the sake of completeness, but once you've imported a module once, you're good to go for the rest of that PowerShell session without re-importing it. In fact, you may not need to call Import-Module at all due to PowerShell's module auto-loading, but I am including it for the sake of completeness.

How it works…

DHCP is one of the core infrastructure roles that almost everyone uses inside their networks. While we have only scratched the surface here of what DHCP is capable of, the ability to automatically hand out IP addresses to connecting client computers is DHCP's core functionality. Installing the role and creating a scope are our primary steps to make use of DHCP. Take a look at our next recipe for one of the most advanced functions that can be accomplished within your scope.