If I wanted to route a certain IP address to my default gateway, could I do it both using routing in the Windows Command Prompt, as well as the hosts file?

Is there a difference?



The HOSTS file and route command functions are different from each other and one cannot be used to accomplish what the other does.

The HOSTS file lets you map a specific host name such as www.google.com to a specific IP address. The route command lets you specify which gateway to use when trying to reach a certain IP address (or more accurately, a certain network).

In your case, you'll want to use the route command, not the HOSTS file.

It's worth noting that all IP addresses that are not on your local subnetwork are automatically routed through your default gateway. You do not need to manually add a route to accomplish this.

  • There is an exception: if for some reason an IP address on the subnet is assigned to a machine on the other side of the gateway, you have to directly specify that that's the case (I'm having trouble thinking why it should be, but it probably could be in theory).
    – cpast
    Dec 23 '14 at 19:42
  • 1
    I can't think of a legitimate configuration where two network segments wishing to communicate would be configured with conflicting subnetwork addresses. If I ran across anything like this, I'd regard it as something to be fixed, not worked-around. Dec 23 '14 at 20:08
  • @Twisty my local network (behind NAT) uses 192.168.2.x ; I communicate via ssh tunnel with another local network (also behind NAT) using 192.168.2.x . Neither end really wants to bother changing their subnet , and so far routing specific addresses over the tunnel works for me :)
    – M.M
    Dec 23 '14 at 22:52
  • @Matt Very good point. I would still deem such a configuration contrary to proper network design, but I shouldn't too hastily dismiss the case where good design and the real world don't see eye to eye. Dec 24 '14 at 1:15

route a certain IP address to my default gateway

This is what the above means: You are telling your TCP/IP stack that the "certain IP address" is reachable via your default gateway.

So your TCP/IP stack, used by all applications on your system, will send ANY traffic with that destination IP address to your default gateway.

Routing rules do not have any concept of domain name or DNS. They work with IP addresses and subnet masks only.

Your hosts file is used by a DNS resolver - which may lie in various libraries/dll's on your system.

What is supposed to happen is that the DNS resolver, which is normally called anytime an application wants to find the IP address of a domain name, will look in this file first to convert the domain name to an IP address. If it's not there, it queries a DNS server.

(This behavior is configurable on Linux and likely Windows - on Linux you can tell it to not consult the file or consult the file second if querying a DNS server fails first).

If this DNS lookup does not happen, the hosts file will not have any effect.

The IP returned is only used by the application that requested it. Routing rules can still have an effect once the application actually sends something to this IP.

So you cannot use your hosts file for the same purpose as setting your default gateway.


The hosts file is used to map a hostname (such as localhost or superuser.com) to an IP address. It involves DNS, not routing - so you can't use the hosts file.

I can't talk specifics on how you would use a static route to accomplish what you want to do, however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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