Mapping one IP address to another in the 'hosts' file

The hosts file (%SystemRoot%\system32\drivers\etc\hosts) lets you make google.com point to, for example, 127.0.0.1 on your local machine, but what if I wanted to make, say, 192.168.1.5 point to 127.0.0.1 on my local machine? Is there a way to do that? Can that be done in the hosts file on Windows 7?


You can't use the hosts file for this.
This maps only names on IP addresses.

I haven't tried this myself, but I would guess that you can abuse the route command for this:

route add 192.168.1.5 127.0.0.1

This instructs the IP stack to route all traffic for 192.168.1.5 to the gateway at 127.0.0.1. Of course there is no gateway there so this only serves to blackhole traffic. You can't do anything useful with the traffic.


As everyone else has noted, you can't do this through the hosts file. You need some form of packet re-writing: the most common is Network Address Translation. NAT is normally used to permit multiple devices to use the same "public" IP address (and this is the only form that many cheap home routers can do), but a more comprehensive NAT engine such as is found in Cisco IOS or Linux IPTables can do generalized re-writing.

Note that NAT is not a cure-all solution for this: some protocols such as FTP include IP addresses within the data packets as well as in the IP header, and if the NAT engine does not handle such protocols specially, simply re-writing the header will cause things to break.


You can't map an IP address to another in a hosts file.

You can probably rewrite IP addresses in certain firewall software on some platforms (perhaps in Linux iptables or equivalent).