Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get netcat to accept connections from outside the LAN?

I'm using netcat as a backend to shovel data back and forth for a program I'm making. I tested my program on the local network, and once it worked I thought it would be a matter of simply forwarding a port from my router to have my program work over the internet. Alas! This seems not to be the case.

If I start netcat listening on port 6666 with:

nc -vv -l -p 6666,

then go to 127.0.0.1:6666 in a browser, as expected I see a HTTP GET request come through netcat (and my browser sits waiting in vain). If I go to my.external.ip.address:6666, however, nothing comes through at all and the browser displays 'could not connect to my.external.ip.address:6666'.

I know that the port is correctly forwarded, as www.canyouseeme.org says port 6666 is open (and when netcat is not listening, that its closed).

If I run netcat with -g my.adslmodem's.local.address to set the gateway address, I get the same behavior. Am I using this command line option correctly? Any insight as to what I'm doing wrong?

like image 653
Chris Avatar asked Mar 31 '10 11:03

Chris


2 Answers

You must listen on the public interface. You're currently listening on localhost where no one outside of your computer can connect.

Your milage may vary, but with my implementation of netcat I have to use this command line to listen on all the public interfaces.

netcat -vv -l 0.0.0.0 6666

like image 118
Kent Avatar answered Sep 17 '22 13:09

Kent


Turns out there is no problem. It only appears that way because my router doesn't allow hairpin connections. That is, even though I've got it set up correctly, the router wouldn't make the connection when both source and destination are behind the NAT. Simply ncat -l -p 6666 works fine, so long as the request comes from outside the LAN. To test this I browsed to my.external.ip.address:6666 with my 3G mobile phone and sure enough, a HTTP request came through :)

This answer came from: a serverfault question, which is where I should have asked this question in the first place. Apologies for that.

like image 23
Chris Avatar answered Sep 18 '22 13:09

Chris