Why am I getting a "port 22: Connection refused" error?

Solution 1:

I went through this issue and finally got appropriate answer.

sudo apt-get update
sudo apt-get install openssh-server
sudo ufw allow 22

Then went into raspi-config in a terminal window and enabled ssh.

This will allow port 22 to be used for sshd.

Solution 2:

While on the server, check to make sure sshd is actually running, and is listening on port 22:

$ sudo netstat -anp | grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1538/sshd       
tcp6       0      0 :::22                   :::*                    LISTEN      1538/sshd       

If you don't get results, or they don't show you're listening on tcp 0.0.0.0:22... fix that.

If you DO show that sshd is running and is listening on tcp port 22, check to make sure there's no firewall rule getting in the way. Again, on the server:

$ sudo iptables -L | grep ssh
DROP       tcp  --  anywhere             anywhere            tcp dpt:ssh 

Or alternately,

$ sudo ufw verbose
Status: active

To                         Action      From
--                         ------      ----
22                         DENY        Anywhere
22/tcp                     DENY        Anywhere

If you do see a rule like one of the ones above, you'll need to fix that.

If you don't see any firewall rules in the way and you do see the service running on the server, then it's time to check your workstation, and the network it's connected to. Can you connect to other servers? Can you ping your own interface or loopback address? Etc.

Solution 3:

Try this

sudo apt-get remove openssh-client openssh-server

and then

sudo apt-get install openssh-client openssh-server

it worked for me :)

Probably not the most orthodox solution... :)