<

Go Premium for a chance to win a PS4. Enter to Win

x

Configuring DHCP server from command-line

Published on
35,200 Points
27,000 Views
2 Endorsements
Last Modified:
Many of us need to configure DHCP server(s) in their environment. We can do that simply via DHCP console on server or using MMC snap-in on each computer with Administrative Tools installed in a network. But what if we have to configure many DHCP servers in short time or our DHCP server is in location where network connection is very slow? Do we have to configure it manually? Do we have to be patient and waste our valuable time? Answer is: NO, we can use a command-line tool which is available on each Windows 2003/2008 server. It’s very powerful utility and it’s simple in use.

If our server has installed DHCP service, we can configure it remotely from command-line using netsh command.

Before we start configuring server, we need some details:

Hostname or IP address of DHCP server(s)
Scope IP address (Network ID) and network mask
Scope name
Description for scope
IP pool for scope
Any IP reservation
Any IP exclusion
Default gateway IP address
IP address of DNS servers
Domain suffix name
IP address of WINS servers (if required)

In our example we use:

192.168.1.1 as DHCP server IP address
192.168.1.0/24 as network ID
TestScope as scope name
“This is my test scope” as description
192.168.1.100 – 192.168.1.149 as scope’s pool
192.168.1.125 reserved IP address for device with 00-03-EF-15-9A-6B MAC address
192.168.1.130 – 192.168.1.134 as excluded IP addresses
192.168.1.254 as default gateway
192.168.1.10 and 192.168.1.11 as DNS servers
testenv.local as domain suffix
192.168.1.10 and 192.168.1.12 as WINS servers

If we collect all of these settings we can start to configure DHCP server(s). We have to login to any Windows 2003/2008 server and run command-line console. In this console we have to type netsh

 NETSH utility
Next, we use dhcp context of netsh to configure our DHCP server(s).
The very first thing is to create a scope on DHCP server. To do this we have to type command

netsh> dhcp server <DHCP_IP_Address_or_hostname> add scope <Scope_Network_ID> <Mask> <Scope_name> <”Scope_description”>

 
dhcp server 192.168.1.1 add scope 192.168.1.0 255.255.255.0 TestScope “This is my test scope”

Open in new window


Each time we will see "Command completed successfully" message, it means that we set an option properly.
We have created a scope on our DHCP server. Now, we need to activate it

netsh> dhcp server <DHCP_IP_Address_or_hostname> scope <Scope_Network_ID> set state 1

If we don’t want to set it as active during creation process, set state value should be 0.

 
dhcp server 192.168.1.1 scope 192.168.1.0 set state 0

Open in new window


We don’t want to active scope right now. We will do it later.

After that, we have to define IP addresses pool
netsh> dhcp server <DHCP_IP_Address_or_hostname> scope <Scope_Network_ID> add iprange <Start_IP_Address> <End_IP_Address>


 
dhcp server 192.168.1.1 scope 192.168.1.0 add iprange 192.168.1.100 192.168.1.149

Open in new window


We have defined scope’s pool and we will exclude some IP addresses, now.
netsh> dhcp server  <DHCP_IP_Address_or_hostname> scope <Scope_Network_ID> add excluderange <Start_excluded_IP_Address> <End_excluded_IP_Address>

 
dhcp server 192.168.1.1 scope 192.168.1.0 add excluderange 192.168.1.130 192.168.1.134

Open in new window

So, if we want to exclude only one IP address we should use this syntax (let’s say only 192.168.1.130)
 
dhcp server 192.168.1.1 scope 192.168.1.0 add excluderange 192.168.1.130 192.168.1.130

Open in new window


Now, we will enable reservation IP address for a device
netsh> dhcp server <DHCP_IP_Address_or_hostname> scope <Scope_Network_ID> add reservedip <Reserved_IP_Address> <MAC_Address> <Reservation_Name> <”Description_for_reservation”> <DHCP_Flags>
DHCP_Flags are: BOOTP only, DHCP only or BOTH

 
dhcp server 192.168.1.1 scope 192.168.1.0 add reservedip 192.168.1.125 0003EF159A6B My_PC “” BOTH

Open in new window


The last steps we need to provide are DNS servers and domain suffix. This time we have to consider where to place them, in the scope options or in a server options. What is the difference?
If our DHCP server contains only one scope we don’t have to worry where we will place these settings, because they will only impact one scope.

Settings applied in “Scope options” affect only that particular scope. Settings configured on “Server options” will be inherited by all scopes, even the new ones. When we set “Server options” and then we additionally configure “Scope options” they will overwrite those global settings.

This is very helpful if we have more than one scope on DHCP settings (most VLAN scenarios) and we have common settings for them. Let’s say that we need to configure the same DNS servers and domain suffix for all scopes then we do it in “Server options”. The only thing we will set in “Scope options” is default gateway.

