telnet says Connection refused when you test a port
telnet: Unable to connect to remote host: Connection refusedShort 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.
Fix it fast
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 -ltnp2Match what you see, then jump to its fix
What does the error look like?
telnet: Unable to connect to remote host: Connection refusedConnection 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 ssh3Verify
The port now shows up as LISTEN.
sudo ss -ltn 'sport = :22'Confirmed by man7.org
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_config2Fix
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 223Verify
ss lists the port on the address you expect.
sudo systemctl restart ssh && sudo ss -ltn 'sport = :22'Confirmed by man7.org
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 numbered2Fix
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 33Verify
Connect again from the client.
telnet 203.0.113.10 22Confirmed by manpages.ubuntu.com
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 fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
Apache warns that a ${VAR} config variable is not defined
${VAR} left unchanged in the configuration, with a warning in the error log
Apache expands ${VAR} from Define or from the shell environment that exists when the server starts.
3 causes, each with a fix