In network configurations where you need direct host-to-host communication within the same LAN segment without default gateways, Windows presents an interesting challenge compared to Unix-like systems. Let's examine this common scenario:
Computer A: 198.51.100.8/24
Computer B: 203.0.113.9/24
Same LAN segment, no default gateway
The standard method using the route
command requires specifying a gateway:
route add 203.0.113.9 mask 255.255.255.255 198.51.100.8
This works but creates a dependency on the gateway IP address, which may not be ideal in certain network configurations.
Unix-like systems offer more flexibility with interface-based routing:
# Linux
ip route add 203.0.113.9 dev eth0
# FreeBSD
route add 203.0.113.9/32 -iface fxp0 -cloning
For Windows XP/2003
Older Windows versions require using the gateway parameter:
route -p add 203.0.113.9 mask 255.255.255.255 198.51.100.8
Modern Windows (7/8/10/11, Server 2008+)
Newer Windows versions support interface-based routing through PowerShell:
New-NetRoute -DestinationPrefix "203.0.113.9/32" -InterfaceAlias "Ethernet"
Alternatively using netsh
(deprecated in newer versions):
netsh interface ipv4 add route 203.0.113.9/32 "Ethernet"
For a production environment with multiple static routes, consider this PowerShell script:
# Get interface index
$ifIndex = (Get-NetAdapter -Name "Ethernet").ifIndex
# Add multiple host routes
@("203.0.113.9", "203.0.113.10") | ForEach-Object {
New-NetRoute -DestinationPrefix "$_/32" -InterfaceIndex $ifIndex
}
When implementing interface-based routing:
- Verify interface names with
Get-NetAdapter
- Check routing table with
Get-NetRoute
- Use
Test-NetConnection
to verify connectivity - Remember that interface-based routes are specific to the layer 2 segment
Interface-based routing can offer slight performance benefits by eliminating the need for ARP resolution of the gateway address. However, the difference is typically negligible in modern networks.
Let's consider two computers on the same LAN segment:
- Computer A:
198.51.100.8/24
- Computer B:
203.0.113.9/24
Neither has a default gateway configured. The standard approach would be to add static routes like:
route add 203.0.113.9 mask 255.255.255.255 198.51.100.8
But what if we want to specify the interface instead of the gateway IP?
The Windows route
command doesn't directly support interface-based routing like Unix systems do. Attempting:
route add 203.0.113.9 mask 255.255.255.255 if 2
Simply returns the command usage help, indicating this syntax isn't supported.
For Windows 8/10/Server 2012 and later, we have better options:
Using PowerShell
The most straightforward method is using PowerShell:
New-NetRoute -DestinationPrefix 203.0.113.9/32 -InterfaceAlias "Ethernet"
This creates a route that uses the specified interface without requiring a gateway.
Using netsh (Legacy Systems)
For older systems (XP/2003), we're forced to use the gateway approach:
netsh interface ip add route 203.0.113.9/32 "Local Area Connection" 198.51.100.8
After adding the route, verify it with:
Get-NetRoute -DestinationPrefix 203.0.113.9/32
Or for older systems:
route print
To make the route persistent across reboots in PowerShell:
New-NetRoute -DestinationPrefix 203.0.113.9/32 -InterfaceAlias "Ethernet" -PolicyStore ActiveStore
For command line:
route -p add 203.0.113.9 mask 255.255.255.255 198.51.100.8
Another way to influence routing without gateway specification is by adjusting interface metrics:
Set-NetIPInterface -InterfaceAlias "Ethernet" -InterfaceMetric 10
This makes Windows prefer this interface for routing decisions.
- Always verify interface names with
Get-NetAdapter
- Check firewall settings if communication still fails
- For complex networks, consider using
Test-NetConnection