In our scenario we don’t have more than one scope, so we will configure “Scope options” providing default gateway, DNS servers and WINS servers.
netsh> dhcp server  <DHCP_IP_Address_or_hostname> scope <Scope_Network_ID> set optionvalue <option_value> IPADDRESS <Default_Gateway_IP_Address or DNS_Server_IP_Addresses or WINS_Server_IP_Addresses>
for WINS we need to set
netsh> dhcp server <DHCP_IP_Address_or_hostname> scope <Scope_Network_ID> set optionvalue <option_value> BYTE <one of these node types: 1,2,4,8>

WINS node types:

1 b-node (broadcasts)
2 p-node (point-to-point name queries to WINS)
4 m-node (broadcasts then query name server)
8 h-node (query name server then broadcasts)

 
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 003 IPADDRESS 192.168.1.254
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 006 IPADDRESS 192.168.1.10 192.168.1.11
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 044 IPADDRESS 192.168.1.10 192.168.1.12
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 046 BYTE 8

Open in new window


Now, we set  domain suffix
netsh> dhcp server <DHCP_IP_Address_or_hostname> scope <Scope_Network_ID> set optionvalue <option_value> STRING <Domain_suffix>

 
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 015 STRING testenv.local

Open in new window


We’ve just finished DHCP configuration. Now, we have to enable scope for serving IP addresses and authorize our DHCP server. To authorize DHCP server we need to be a member of one of these groups: "DHCP Administrators" or "Enterprise Administrators". In case of standalone DHCP server we need only local administrator credentials. In single/multi domain environment we have to be a member of mentioned groups in root domain. DHCP server needs authorization only once. So, if it's authorized we can create as many scopes as we wish. In case that we aren't members of those groups we have to ask someone who can do that for us.

dhcp server 192.168.1.1 scope 192.168.1.0 set state 1

Open in new window

(now scope is active)
dhcp server initiate auth

Open in new window

(now DHCP server is authorized)

We finished our DHCP server configuration. If we need any other “Scope/Server options” we can set them like we did it with DNS,WINS and others.
And now, one more important thing: our scope has set by default 8 days for leases. What if we want to have shorter or longer lease time? Let's say that we have 10-15 addresses in DHCP pool and people with notebooks very often leave/visit our office. Then we have to configure one more option for our DHCP scope. To set lease time for scope we have to define optionvalue 051 and specify lease time in seconds. So, let's try to configure this option for our scope:

netsh> dhcp server <DHCP_IP_Address_or_hostname> scope <Scope_Network_ID> set optionvalue 051 DWORD <time_in_seconds>

We will set 4 hrs. for our lease time

 
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 051 DWORD 14400

Open in new window


OK, but you wrote that it will be automated and simple configuration but we lost so much time configuring DHCP server from command-line? We could do it via console! Yes, you’re right we have to prepare a template for automated configuration.

Preparing template for automated DHCP configuration is very simple. We have to put all those commands into text file without NETSH command, so let’s create i.e. text file named dhcp_conf.txt and put there

 
dhcp server 192.168.1.1 add scope 192.168.1.0 255.255.255.0 TestScope “This is my test scope”
dhcp server 192.168.1.1 scope 192.168.1.0 set state 0
dhcp server 192.168.1.1 scope 192.168.1.0 add iprange 192.168.1.100 192.168.1.149
dhcp server 192.168.1.1 scope 192.168.1.0 add excluderange 192.168.1.130 192.168.1.134
dhcp server 192.168.1.1 scope 192.168.1.0 add reservedip 192.168.1.125 0003EF159A6B My_PC “” BOTH
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 003 IPADDRESS 192.168.1.254
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 006 IPADDRESS 192.168.1.10 192.168.1.11
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 044 IPADDRESS 192.168.1.10 192.168.1.12
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 046 BYTE 8
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 015 STRING testenv.local
dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 051 DWORD 14400
dhcp server 192.168.1.1 scope 192.168.1.0 set state 1
dhcp server initiate auth

Open in new window


and save this file on the network drive which is available from any Windows Server 2003/2008 machine. Now, we can modify necessary parts of this template to adjust it for any other DHCP server configuration.
The only thing that we have to do is running NETSH command on Windows 2003/2008 Server in context of EXEC <full_path_to_dhcp-conf.txt_file>

in example:
netsh exec c:\dhcp_conf.txt

 NETSH output
If we have really slow network connection or we want to configure DHCP server from our workstation, we can use SysInternals tool called PsExec which is able to run command on remote system. But first we have to copy txt file to the server using xcopy command.

xcopy <path_to_txt_file> <unc_location_for_the _folder>

 
xcopy c:\dhcp.txt "\\server01\c$\documents and settings\administrator\desktop"

