How to open port to outside world?
I have a remote server with an IP - 111.222.333.444 I want to run an http server on that machine, that runs on localhost:8000
How can I make requests to 111.222.333.444:6000 from outside, from my hope machine, to reach my http-server running on a localhost:8000 on a remote server.
I was using ufw.
Enabled ip_forwarding in /etc/sysctl.conf DEFAULT_FORWARD_POLICY in /etc/default/ufw
Tried this in /etc/ufw/before.rules
*nat
:PREROUTING ACCEPT [0:0]
-A PREROUTING -p tcp --dport 6000 -j REDIRECT --to-port 8000
COMMITIn the line -A PREROUTING -p tcp --dport 6000 -j REDIRECT --to-port 8000 does the absence of specified ip, where the traffic should come in, sets it to the default IP address of eth0 interface?
Using Ubuntu-20.04-amd64
If someone could tell, how would he write this thing, to achieve the same purposes. Through editing iptables, or ufw, etc.
Update:Started from the ground up. Http server is running on localhost:8000.
My ufw status shows this
To Action From
-- ------ ----
8000/tcp ALLOW Anywhere
6000/tcp ALLOW Anywhere
8000/tcp (v6) ALLOW Anywhere (v6)
6000/tcp (v6) ALLOW Anywhere (v6)Added lines into /etc/ufw/before.rules before *filter:
:PREROUTING ACCEPT [0:0]
-A PREROUTING -p tcp --dport 6000 -j REDIRECT --to-port 8000What should allow me to type in command line, on a remote servercurl localhost:6000 or curl 127.0.0.1:6000 and get the response, as i understand it.
But instead I'm receiving - curl: (7) Failed to connect to localhost port 6000: Connection refused .
Update:Changed /etc/ufw/before.rules from -A PREROUTING -p tcp --dport 6000 -j REDIRECT --to-port 8000 to
-A INPUT -i eth0 -p tcp --dport 6000 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 8000 -j ACCEPT
-A PREROUTING -t nat -i eth0 -p tcp --dport 6000 -j REDIRECT --to-port 8000Now with the new rules, when i'm making curl request from terminal, it hangs. Instead of producing error.
102 Answers
Port Forwading
Using Ngrok might help you if you want to access your localhost from the public internet. Ngrok 'exposes your local development server to the Internet', giving you a unique random subdomain url eg. abcdef.ngrok.io to access your localhost from. You can choose a custom domain if you own one (more info about custom domain is here).
NB: Unfortunately, you won't have to edit the IP tables with this method.
3Please, try these commands on the server:
$ rm -f /tmp/f ; mkfifo /tmp/f
$ nc -l 6000 </tmp/f | nc localhost 8000 >/tmp/fThis will redirect any TCP traffic coming to your server's 6000 port to the TCP port 8000 internally.
Please, note that this will work for a single connection only and the second command above will have to be repeated for each new connection. So, this is not actually a solution, but just a test to see that this is what you actually want to do.