Windows Server - static IP address, DHCP, DNS server

Network topology
Renaming computers
Setting static IP address
Adding the DNS role
Adding the DHCP role and defining a DHCP scope
Testing DHCP IP assignment

Enabling ping via firewall rules and testing connectivity

Network topology

We have a topology of 2 Windows Server 2022 VMs, which we will call winsrv-1 and winsrv2-2.
winsrv-1 is directly connected to the router via the IP 192.168.1.10.
It will also be DHCP server for the 172.16.1.0/24 subnet and will connect to winsrv-2, which will connect to winsrv-1 via a the latter’s second network card.

                                  +---------------+
                                  |   Router      |
                                  | 192.168.1.1   |
                                  +---------------+
                                         |
                                         | 192.168.1.0/24
                                         |
                                         |
                                         |
                +---------------+        |        +---------------+
                | Server 1      |        |        | Server 2      |
                | DNS/DHCP      |        |        | (Other)       |
                | 192.168.1.10  |--------+        |               |
                | 172.16.1.1    |-----------------| 172.16.1.10   |
                +---------------+  172.16.1.0/24  +---------------+

Renaming computers

The first thing is to rename the computers:

PS > Rename-Computer -NewName "winsrv-1"
WARNING: The changes will take effect after you restart the computer WIN-P5C9JAAIAI0.

PS > Rename-Computer -NewName "winsrv-2"
WARNING: The changes will take effect after you restart the computer WIN-JNRNAFJURQ8.

We will then reboot the VMs via Restart-Computer.
To confirm the hostname change after the reboot, we can use $env:COMPUTERNAME:

PS > $env:COMPUTERNAME
WINSRV-1

Setting static IP address

Since winsrv-1 has two network cards and is connected to the internet via the router with a DHCP IP address in the 192.168.1.0/24 range, we will set the IP to static, as it is required if we later want to promote it to domain controller in Active Directory, or for the role of DNS and DHCP server.
To list the network cards for winsrv-1, we will use Get-NetAdapter and show only the name, MAC address, index and status via Select-Object:

PS C:\Users\Administrator> Get-NetAdapter | Select-Object Name,MacAddress,ifIndex,InterfaceOperationalStatus

Name      MacAddress        ifIndex InterfaceOperationalStatus
----      ----------        ------- --------------------------
Ethernet0 00-0C-29-3E-30-56      15                          1
Ethernet1 00-0C-29-3E-30-60       8                          1

We will list the current IP addresses of the 2 interfaces.
We can notice that one network card has a dynamically assigned IP 192.168.1.102 from the router. We will change this to a static IP.

PS > Get-NetIPAddress -InterfaceIndex 8, 15 -AddressFamily IPv4 | Format-Table InterfaceIndex, IPAddress, SuffixOrigin

InterfaceIndex IPAddress      SuffixOrigin
-------------- ---------      ------------
             8 169.254.77.136         Link
            15 192.168.1.102          Dhcp

We will use New-NetIPAddress:

PS > New-NetIPAddress -InterfaceIndex 15 -IPAddress 192.168.1.110 -PrefixLength 24 -DefaultGateway 192.168.1.1


IPAddress         : 192.168.1.110
InterfaceIndex    : 15
InterfaceAlias    : Ethernet0
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Tentative
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 192.168.1.110
InterfaceIndex    : 15
InterfaceAlias    : Ethernet0
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Invalid
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : PersistentStore

We will set the DNS to the default gateway IP:

 Set-DnsClientServerAddress -InterfaceIndex 15 -ServerAddresses 192.168.1.1

We will set the static IP of 172.16.1.1 for the other network card, index 8; this will be necessary for the DHCP role, which will distribute IPs in the 172.16.1.1/24 range:

PS > New-NetIPAddress -InterfaceIndex 8 -IPAddress 172.16.1.1 -PrefixLength 24 -DefaultGateway 172.16.1.1


IPAddress         : 172.16.1.1
InterfaceIndex    : 8
InterfaceAlias    : Ethernet1
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Tentative
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

We will set the DNS server to the gateway IP for the subnet:

Set-DnsClientServerAddress -InterfaceIndex 8 -ServerAddresses 172.16.1.1

Adding the DNS role

Let’s install the DNS role on the winsrv-1 VM. DNS will be needed for forward queries to the gateway for name resolution from the winsrv-2 VM, which will be connected via the 172.16.1.0/24 subnet.

PS > Install-WindowsFeature -Name DNS -IncludeManagementTools

Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {DNS Server, Remote Server Administration

Adding the DHCP role and defining a DHCP scope

Let’s install the DHCP server role:

PS > Install-WindowsFeature -Name DHCP -IncludeManagementTools

Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {DHCP Server, DHCP Server Tools}

We will then define the scope for the 172.16.1.1/24 subnet.
We will allocate IPs from .10 to .254.
The .1 IP is used by the server, the .255 is a broadcast address, messages are sent to all hosts in the subnet.

# Define the scope settings
$ScopeName = "Scope172.16.1.0"
$SubnetMask = "255.255.255.0"
$StartRange = "172.16.1.10"
$EndRange = "172.16.1.254"
$DefaultGateway = "172.16.1.1"

# Create the DHCP scope
Add-DhcpServerv4Scope -Name $ScopeName -StartRange $StartRange `
	-EndRange $EndRange -SubnetMask $SubnetMask -State Active

# Configure the default gateway for the scope
Set-DhcpServerv4OptionValue -ScopeId "172.16.1.0" `
	-OptionId 3 -Value $DefaultGateway

Testing DHCP IP assignment

Let’s connect to winsrv-2 and switch the adapter off and on again, for it to pick up a DHCP IP from winsrv-1.
We will use Disable-NetAdapter and Enable-NetAdapter:

PS C> Get-NetAdapter

Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
----                      --------------------                    ------- ------       ----------             ---------
Ethernet0                 Intel(R) 82574L Gigabit Network Conn...       5 Up           00-0C-29-96-18-C6         1 Gbps

PS > Disable-NetAdapter -Name "Ethernet0" -Confirm:$false
PS > Enable-NetAdapter -Name "Ethernet0"
PS > Get-NetIPAddress -InterfaceIndex 5 -AddressFamily IPv4 | Format-Table InterfaceIndex, IPAddress, SuffixOrigin

InterfaceIndex IPAddress   SuffixOrigin
-------------- ---------   ------------
             5 172.16.1.10         Dhcp

We can now see that IP 172.16.1.10 was assigned to the host.

Enabling ping via firewall rules and testing connectivity

Let’s enable ping in the firewall rules for both machines:

PS > Set-NetFirewallRule -DisplayName "File and Printer Sharing (Echo Request - ICMPv4-In)" -Enabled True -Profile Any

We can then use Test-NetConnection to test connectivity:

PS > Test-NetConnection -ComputerName 172.16.1.1

ComputerName           : 172.16.1.1
RemoteAddress          : 172.16.1.1
InterfaceAlias         : Ethernet0
SourceAddress          : 172.16.1.10
PingSucceeded          : True
PingReplyDetails (RTT) : 0 ms

PS > Test-NetConnection -ComputerName 172.16.1.10

ComputerName           : 172.16.1.10
RemoteAddress          : 172.16.1.10
InterfaceAlias         : Ethernet1
SourceAddress          : 172.16.1.1
PingSucceeded          : True
PingReplyDetails (RTT) : 0 ms