Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am getting a

port 22: Connection refused 

error while connecting to a server.

I have both the openssh client and server installed, and they are running. But still there is an error. Please help.

share|improve this question
Just to clarify, is openssh server installed on the server? – Kevin Nov 17 '12 at 3:46
Check ssh is running on port 22 and if you have a firewall rule blocking the port. – Manula Waidyanatha Nov 17 '12 at 3:46
According to me , Configure firewall such that it allows to ssh (port 22 ) which is currently blocked by firewall. – K.K Patel Nov 17 '12 at 3:53
sudo ufw enable… – user548701 May 26 at 4:38

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    *               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 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.

share|improve this answer
and if you can connect to other servers? – opensourcechris Apr 25 '13 at 20:43

I went through this issue and finally got appropriate answer.

sudo apt-get update

sudo apt-get install openssh-server

sudo ufw allow 22

This will allow port 22 to be used for sshd.

share|improve this answer

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... :)

share|improve this answer
Using dpkg-reconfigure would possible be easier to use in lieu of an uninstall/reinstall solution – Stephen Michael Kellat Feb 23 '14 at 4:06
I had to use apt-get purge openssh-server (instead of remove) to get it to work. – 11101101b May 14 at 15:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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