Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a development server, which is only accessible from, not 192.168.1.x:8000. As a quick hack, is there a way to set up something to listen on another port (say, 8001) so that from the local network I could connect 192.168.1.x:8001 and it would tunnel the traffic between the client and

share|improve this question
netcat can do this. – Andy Apr 1 '11 at 5:25
up vote 13 down vote accepted

Using ssh is the easiest solution.

ssh -g -L 8001:localhost:8000 -f -N user@remote-server.com

This forwards the local port 8001 on your workstation to the localhost address on remote-server.com port 8000. -g means allow other clients on my network to connect to port 8001 on my workstation. Otherwise only local clients on your workstation can connect to the forwarded port. -N means all I am doing is forwarding ports, don't start a shell. -f means fork into background after a successful SSH connection and log-in. Port 8001 will stay open for many connections, until ssh dies or is killed. If you happen to be on Windows, the excellent SSH client PuTTY can do this as well. Use 8001 as the local port and localhost:8000 and the destination and add a local port forwarding in settings. You can add it after a successful connect with PuTTY.

share|improve this answer

With socat on the server:

socat tcp-listen:8001,reuseaddr,fork tcp:localhost:8000
share|improve this answer
Thanks, this is great since you do not have to have a local ssh server running. – jontro Dec 15 '15 at 15:11

OpenBSD netcat is available by default on Ubuntu and also on OS X.

mkfifo a
mkfifo b
nc 8000 < b > a &
nc -l 8001 < a > b &

An alternative that works on OS X bash is to use a bidirectional pipe. It may work on other Unixes:

nc 8000 <&1 | nc -l 8001 >&0
share|improve this answer
I didn't notice at first that you were using openbsd netcat. This is better than having to install another netcat from an Ubuntu package. – RobertR Feb 26 '15 at 15:24
OpenBSD example failed on Ubuntu 15.04. With the shell redirects, netcat fails to open the port for listening as seen by ss -tan or netstat -tan. – Justin C Sep 3 '15 at 0:12

Using the traditional nc is the easiest solution:

nc -l -p 8001 -c "nc 8000"

This version of nc is in the netcat-traditional package on Ubuntu. (You have to update-alternatives or call it nc.traditional.)

Note that in contrast to ssh this is not encrypted. Keep that in mind if you use it outside one host.

share|improve this answer
iptables -t nat -A PREROUTING -p tcp --dport <origin-port> -j REDIRECT --to-port <destination-port>

service iptables save
service iptables restart
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.