Out of nowhere, my SSH client decided to stop connecting to any server (whether it is an SSH server or not) and output the following error:

ssh: connect to host x.x.x.x port 22: Connection refused

Mind you, this is not a simple setup error. I am fully aware that an SSH server is running on hosts that I try to connect to, evident by the following:

➜  ~ sudo nmap -sS

Starting Nmap 7.01 ( https://nmap.org ) at 2018-04-01 10:58 IST
Nmap scan report for
Host is up (0.0035s latency).
Not shown: 996 closed ports
22/tcp  open  ssh   <========================
80/tcp  open  http
81/tcp  open  hosts2-ns
443/tcp open  https
MAC Address: B8:27:EB:7C:24:64 (Raspberry Pi Foundation)

Nmap done: 1 IP address (1 host up) scanned in 0.47 seconds

I can also telnet into it successfully (no "connection refused" message)

➜  ~ telnet 22
Connected to
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4p1 Raspbian-10+deb9u2


➜  ~ ssh srv@
ssh: connect to host port 22: Connection refused

This shows that it is not an error with my server, or even my machine per se, but with the SSH client as it is unable to connect to a port which is clearly open and an SSH service port. Interestingly, it fails with ANY address, regardless of whether it's even reachable or not, which further makes me think this is a problem with my SSH client and not firewall:

➜  ~ ssh
ssh: connect to host port 22: Connection refused
➜  ~ ssh
ssh: connect to host port 22: Connection refused
➜  ~ ssh
ssh: connect to host port 22: Connection refused
➜  ~ ssh
ssh: connect to host port 22: Connection refused
➜  ~ ssh
ssh: connect to host port 22: Connection refused

How is this even possible? SSH doesn't even attempt the connection and claims "connection refused". What can possibly be the culprit?

Additional info:

➜  ~ uname -a
Darwin MacBook-Air.local 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64
  • I assume you are blocking outgoing port 22 traffic on your machine. Or, your router is dropping SSH coming from your IP address. You could try and configure one of the servers to listen on a different port, then connect to it in order to test. – fragamemnon Apr 1 '18 at 8:00
  • @fragamemnon This was my first thought as well, but it is only SSH which fails. Telnet and Nmap both work with this port. – Joseph Apr 1 '18 at 8:41
  • Check type ssh to see if you have a bad alias, function, or imposter program shadowing the real ssh -- or if you know the correct full path (I don't for Mac) use that. – dave_thompson_085 Apr 1 '18 at 9:07

It turned out to be an easy fix. If there is anyone else going through this, consider the following...

Earlier, I had enabled SOCKS proxying in the macOS Network Preferences > Advanced ... > Proxies > SOCKS Proxy. It looked like this:

enter image description here

Long story short, this server was no longer running, so proxy connections to loopback were unsuccessful. The reason Chrome, Telnet, and Nmap worked was because they did not respect the macOS SOCKS proxy setting. Any applications which actually respected these settings (such as ssh) were unable to access the Internet, which is why SSH connections were failing.

I'm not 100% sure why SSH came up with "connection refused" instead of some generic "unreachable" error, but I know that the SOCKS proxy was the reason. If the same happens to you, try checking your proxy settings!

  • As the socks proxy was not running, any attempt to connect to it resulted in "connection refused". Whatever the reason, ssh mistakenly reported it against target host rather than socks proxy itself (probably because it was NOT aware of it). – Tomek Dec 13 '18 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.