How to add a Static or Persistent Route on Windows

Introduction

What is a Routing Table

A routing table indicates where will go the packets after leaving the system(PC, Server, Router, Switch. Firewall ..). The routing table in Windows does the same job. Determinate the best path to send the packets when will receive the packets from another source (PC, Server, ....)

In a big environment that they have multiple networks, it's a very common practice to add routes for different networks or sometimes for specific IP Addresses. Always the decisions are taken based on the requirements that they have.

I will try to give you a very simple example.

We have a PC with two different Network Interfaces that are connected to different Networks. Because we can't assign different gateways in each Interface we should leave one interface without a gateway.

In this case, we can route the packets from the interface without a gateway in the gateway that we want. So each time that we use the network that we have assigned in the interface without the gateway Windows will know where to send the packets. 

 

How to view the routing table

Before proceeding with any change in the routing table or troubleshooting network issues you should view the routing table that already exists. As a best practice, it's recommended to keep a note of the routing table before proceeding with any change.

You can type the following command to view the routing table. You will see a long list of network destinations and gateways. However, if you don't have added a static route all the entries are created dynamically from the Windows.

You can see 3 different categories:

  • IPv4 Route Table = List all the IPv4 dynamic routes that have been building from the windows.
  • Persistent Routes = List all the static routes created by the IT Admins.
  • IPv6 Route table = List all the IPv6 dynamic routes that have been building from the windows.

route print

 

How to add a static route

To add a static route on the routing table type the following command.

route add destination_network MASK subnet_mask gateway_ip_addres metric

Let's explain one by one and give an example

  • destination_network = From the network subnet that will receive the traffic
  • subnet_mask= Is the subnet mask of the destination_network
  • gateway_ip_address = The gateway that will pass the traffic.
  • metric = It's a value used by routing table to make a routing decision. This value is based on link speed, number of hops, and time delay. It's optional.

Note

The static route that will be added manually, exists until the next restart of the PC/Server

How to add a persistent route

If we want to keep permanent the route then we need to add the option -p as in the example.

route add -p destination_network MASK subnet_mask gateway_ip_addres metric

This option will keep the route in the Persistent Routes of the routing table and will not deleted after a restart.

Let's go to do an example with a very simple scenario

The scenario is the following:

We have one Laptop with a connection to a Local Network with a wired cable and a connection to Wi-fi.

We want to pass the traffic from the Local connection to our Local Network (Domain Controller, Fileserver ...). However, we would like to connect to the Internet only from the Wi-fi connection.

The networks are

  • 172.16.3.0/24 for the local network
  • 192.168.137.0 for the Wi-fi connection

We will type the following command to route the traffic from the Local connection (wired cable)  to the Local Network.

route add -p 172.0.0.0 MASK 255.0.0.0 172.16.3.254

 

Let's explain the above command.

Because we have multiple VLANs in our network we say that any traffic that comes from the subnet 172.0.0.0 (which includes all the subnets that start with 172)  routes to the gateway 172.16.3.254 which is the gateway of the Local connection.

Now let's create the routing for the Wi-fi.

route add -p 0.0.0.0 MASK 0.0.0.0 192.168.137.254

 

 

Let's explain the above command.

The following command routes all the traffic that isn't matched to the subnets 172.0.0.0 with mask 255.0.0.0 to the gateway of the Wi-fi adapter.

Let's take a look at the routing table to verify the routes

Type route print and check the Persistent Routes. We should see the new routes

 

After we have created and verified the routes in the routing table, we should verify that all work as expected.

Type the following command, and monitor the real-time connections of your machine.

netstat /f

Then open a Web page and check from where to send the requests.

Also, open a Remote desktop connection to an internal Server,  and check from where the requests are sent.

If all are working as expected, the requests to the web page should be sent from 192.168.137.x, and the requests to the Remote desktop connections should be sent from 172.16.3.x

How to delete a static or persistent route

If we have created routes we would like to delete, we can replace the route add with the route delete.

Let's see the example with the command.

route delete 0.0.0.0 MASK 0.0.0.0 192.168.137.254

 

The command for the route it's very simple. However, you should have understood the traffic flow and what you want to achieve.

I hope to be valuable in this article.

See you next week.