port is allowed however connection is refued?

# sudo ufw allow 9999/tcp
  Rule added
  Rule added (v6)

When I check status

# sudo ufw status
  9999/tcp           ALLOW       Anywhere
  9999/tcp (v6)      ALLOW       Anywhere (v6)

Everything seems fine but connection is yet refused. I tried to telnet from local machine

# telnet 9999
telnet: Unable to connect to remote host: Connection refused

as well as from external machine (actual ip redacted)

# telnet 9999
telnet: Unable to connect to remote host: Connection refused

While other ports connections can be established but this newly added port isn't working. What could be the reason?

PS. server OS is Ubuntu 16.04

  • 1
    Do you have some process listening on port 9999? Commented Mar 9, 2018 at 2:30
  • when I run this: sudo netstat -tulpn | grep LISTEN, I don't see 9999 anywhere so it's not listening by any process Commented Mar 9, 2018 at 2:34
  • @GoodtheBest There you go. You need to listen on this port.
    – user147505
    Commented Mar 9, 2018 at 2:36
  • how do I do that? I opened port and tried to telnet as you can see above, connection is refused Commented Mar 9, 2018 at 2:37

2 Answers 2


You need to listen on that port in order to be able to connect to it. For simple testing, you can use nc or netcat:

nc -l -p 9999

Now nc is listening on port 9999 and you can telnet to this port from another terminal (or machine):

$ telnet localhost 9999
Trying ::1...
Connected to localhost.
Escape character is '^]'.

Once you connect, you can type in one terminal and after pressing Enter, the text should show up in the other terminal.


Let's ignore firewalls for a moment. When a client process tries to create a TCP connection to a host, that host needs to have some server process running on it that is "listening" for incoming connections on that port. If the port is not "open", meaning that no process is listening on the port, when the client process tries to connect, the kernel of the host will send back a response to indicate that there is no process listening on the port. When you see Connection refused on the client, that's what that means.

Enter the firewall. If a host has a firewall is place, the firewall can block network traffic going out of or coming into the host. It would then be possible that a client process tries to connect to a server process on a given port, and the connection be rejected by the firewall even if there was a server process listening on that port. That, however, is not the problem you're encountering.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .