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@
ssh: connect to host 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.



About these ads

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

  1. ssh_port22_victim

    you are genius!
    thank you

  2. Hi friend…. I get this error bash: /etc/init.d/sshd: No such file or directory
    i have folders upto /etc/init.d but sshd is not present..wat to do now…

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

  3. Gaurav Godhwani

    Thanks a lot.. ;)

  4. ‘$/etc/init.d/sshd start’
    -bash: $/etc/init.d/sshd start: No such file or directory
    please help

  5. sudo service ssh start

  6. Hi,

    I wanted to know how to check secure shell (ssh) connections on port 22 of system’s Ethernet interface ???

  7. hi,
    i am getting Permission denied .please try again later when i enter ssh localhost..

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


  9. @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).

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

  11. Pingback: ssh连不上vmware虚拟机ubuntu9.10 ubuntu10.10 | 罗生沙博客|java|开发工具|生活

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s