Score:2

Why am I getting "Connection refused"?

vn flag

I am running a hello-world http server on an ubuntu EC2 instance, let's say, myurl.com. I am unable to curl it from my client:

$ curl myurl.com:4296                     
curl: (7) Failed to connect to myurl.com port 4296: Connection refused

When I try to reach any other port, my connection is timed out:

$ curl myurl.com:4244                   
curl: (7) Failed to connect to myurl.com port 4244: Operation timed out

I have the following inbound rule on AWS:

enter image description here

I am able to curl it on the server:

$ curl localhost:4296
Hello World

My netstat:

$ netstat -a | grep 4296
tcp        0      0 localhost:4296          0.0.0.0:*               LISTEN   

What am I doing wrong?

Michael Hampton avatar
cz flag
Does this answer your question? [What causes the 'Connection Refused' message?](https://serverfault.com/questions/725262/what-causes-the-connection-refused-message)
MLu avatar
id flag
MLu
@MichaelHampton none of the answers to that linked question mention the reason for the OP's problem - his program listening on the localhost address only. This is not a duplicate.
Score:2
id flag
MLu

The process on port 4296 listens only on the localhost / 127.0.0.1 address and therefore is not accessible from outside. You have to change the configuration (or the program itself if it's one that you wrote) to listen on 0.0.0.0 - that will make it listen on all addresses.

Here's an example from my system:

~ # netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address    Foreign Address   State       PID/Program name    
tcp        0      0 0.0.0.0:22       0.0.0.0:*         LISTEN      1311/sshd           
tcp        0      0 127.0.0.1:631    0.0.0.0:*         LISTEN      1183/cupsd          

Here SSH listens on port 22 on all addresses and is therefore accessible from outside (if firewall and SG permits of course).

On the other hand CUPS listens on port 631 only on the localhost (127.0.0.1) and even if firewall / SG allowed this port it wouldn't be accessible from outside.

Hope that helps :)

Score:-1
ke flag

Check whether are you able to access it with public up. If you are able to then you need to check whether route53 is correctly configured. If you can't with public ip also then add the firewall rule to allow the traffic. https://linuxdiaryblog.blogspot.com/2020/06/add-firewall-exception-to-port.html?m=1

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.