How map/forward port under Ubuntu for other machine ? (localhost 555 -> 192.168.0.21:555)

13,931

Solution 1

I think iptables is what you're looking for and it should already be installed with Ubuntu.

It may take a bit of trial and error but something like the commands below should do the trick:

iptables -A PREROUTING -t nat -i eth1 -p tcp --dport 555 -j DNAT --to 192.168.0.21:555
iptables -A INPUT -p tcp -m state --state NEW --dport 555 -i eth1 -j ACCEPT

A more detailed explanation of what these commands do can be found here.

Another method is to use a program called rinetd which is available in Ubuntu via synaptic.

Redirects TCP connections from one IP address and port to another. rinetd is a single-process server which handles any number of connections to the address/port pairs specified in the file /etc/rinetd.conf.

There's a nice guide on how to use it here

Solution 2

http://www.frozentux.net/iptables-tutorial/chunkyhtml/x4033.html

You think this should be enough by now, and it really is, unless considering one final aspect to this whole scenario. What if the firewall itself tries to access the HTTP server, where will it go? As it looks now, it will unfortunately try to get to its own HTTP server, and not the server residing on $HTTP_IP. To get around this, we need to add a DNAT rule in the OUTPUT chain as well. Following the above example, this should look something like the following:

iptables -t nat -A OUTPUT --dst $INET_IP -p tcp --dport 80 -j DNAT --to-destination $HTTP_IP

Solution 3

Use redir instead of iptables: http://manpages.ubuntu.com/manpages/lucid/man1/redir.1.html

Share:
13,931

Related videos on Youtube

marc
Author by

marc

Updated on September 17, 2022

Comments

  • marc
    marc almost 2 years

    Any idea how I can create a "virtual" listening port on my Ubuntu computer porting for remote IP?

    I mean, something like this.

    When I write telnet 127.0.0.1 555, I want to get a connection to computer 192.168.0.21 on port 555 (where I have my server).

    Any idea?

  • marc
    marc about 14 years
    But it won't bind local port. I'm looking for "port tunneling" I don't want access that port from LAN but from local machine. On computer where i want forward it, i want say telnet localhost 555 and it should connect me to remote computer called 192.168.0.21. On windows it's called port tunell. steelbytes.com/?mid=18
  • user2661503
    user2661503 about 14 years
    I would have thought IP tables would work by telneting 127.0.0.1:55 but you could also try a program called rinetd which can redirect ports too. I've added details to the answer above.