I have the following scenario:

Computer A:, netmask
Computer B:, netmask
Both computers are on the same LAN segment; no default gateway is specified in either case.

To get these two computers to communicate with each other, I've added two static routes, like so:

route add mask

However, I would prefer to add the static routes by specifying a network interface, instead of by specifying a gateway IP address.

This is possible with Linux by using a command such as:

ip route add dev eth0

and similarly in FreeBSD:

route add -iface fxp0 -cloning

However, I'm at a loss of how to do this with Windows. Ideally I want to do something like:

route add mask if 2

but that merely prints out the usage for the route command, which tells me that I'm doing it wrong. I've also tried to use netsh, which tells me:

> netsh routing ip add persistentroute "Local Area Connection"
Specify the next-hop for non point-to-point interfaces.

Any thoughts or suggestions?

Update: When I originally posted this question, I was using Windows XP. But I neglected to mention that.

I'll leave Grizly's original answer, as it is correct for my original question. But if you're using a newer version of Windows than XP/2003, give one of the other answers a try.


5 Answers 5


This may not be possible with windows


Quote: For locally attached subnet routes, the gateway address is the IP address assigned to the interface that is attached to the subnet.

  • Hey Grizly -- thanks for the response! I tried both commands you suggested, but I got: "The route addition failed: Either the interface index is wrong or the gateway does not lie on the same network as the interface. Check the IP Address Table for the machine." I've only got two interfaces on the machine, 0x1 (MS TCP Loopback interface) and 0x2 (Intel(R) PRO/1000 MT Network Connection), so I'm pretty sure "if 2" is what I want to use. Any other thoughts?
    – fission
    Feb 23, 2010 at 22:21
  • Just ignore the "metric 1 if 2" part.. it should figure it out. Best to test without -p as well. (that makes it persistent)
    – Grizly
    Feb 24, 2010 at 7:29
  • I get the same result w/o the "metric 1 if 2" part.
    – fission
    Feb 24, 2010 at 9:03
  • Ohh, my bad, seems the gateway is the ip of the interface.. answer updated.
    – Grizly
    Feb 24, 2010 at 13:52
  • Right, which is what I am already doing, and what I was hoping to avoid. Thanks anyhow.
    – fission
    Feb 24, 2010 at 19:11

In windows you can add a route based on the interface without knowing the gateway by passing as gateway

this gives something like this:

route add <IPtoRoute> mask <MaskOfTheIp> IF <InterfaceNumber>

route add mask IF 2
  • 2
    I tried this on Windows 7 x64. It works! My command: route ADD IF 25 It returned OK! and a new entry appeared in routing table as expected
    – Dmitry
    Apr 10, 2013 at 17:56
  • FYI this is the right thing to do when trying to "clone" a route that has a gateway of On-link Dec 16, 2020 at 18:14

I got the same in Windows 7 Enterprise with the Juniper Junos Pulse VPN client.
I had a problem with this, as it captured all possible IPv4 addresses and routed these to the dial-up connection:

Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
         On-link       XX.XX.XX.XX     11
         On-link       XX.XX.XX.XX     11
         On-link       XX.XX.XX.XX     11
         On-link       XX.XX.XX.XX     11         On-link       XX.XX.XX.XX     11         On-link       XX.XX.XX.XX     11         On-link       XX.XX.XX.XX     11         On-link       XX.XX.XX.XX     11

I did not want all my traffic to pass through the VPN, so in case anybody needs it, I wrote a small cmd file to remove these routes and then install the only one I need ( without being able to specify a gateway, by specifying the right interface.
You can use this to dynamically retrieve an interface's number.

@rem Get the interface number
set IF=
for /f "tokens=1,8 delims=. " %%A in ('route print') do @if /i "%%B" equ "Juniper" set IF=%%A
@rem If interface is not found, terminate quietly
if not defined IF exit /b
for %%A in (1 2 4 8 16 32 64 128) do @route delete %%A.0.0.0
route add mask IF %IF%
  • 1
    you can just disable "force tunneling" in ip properties section of your vpn connection. Dec 9, 2014 at 15:15

The Interface number in decimal is displayed with route print. Look at the top of the output under Interface List.

Another way is to use arp -a and make note of the hexadecimal number, eg:

C:\>arp -a

Interface: --- 0xc  
  Internet Address      Physical Address      Type

Both are accepted after the if argument in route.exe, eg:

route ADD <NET-ID> MASK <mask> <GW-address or for on-link> IF 0xc -P

I prefer arp -a, as it is easier to identify the NIC.

Numerous other ways, but this is the simplest.


You can not omit gateway in permanent table. Some people offer putting there an interface card IP (user side), which was acceptable in Windows XP. But it is not valid any more. OS will keep knocking every network interface in this case, at least until cache is populated; it is not a good behavior and makes no difference with empty routing table.

I found out that putting there a destination IP as a gateway solves the problem at least on Windows 10. But I have little statistics at the moment to confirm it as 100.1% truth.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .