121

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.

7
  • 3
    Just to clarify, is openssh server installed on the server?
    – Kevin
    Commented Nov 17, 2012 at 3:46
  • Check ssh is running on port 22 and if you have a firewall rule blocking the port. Commented Nov 17, 2012 at 3:46
  • 1
    According to me , Configure firewall such that it allows to ssh (port 22 ) which is currently blocked by firewall.
    – KK Patel
    Commented Nov 17, 2012 at 3:53
  • sudo ufw enable askubuntu.com/questions/417998/…
    – user548701
    Commented May 26, 2016 at 4:38
  • This could occur probably because of temporary network loss too. So check your internet connection.
    – user648610
    Commented Jan 30, 2017 at 16:43

7 Answers 7

124

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.

4
  • Same problem here as @SDsolar
    – Nathan G
    Commented Apr 29, 2018 at 11:23
  • @Nadiv, I also had to use raspi-config to enable ssh. Answer has been edited to reflect this.
    – SDsolar
    Commented Apr 29, 2018 at 15:50
  • 1
    Performed the first three lines on both computers and the file is now successfully transferred. Why does Ubuntu not come with these installed/enabled by default? The connection refused error is so cryptic, I had no idea if it was a firewall issues, username issue, IP address issue, etc etc. Very frustrating.
    – Hatefiend
    Commented Jun 20, 2019 at 1:53
  • sudo ufw allow 22 worked once. Again the error started appearing.
    – vineeshvs
    Commented Sep 5, 2019 at 7:57
37

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.

6
  • 1
    and if you can connect to other servers?
    – OpenCoderX
    Commented Apr 25, 2013 at 20:43
  • 2
    Hey thanks for answer, mine is not port 22, its 2222. You said, fix that, but how?
    – Yogesh D
    Commented Mar 11, 2017 at 18:50
  • Mine gave no results.
    – SDsolar
    Commented Jun 23, 2017 at 21:20
  • fix it with: sudo ufw allow 22 (or whatever your port is)
    – John D
    Commented Jan 19, 2018 at 4:50
  • No firewall rules in the server. But still SSH is allowed from the PCs in the local network and not from outside our lab (but in the same institute network)
    – vineeshvs
    Commented Sep 5, 2019 at 7:59
18

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

2
  • 3
    Using dpkg-reconfigure would possible be easier to use in lieu of an uninstall/reinstall solution Commented Feb 23, 2014 at 4:06
  • 1
    I had to use apt-get purge openssh-server (instead of remove) to get it to work.
    – 11101101b
    Commented May 14, 2016 at 15:01
9

Came across same problem after installing Raspbian. Solution that worked for me:

sudo apt-get purge openssh-server
sudo apt-get install openssh-server
3
  • 1
    If this worked you must have changed your sshd-config before.
    – guntbert
    Commented Dec 22, 2016 at 21:29
  • I edited sshd_config on Raspbian while trying to setup ssh. This solution worked for me.
    – Scott
    Commented Jan 9, 2017 at 20:45
  • 1
    +1. Only solution that worked!
    – Nathan G
    Commented Apr 29, 2018 at 11:29
6

The following commands worked for me:

cd /root/.ssh
vi known_hosts

Now delete everything in that file and enter on the terminal:

service sshd restart

Source: SSH - Connection Refused

2
  • 1
    similar problem for me, but I cleared my known_hosts with ssh-keygen -f "/home/MYUSER/.ssh/known_hosts" -R MYHOST, substituting in my linux user and remote host I was trying to connect to
    – Chris
    Commented Apr 20, 2018 at 16:52
  • What are known_hosts and why does knowing some hosts prevent the machine from connecting? Commented Mar 12, 2023 at 16:48
2

The static IP was wrong in my case. Found out by doing ping <my_ip> after switching off the server. It was still pinging even when the server was off. Correcting the IP solved the issue.

1

This might fix it for you as well. Try port forwarding connection 22 from your outgoing IP address to your local IP on the same port. Worked for me allowing me to ssh in to remote ubuntu computer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .