Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 5 down vote accepted

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.

share|improve this answer
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 '10 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 '10 at 7:29
I get the same result w/o the "metric 1 if 2" part. – fission Feb 24 '10 at 9:03
Ohh, my bad, seems the gateway is the ip of the interface.. answer updated. – Grizly Feb 24 '10 at 13:52
Right, which is what I am already doing, and what I was hoping to avoid. Thanks anyhow. – fission Feb 24 '10 at 19:11

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

this gives something like this:

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

route add mask IF 2
share|improve this answer
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 '13 at 17:56

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%
share|improve this answer
you can just disable "force tunneling" in ip properties section of your vpn connection. – strange walker Dec 9 '14 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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.