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

50,732

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
50,732

Related videos on Youtube

neubert
Author by

neubert

Apparently, this user prefers to keep an air of mystery about them.

Updated on September 18, 2022

Comments

  • neubert
    neubert over 1 year

    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?

    • Patrick Seymour
      Patrick Seymour over 9 years
      If you are using, or can use, static IP addresses on your local machine, you can assign one network interface multiple IP addresses.
    • Tonny
      Tonny over 9 years
      @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...
    • Patrick Seymour
      Patrick Seymour over 9 years
      @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.
    • Ƭᴇcʜιᴇ007
      Ƭᴇcʜιᴇ007 over 9 years
    • Ron Maupin
      Ron Maupin about 8 years
      If you want to translate an IP address to a different IP address, you use NAT (Network Address Translation).
  • Admin
    Admin almost 2 years
    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?
  • Admin
    Admin almost 2 years
    @PirateApp That would probably only work if the pc itself, the original host and the redirected host are on the same op subnet.