58

So I am trying to get ssh working on my server. I can connect on my local network with the local ip address of the server perfectly fine. When I try to connect remotely (through a domain name that I set-up for the server, which I know works) I get this connection refused error:

ssh: connect to host bahhudson.mine.nu port 22: Connection refused

From the research I have done into the problem it looks like something is going wrong with my router. I have configured port-forwarding, but that's the only think I can think is causing the "Connection Refused" error... My router is a WRT120N and I have configured it for port-forwarding based on Portforward.com, which seems somewhat outdated since some of those images are not exactly like what my router settings look like, but I have tried multiple ways for setting the portforwarding up, and I know that forwarding for port 80 works because my server has HTTP access which is working...

Also VPN access was getting blocked in what I believe to be a similar way, so the solution to the SSH port 22 being blocked might also be similar to that of the VPN being blocked as well.

Any ideas?

Thanks!

10 Answers 10

42

Just to share. (might be with different configuration)

In my case, I found that that error can happen because I have not install openssh-server in the other machine.

After I install openssh-server in the other machine, the problem:

ssh: connect to host 192.168.XXX.XXX port 22: Connection refused

is solved.

2
  • 1
    It seems silly to forget this but it was my case! This answer should be the first check before trying other tricks
    – Garini
    May 27, 2022 at 9:03
  • Same case with me. I have not installed in my host computer ssh sevice (sudo apt install openssh-server). Once did, ssh-copy-id -i id_rsa.pub [email protected] did not throw any error. I ws copying ssh key to the host in from the Docker container it was hosting using ssh.
    – yogender
    Nov 27, 2022 at 13:53
37
  1. First check openssh-server installed in that system.

  2. check the status of ssh service, make ssh service start.

     sudo service ssh status    
     sudo service ssh start
    
  3. Check whether port 22 in that system is blocked by iptables. Just allow port in iptables and then check.

     sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT
    
  4. Else change port number of ssh from 22 to 2222 by editing

     vi /etc/ssh/sshd_config         
     /etc/init.d/ssh restart.
    
2
  • Thanks! It worked for me! This should be the accepted solution! Oct 16, 2020 at 13:04
  • in my case ssh was failing while starting the service. This error was the output: sshd: no hostkeys available -- exiting and this was the fix: ssh-keygen -A
    – Garini
    May 27, 2022 at 9:25
28

Your ISP may block connections to port 22 (nothing you or your router can do about it). Just set SSHd to run on a different port, e.g. 2222.

In /etc/ssh/sshd_config, change Port 22 to Port 2222 and then sudo service ssh restart. Port forward 2222 (or whatever), and try again.

6
  • I still get the same connection refused error (22 when I don't specify a port when using ssh and 2222 when I use -p 222). I put incoming port 22 to forward to 2222 and changed the port to 2222 in sshd_config. Am I leaving out something?
    – bahudso
    May 31, 2012 at 1:35
  • 1
    Just try this: Incoming 2222 on router has to forward to 2222 (with port 2222 set in sshd_config).
    – ish
    May 31, 2012 at 1:45
  • @izx kudos for the router rules. At some point I thought that I was freaking out and don't know what I'm doing. LOL!
    – GTodorov
    Nov 9, 2017 at 17:43
  • For future readers. Make sure that you are referring /etc/ssh/sshd_config (notice sshd) file. After pulling my hair for an hour I came to know this small difference. ;P
    – Shashanth
    Sep 19, 2018 at 11:57
  • My new router uses port 22 for it's remote administration feature and doesn't list it anywhere on the interface, so it may not necessarily be the ISP. Oct 8, 2018 at 22:48
9

I solved this problem by reinstalling openssh-server as follows:

sudo apt-get purge openssh-server 
sudo apt-get install openssh-server
2
  • 1
    On client or server ? Sorry, I don't know much.
    – user227495
    Feb 15, 2020 at 2:43
  • On client...... Jul 1, 2021 at 15:18
7

Check the SSH service status and start the SSH service if it is stopped:

sudo service ssh status

In my case SSH was stopped, but after starting the service:

sudo service ssh start

I can be able to remote login to Linux.

1

I got the same error, but, after restarting my computer, everything worked again.

So, you may want to give it a try before reinstalling some components.

0

The other likely cause is that the proxy in between the terminal-session and the host-server needs userid-password access, that it is rejecting to allow the traffic flow, this is due to a network-firewall or ssl-socks etc.

1
  • This does not seem like a 'likely cause', especially given the accepted answer.
    – belacqua
    Jul 8, 2013 at 20:33
0

The solution will be by adding the port number of your server preceded with "-p":

Example of the command line:

ssh -L localport:127.0.0.1:localport username@ServerIP **-p** **serverport**
0

Just go to whm then open Host Access Control Change as follows

sshd ALL allow

0

If it's your own private server, your best course of action is to start simple and not complicate things. Since you cannot access your machine via SSH, you can login to your provider hosting the Ubuntu server and reboot the machine from there. After rebooting, try logging back in via console (or putty) with ssh user@ip-address again.

You must log in to answer this question.

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