How can I redirect "localhost" to another local IP?

If you’re on Windows, you can use the command:

netsh interface portproxy add v4tov4 listenaddress=192.168.1.101 listenport=21 connectaddress=192.168.1.102 connectport=21

Replace:

  • 21 with the port you want to forward,
  • 192.168.1.101 with your ip,
  • 192.168.1.102 with the destination ip.

Don't redefine the meaning of localhost. You'll break things in surprising ways.

Instead, look at the -L and -R options in ssh(1). They make it easy to redirect a port on the local machine to the IP address and port of a remote machine. Plus the tunnel is secured by SSH.

Note that these tunnels are not necessarily specific to ssh/shell traffic. It can be for arbitrary traffic.


Not sure if that is a smart thing to do. A lot of services intended to run on the local machine use either 127.0.0.1 (which would be no problem with your change) or localhost (in which case your change would mess things up) to connect to other services on the same machine. And a lot of other packages comes configured this way.

If the purpose is to have the same name to connect crosswise, regardless on which machine you are, I'd rather recommend to use a different name for this. Instead of "localhost" you could e.g. use "crosshost" - and with that follow the hints given by Indrek: Check for your hosts file (on Linux/Unix and probably also Mac it is /etc/hosts, on Windows it is C:\Windows\System32\drivers\etc\hosts (without extension -- so take care here, Windows usually already has some other hosts.* file in that directory). Do NOT comment out the 127.0.0.1 localhost line, nor modify it -- leave it as is. Just add a line

192.168.1.101 crosshost

on the 192.168.1.102 machine, and similarly with the other address on the other machine. Now you can e.g. ssh crosshost to connect to one machine from the other, or copy files over with scp filename.ext crosshost:/path/to/put, or whatever you intended.