Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

telnet says Connection refused when you test a port

Errortelnet: Unable to connect to remote host: Connection refused

Short answer

Connection refused means the host answered and nothing was listening on that port, or a firewall rule sent a refusal. Log in to the server, run ss -ltn, and check that a service listens on the port and on the right address. Then check the firewall.

  • Applies to Ubuntu 24.04, OpenSSH 9.6, ufw 0.36
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    On the server, list the listening TCP sockets with numeric ports. Look for your port and for the address it is bound to.

    sudo ss -ltnp
  2. 2Match what you see, then jump to its fix

    1. 1the port is missing from ss outputNo service is listening on that portFix
    2. 2ss shows the port on 127.0.0.1, or another port numberThe service listens on a different address or portFix
    3. 3the port is listening locally, but connecting from another host is refusedA firewall rule rejects the connectionFix

What does the error look like?

  • telnet: Unable to connect to remote host: Connection refused
  • Connection refused

Cause 1 of 3

No service is listening on that port

1Check

The kernel reports a refusal when a connect finds no one listening on the remote address. Check whether the port is in the listing.

sudo ss -ltn 'sport = :22'

2Fix

Start the service that should own the port, and fix its own configuration if it exits on start.

sudo systemctl status ssh
sudo systemctl start ssh

3Verify

The port now shows up as LISTEN.

sudo ss -ltn 'sport = :22'

Confirmed by man7.org

Did this fix it?

Cause 2 of 3

The service listens on a different address or port

1Check

A service bound to 127.0.0.1 is reachable only from the same machine. For sshd, the ListenAddress and Port lines decide this; with no ListenAddress it listens on all local addresses and the default port is 22.

sudo ss -ltn
grep -E '^(ListenAddress|Port)' /etc/ssh/sshd_config

2Fix

Set the address and port you want in the service configuration, then restart the service. Use the port that ss shows in your telnet command.

# /etc/ssh/sshd_config
ListenAddress 0.0.0.0
Port 22

3Verify

ss lists the port on the address you expect.

sudo systemctl restart ssh && sudo ss -ltn 'sport = :22'

Confirmed by man7.org

Did this fix it?

Cause 3 of 3

A firewall rule rejects the connection

1Check

ufw can answer a blocked connection with a refusal when the rule is reject, instead of dropping it silently. Read the rules.

sudo ufw status numbered

2Fix

Allow the port, or delete the rule that rejects it. Add an allow rule for SSH before you enable ufw on a remote machine.

sudo ufw allow 22/tcp
# or remove a rule by its number
sudo ufw delete 3

3Verify

Connect again from the client.

telnet 203.0.113.10 22

Confirmed by manpages.ubuntu.com

Did this fix it?

How do I stop it coming back?

  • Check ss -ltn on the server before you blame the network.
  • Keep one note of which port and bind address each service should use.
  • Add the allow rule for a port in the same step that you open it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.