Configuring DHCP server from command-line on Windows Server 2003


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.

This article describes how to do that on Microsoft Windows Server 2003.

If our server has installed DHCP service, we can configure it remotely from command-line using netsh command. We have to login to any Windows 2003 server and run command-line console. In this console we have to type netsh

netsh command-line

Next, we use dhcp context of netsh to configure our DHCP server(s).

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 DNS domain name
  • 192.168.1.10 and 192.168.1.12 as WINS servers
  • Hostname or IP address of DHCP server(s)

If we collect all of these settings we can start to configure 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”>

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

netsh output

Each time we will see this 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.

netsh> dhcp server 192.168.1.1 scope 192.168.1.0 set state 0

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>

netsh> dhcp server 192.168.1.1 scope 192.168.1.0 add iprange 192.168.1.100 192.168.1.149

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>

netsh> dhcp server 192.168.1.1 scope 192.168.1.0 add excluderange 192.168.1.130 192.168.1.134

So, if we want to exclude only one IP address we should use this syntax (let’s say only 192.168.1.130)

netsh> dhcp server 192.168.1.1 scope 192.168.1.0 add excluderange 192.168.1.130 192.168.1.130

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>

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

DHCP_Flags are: BOOTP only, DHCP only or BOTH

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 that 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)

netsh> dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 003 IPADDRESS 192.168.1.254

netsh> dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 006 IPADDRESS 192.168.1.10 192.168.1.11

netsh> dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 044 IPADDRESS 192.168.1.10 192.168.1.12

netsh> dhcp server 192.168.1.1 scope 192.168.1.0 set optionvalue 046 BYTE 8

Now, we set  domain suffix

netsh> dhcp server <DHCP_IP_Address_or_hostname> scope <Scope_Network_ID> set optionvalue <option_value> STRING <Domain_suffix>

 

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

We’ve just finished DHCP configuration. Now, we have to enable scope for serving IP addresses and authorize our DHCP server

netsh> dhcp server 192.168.1.1 scope 192.168.1.0 set state 1 (now scope is active)

netsh> dhcp server initiate auth (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.

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 state 1

dhcp server initiate auth

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 script execution

It’s done!

Author: Krzysztof Pytko

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 63 other followers

%d bloggers like this: