19

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?

5
  • If you are using, or can use, static IP addresses on your local machine, you can assign one network interface multiple IP addresses. Dec 28, 2014 at 22:17
  • @PatrickS. That works, but it can get messy if 192.168.1.5 really exists in the local LAN as well. Duplicate IP addresses on the LAN aren't nice...
    – Tonny
    Dec 28, 2014 at 22:19
  • @Tonny True. I'm assuming either it doesn't exist on the network, or OP is in a position to exclude two IPs on his DHCP server. Not always safe assumptions, I know. Dec 28, 2014 at 22:35
  • Also see: hosts file for ip address Dec 29, 2014 at 0:12
  • If you want to translate an IP address to a different IP address, you use NAT (Network Address Translation).
    – Ron Maupin
    May 6, 2016 at 0:53

3 Answers 3

15

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.

2
  • can you do this to redirect one ip address to another? say all outbound requests going to 185.23.38.85 should go to 185.23.38.82?
    – PirateApp
    Jun 26, 2022 at 11:11
  • @PirateApp That would probably only work if the pc itself, the original host and the redirected host are on the same op subnet.
    – Tonny
    Jun 26, 2022 at 15:59
3

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.

2

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).

You must log in to answer this question.

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