Open in new window


We should download it from the Internet and extract on our PC. Next in command-line type proper command syntax (Path to PsExec has to be accessible from command line, so put it into one of PATH variable location i.e. %WINDIR%\SYSTEM32)  

PsExec \\<server_name_or_IP_where_DHCP_will_be_configured> -u <user_credentials_running_script_remotely> -p <password_for_this_user> <netsh_excec_local_path_to_txt_file>

 
PsExec \\server01 -u administrator@testenv.local -p <admin_password> netsh exec "c:\documents and settings\administrator\desktop\dhcp.txt"

Open in new window


It’s done!

iSiek
2
Comment
1 Comment
 
LVL 2

Expert Comment

by:Vivek Reddy
Hi,

try this Steeps

C:\Users\Administrator>start /w ocsetup DHCPServer
In Windows Server 2008 Core, the Role name is “DHCPServerCore”. So, in Windows Server 2008 Core installation, it is
C:\Users\Administrator>start /w ocsetup DHCPServerCore
Set DHCP Service to be Automatic
By default, after the role is enabled, the Service is still disabled. Hence Set the type to Auto as follows
C:\Users\Administrator> sc config dhcpserver start= auto
Start the DHCP Server
C:\Users\Administrator>net start dhcpserver
Add DHCP Server and Authroize in AD
If the DHCP server is installed in an Active Directory domain, you must authorize it in Active Directory. Now, let’s use the netsh commands to setup the server and configure the relevant parameters.
C:\Users\Administrator>netsh dhcp add server dc1 192.168.0.5
Adding server dc1, 192.168.0.5
Command completed successfully.
Where DC1, is the DHCP Server and the IP Address follows it
Add DHCP Scope
C:\Users\Administrator>netsh dhcp server 192.168.0.5 add scope 192.168.10.0 255.255.255.0 Scope1 Scopevlan10
Command completed successfully.
In the above the
DHCP scope – 192.168.10.0 255.255.255.0
Scope1 – Scope Name
ScopeVlan10 – Comment for the scope
Syntax is
netsh dhcp server 192.168.0.5 add scope <Subnet> <Subnet mask> <ScopeName> <Scope comment>
Set Scope IP Range
C:\Users\Administrator>netsh dhcp server 192.168.0.5 scope 192.168.10.0 add ipra nge 192.168.10.1 192.168.10.254
Changed the current scope context to 192.168.10.0 scope.
Command completed successfully.
Here the IP range is 192.68.10.1-192.168.10.254 for the scope 192.168.10.0
Syntax is
netsh dhcp server <Server> scope 192.168.10.0 add iprange <StartIP> <EndIP>
Add Exclusion Range
Add any IP Exclusion ranges if any.
C:\Users\Administrator>netsh dhcp server 192.168.0.5 scope 192.168.10.0 add excluderange 192.168.10.1 192.168.10.25
Changed the current scope context to 192.168.10.0 scope.
Command completed successfully.
Here the exclusion list has the range between 192.168.10.1 to 192.168.10.25
Syntax is
netsh dhcp server <Server> scope <Scope> add excluderange <StartExclusion> <End-Exclusion>
Set Option Code 003 for Default Routers
Set the option Code 003 and specify the Gateways
C:\Users\Administrator>netsh dhcp server 192.168.0.5 scope 192.168.10.0 set optionvalue 003 IPADDRESS 10.1.1.1 10.1.1.2
Changed the current scope context to 192.168.10.0 scope.
Command completed successfully.
Here the gateways are 10.1.1.1 & 10.1.1.2
Syntax is
netsh dhcp server <Server> scope 192.168.10.0 set optionvalue 003 IPADDRESS <Gateway1> <Gateway2>
Set Option Code 006 for Default DNS Servers
C:\Users\Administrator>netsh dhcp server 192.168.0.5 scope 192.168.10.0 set optionvalue 006 IPADDRESS 192.168.0.5 192.168.10.1
Changed the current scope context to 192.168.10.0 scope.
Command completed successfully.
Here the DNS Servers are 192.168.0.5 & 192.168.10.1
Syntax is
netsh dhcp server <Server> scope 192.168.10.0 set optionvalue 006 IPADDRESS <Primary DNS> <Secondary DNS>
Activate DHCP Server Scope
Now, the relevant DHCP settings are complete. Lets activate the Scope
C:\Users\Administrator>netsh dhcp server 192.168.0.5 scope 192.168.10.0 set state 1
Changed the current scope context to 192.168.10.0 scope.
Command completed successfully.
This should help you up and running with DHCP in less than 5 minutes. You can always modify from command line or by using DHCP MMC later on!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Join & Write a Comment

this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month