Sometimes you may need to route traffic through a specific gateway only for destinations matching a group of IPs or a subnet.
Static routes are usually configured at the router level but you can also configure them locally, from the Windows command prompt.
The operation is quite easy, you won’t miss the GUI. In our example we are using Windows Server 2012 R2, but you can do the same with any version of Windows.
First of all, let’s see our routing table with the command
We can see Internet traffic (identified by destination 0.0.0.0 and mask 0.0.0.0) is routed through the gateway 192.168.0.1 while the subnet 192.168.0.0/24 is directly connected (On-Link).
Now it’s time to create a static route. We will send traffic directed to the IP address 184.108.40.206 through the alternative gateway 192.168.0.254.
We need to use the command
route of the CMD prompt. Here’s a description of the parameters:
- Destination: specifies an IP address, a hostname or the host
- Subnetmask: specifies a subnetmask associated to the route (if not specified the value is 255.255.255.255)
- Gateaway: specifies the IP address of the gateway
- Cost: specifies an integer value between 1 and 9,999 for cost measurement (if not specified the value is 1)
- Interface: specifies the interface used for the route (if not specified it will be based upon the IP address)
In our example we sent the command as following:
c:\route add 220.127.116.11 mask 255.255.255.255 192.168.0-254 –p
The -p parameter specifies the configuration is permanent:
netstat -rn we can check if the static route has a metric value lower than the other ones (higher priority):
tracert command we can check if the static route is working:
You can delete the route any time with the command
c:\route delete 18.104.22.168: