Configuring an IP Address

Using a graphical user interface

 

  1. From the Control Panel, open the Network Connections applet.
  2. Double-click the connection you want to configure.
  3. Click the Properties button.
  4. Double-click Internet Protocol (TCP/IP).
  5. To enable DHCP, select Obtain an IP address automatically. To use a static address, select Use the following IP address. Then configure the IP address, subnet mask, and default gateway.
  6. Click OK until all windows are closed.


Using a command-line interface

The following command configures DHCP for a connection:

> netsh int ip set address name="<ConnectionName>" source=dhcp

Here is an example for configuring the connection named “Local Area Connection” to use DHCP:

> netsh int ip set address name="Local Area Connection" source=dhcp

This configures a connection with a static IP and default gateway:

> netsh int ip set address name="<ConnectionName>" source=static <IP> <Mask> <GateWayIP> <Metric>

This example configures a static IP address for “Local Area Connection”:

> netsh int ip set address name="Local Area Connection" source=static 10.3.53.3
255.255.255.0 10.3.53.1 1

Using VBScript

' This code enables DHCP for the specified connection.
' ------ SCRIPT CONFIGURATION ------
strComputer = "."
strConnection = "Local Area Connection"
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colNA = objWMI.ExecQuery("select * " & _
                             " from Win32_NetworkAdapter " & _
                             " where NetConnectionID = '" & strConnection & "'" )
for each objNA in colNA
   set colNAConfig = objWMI.ExecQuery _
      ("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _
        objNA.DeviceID & "'} " & _
      " WHERE resultClass = win32_NetworkAdapterConfiguration ")
   for each objNAConfig in colNAConfig
      if objNAConfig.DHCPEnabled = True then
         WScript.Echo "DHCP already enabled for " & strConnection
      else
         intRC = objNAConfig.EnableDHCP( )
         if intRC = 0 then
            WScript.Echo "DHCP Enabled for " & strConnection
         elseif intRC = 1 then
            WScript.Echo "You must reboot to start using DHCP for " & _
                         strConnection
         else
            WScript.Echo "There was an error enabling DHCP for " & _
                         strconnection & ": " & intRC
         end if
      end if
   next
next

' This code configures an IP address, subnet mask, and default gateway
' for the specified connection.
' ------ SCRIPT CONFIGURATION ------
strComputer = "."
strConnection = "Local Area Connection"
strIP = Array("1.22.2.2")
strMask = Array("255.255.255.0")
strGatewayIP = Array("1.2.3.3")
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colNA = objWMI.ExecQuery("select * " & _
                            " from Win32_NetworkAdapter " & _
                            " where NetConnectionID = '" & strConnection & "'" )
for each objNA in colNA
   set colNAConfig = objWMI.ExecQuery _
      ("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _
        objNA.DeviceID & "'} " & _
      " WHERE resultClass = win32_NetworkAdapterConfiguration ")
   for each objNAConfig in colNAConfig
      intRC = objNAConfig.EnableStatic(strIP,strMask)
      intRC2 = objNAConfig.SetGateways(strGatewayIP)
      if intRC = 0 and intRC2 = 0 then
         WScript.Echo "IP address configured for " & strConnection
      elseif intRC = 1 or intRC2 = 1 then
         WScript.Echo "You must reboot for the changes to take effect for " & _
                      strConnection
      else
         WScript.Echo "There was an error configuring IP for " & _
                      strconnection & ": " & intRC & " and " & intRC2
      end if
   next
next

If you use static IP addresses, any time you build a new server, you have to configure an IP address on that server. However, there is no reason why you can’t automate the process using either the netsh command shown in the command-line solution or WMI. You still have to find an available IP address, which may not be easy to automate depending on your environment, but at least you can provision the IP address in an automated fashion.

This leads to a discussion of using DHCP on servers. You’ll find some people dead set against it and others who wouldn’t do it any other way. Since network adapters are by default configured to use DHCP, you can remove the step of configuring network settings, such as IP address, DNS servers, etc., on your servers by using DHCP. After the server completes the build process and reboots, it will automatically request an IP address. But as you probably know, DHCP addresses aren’t set in stone. The next time the server reboots, it might obtain a different address. And since you more than likely want to add an A record (and possibly a PTR record) in DNS for this server, if it got a new address, those records would no longer be valid.

There are two ways to work around this. First, you can configure a reservation on the DHCP server for a specific IP address. You only need to retrieve the MAC address of the server’s network adapter order to configure a reservation (you can get this via the ipconfig /all command). With this configuration, the server will always receive the same IP address even though it is getting it from DHCP. The benefit of this is that you don’t have to manually configure the other settings such as DNS and WINS servers directly on the server. The potential drawback is that if you have to change network adapters for any reason, the reservation would also have to be updated.

The second option is to use dynamic DNS to have the server automatically update DNS based on its current IP address. In fact, you could have the sever itself send the dynamic updates or use DHCP to do that. That way, even if the server gets a new IP address after every reboot, DNS will be updated automatically. The benefit to this is that it is the most automated solution. There is very little configuration you have to do on a per-server basis (and it is therefore attractive for large environments). The downside is that DNS could have outdated information for a period of time until the server dynamically updates its new information.