Solution for “ssh: connect to host localhost port 22: Connection refused”


Hi friends,

Since three days, i had problem with my ssh connection. When i was tried to connect my laptop to others machine,  I’m able to connect with them. But when i tried to connect, others machine to my laptop, I got this Error.

[student@mercury ~]$ ssh suresh@192.168.1.9
ssh: connect to host 192.168.1.9 port 22: Connection refused

Solution for this “port 22: Connection refused” :

First check if sshd is running by trying the command ‘$ pgrep sshd
on the host. If the output is empty, its very likely that sshd is not
running. Try ‘tail -f /var/log/messages’ on one terminal and do
a ‘$ /etc/init.d/sshd start‘ on another terminal.  Then, check the
output that comes up on the previous terminal.

 

if PID comes, like 1702(any number), we conform the “sshd” is running.

Next, we have to flush the “iptables” by using the following command,

$ sudo iptables -F

That’s all. Now you can connect with anyone via ssh.

If again the same problem appear, Try ‘$ grep sshd /var/log/messages | tail‘ and
$ grep ssh /var/log/secure | tail‘ and check for any possible issues.

🙂

 

Advertisements

13 thoughts on “Solution for “ssh: connect to host localhost port 22: Connection refused”

    • Dear Theopaul,

      I think, you didn’t install “openssh-server” in your system. Which distro you are currently using? If you have any more doubts feel free to contact me 🙂

  1. please help….
    root@nagios-desktop:/# ssh localhost
    root@localhost’s password:
    Permission denied, please try again.
    root@localhost’s password:

    root@nagios-desktop:/#

  2. @valentin: If you did not find out yet, sshd is usually configured to block root login via ssh. So either allow it in /etc/ssh/sshd_config (insecure option) or ssh as a normal user and use sudo / su / sudo bash (depending on your distro).

  3. Hi Suresh,

    I install Cgywin and new to linux,…when i am giving ssh localhost command i got the same port 22 error and when i tried to give the “pgrep” command it gives me below error –
    – bash : pgrep : command not found

    please help.

  4. Pingback: ssh连不上vmware虚拟机ubuntu9.10 ubuntu10.10 | 罗生沙博客|java|开发工具|生活
  5. Hi all,
    I have also come across the similar problem mentioned above,I need to study mininet-vm and I am using Ubuntu-16.04 LTS machine 64 bit, actually I also installed both open-ssh-server and open-ssh client in my computer.While I am able to connect to some machines like Ubuntu in VirtualBox or any other physical computer, I downloaded mininet-vm and then imported it in virtualbox,after that a neccessity appears like ssh connection between mininet-vm which is a Linux like machine and my local computer.All these have open-ssh-server or client.I can be able to reach via ssh from mininet-vm to my local,however,I can not reach to mininet from my local.How can I solve such a problem ssh: connect to host 192.168.56.195 port 22: No route to host?
    I also added a forwarding rule to sth like this
    HostIp host-port Guest-Ip Guest-Port
    192.168.56.195 2222 192.168.1.123 22
    I also tried removing ssh and then reinstall it.Both conditions did not work for me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s