SSH "connection refused" on Raspberry pi - cannot find why

  • I have the following problem SSH "connection refused". It would work fine, but while I am working on the SSH, I would get "connection refused" and would have to re-connect (which does not occur for good 5-10 minutes). I have tried everything (restart linux, restart ssh) yet still does not help. I've checked var/log as well but nothing helpful in there...

    NOTE: Interesting note I found is, when I try ssh to my external ip(277...), it goes to black cmd screen, I close this and try my 192.168.0.13 address and it lets me sign in.. but short while, again connection refused... Of course if I open my server and sudo sshd restart, my ssh "connection refused" goes away...

    My ssh port is opened at 23, and I have it set on modem/router as well

    [email protected]:~$ sudo netstat -tlpn
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1817/apache2
    tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      2227/vsftpd
    tcp        0      0 127.0.0.1:3350          0.0.0.0:*               LISTEN      1784/xrdp-sesman
    tcp        0      0 0.0.0.0:23              0.0.0.0:*               LISTEN      3115/sshd
    tcp        0      0 0.0.0.0:3389            0.0.0.0:*               LISTEN      1781/xrdp
    tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      2397/mysqld
    

    When I get disconnected, and log back in and check "WHO" I only see my previous connection, my root and the current connection

    [email protected]:~$ who  
    pi       tty1         2013-01-28 09:20
    pi       pts/0        2013-01-28 10:28 (192.168.0.12)
    pi       pts/1        2013-01-28 10:32 (192.168.0.12)
    

    This is what I have in my ssh_config that I have modified (SSH to 23 because rogers use 22...)

    #   Port 23
    ...    
    #Keep my damn connection alive!
    KeepAlive yes
    ServerAliveInterval 60
    

    This is what I get when I run arp-scan

    [email protected]:/etc$ sudo arp-scan -I eth0 -l | grep 192.168.0.13
    192.168.0.13    a0:6c:ec:ec:bb:5b       (Unknown)
    

    This part I found online that told me I should post the following two information

    [email protected]:~$ ps afxu | grep sshd
    root      2838  0.0  0.7   9800  3168 ?        Ss   10:27   0:00 sshd: pi [priv]                                                                                   
    pi        2845  0.0  0.3   9800  1628 ?        S    10:28   0:00  \_ sshd: [email protected]                                                                                   s/0
    root      2854  0.0  0.7   9800  3168 ?        Ss   10:32   0:00 sshd: pi [priv]                                                                                   
    pi        2861  0.0  0.3   9800  1628 ?        S    10:32   0:00  \_ sshd: [email protected]                                                                                   s/1
    root      2900  0.3  0.7   9800  3168 ?        Ss   10:44   0:00 sshd: pi [priv]                                                                                   
    pi        2907  0.0  0.3   9800  1628 ?        S    10:44   0:00  \_ sshd: [email protected]                                                                                   s/2
    root      2934  0.0  0.2   6204  1060 ?        Ss   10:45   0:00 /usr/sbin/sshd
    root      2954  0.7  0.7   9800  3164 ?        Ss   10:45   0:00  \_ sshd: pi [p                                                                                   riv]
    pi        2961  0.1  0.3   9800  1624 ?        S    10:46   0:00      \_ sshd: p                                                                                   [email protected]/3
    pi        2970  0.0  0.1   3536   796 pts/3    S+   10:46   0:00              \_                                                                                    grep sshd
    

    EDIT: [email protected]:~$ ssh -vvv 192.168.0.13 (THIS might be long... Thank you Bart Friederichs)

    [email protected]:~$ ssh -vvv 192.168.0.13 -p 23
    OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: Applying options for *
    debug2: ssh_connect: needpriv 0
    debug1: Connecting to 192.168.0.13 [192.168.0.13] port 23.
    debug1: Connection established.
    debug1: SELinux support disabled
    ...
    debug1: match: OpenSSH_6.0p1 Debian-3 pat OpenSSH*
    debug1: Enabling compatibility mode for protocol 2.0
    debug1: Local version string SSH-2.0-OpenSSH_6.0p1 Debian-3
    debug2: fd 3 setting O_NONBLOCK
    debug3: put_host_port: [192.168.0.13]:23
    debug1: SSH2_MSG_KEXINIT sent
    debug1: SSH2_MSG_KEXINIT received
    ...
    debug1: Next authentication method: publickey
    debug1: Trying private key: /var/www/.ssh/id_rsa
    debug3: no such identity: /var/www/.ssh/id_rsa
    debug1: Trying private key: /var/www/.ssh/id_dsa
    debug3: no such identity: /var/www/.ssh/id_dsa
    debug1: Trying private key: /var/www/.ssh/id_ecdsa
    debug3: no such identity: /var/www/.ssh/id_ecdsa
    debug2: we did not send a packet, disable method
    debug3: authmethod_lookup password
    debug3: remaining preferred: ,password
    debug3: authmethod_is_enabled password
    debug1: Next authentication method: password
    [email protected]'s password: - I TYPED PASSWORD -
    ...
    debug2: channel_input_status_confirm: type 99 id 0
    debug2: PTY allocation request accepted on channel 0
    debug2: channel 0: rcvd adjust 2097152
    debug2: channel_input_status_confirm: type 99 id 0
    debug2: shell request accepted on channel 0
    Linux raspberrypi 3.6.11+ #350 PREEMPT Mon Jan 7 21:51:11 GMT 2013 armv6l
    

    Run `netstat -tlpn` as root (or with `sudo`) if you want to see the program names/PIDs. Moving SSH from its default port doesn't seem to be a good idea if you're not sufficiently familiar with Linux administration.

    Lines started with # are comments, so in the line you specify the port 23 you have the # before, it will not be processed, and therefore it will use standard port (22). You can also have something in /etc/hosts.allow or /etc/hosts.deny controlling who can connect to sshd. Do you have checked if you don't have collision with ip address in your network?

  • Your sshd is listening on port 23, yet you are connecting to port 22 (ssh's default).

    Try this:

      [email protected]:~$ ssh -vvv 192.168.0.13 -p 23
    

    A "connection refused" error means the TCP layer in the kernel is not accepting any connections on that port. It has nothing to do with keeping alive or anything.

    Also, you are using arp to find out stuff, but a better tool would be nmap. arp only tells you if the IP is in your broadcast domain, not if any ports are open.

    Hello, Thank you for your reply, I've tried [email protected]:~$ nmap -bash: nmap: command not found... would I need to install it? and is there such for raspberry? Also thank you for the correction, I've edited with -p 23, but It doesn't have anything that looks out of place...

    `nmap` isn't really needed, just a handy tool. The update you made seems to have made it work?

  • Quoting the documentation:

    As of the November 2016 release, Raspbian has the SSH server disabled by default. You will have to enable it manually.

    To enable SSH on machines through console:

    Enter sudo raspi-config in the terminal, first select advanced options, then navigate to ssh, press Enter and select Enable or disable ssh server.

    To enable SSH for headless machines:

    For headless setup, SSH can be enabled by placing a file named ssh, without any extension, onto the boot partition of the SD card.


    Copy-pasted from @techraf's answer in this related question: SSH not working with fresh install.

    Whilst this might technically answer the question, I'm not sure techraf is going to be happy that you have just copy and pasted their answer. However, if you asked for their permission before making this post that would be different.

    Part of the "terms of service" (sec. 3) is that contributions here are considered to have a Creative Commons Attribution ShareAlike license, and this re-appropriation is within those terms. *However, it's not much of an answer here since it is pretty clear in the question that `sshd` is running, and the issue was prior to 2016.*

    @DarthVader I posted this as a community answer, properly crediting the author, I thought it would be enough.

    @goldilocks As you may have guessed, this question comes on the top of Google search results about ssh connexion refused for Raspberry Pi. I believed this would help people to figure out faster what could be the issue.

    On the current newest (Raspbian Stretch) SSH configuration is located under 5) Interfacing Options -> P2) SSH

  • I had the same problem this morning, and got it fixed by removing and installing openssh-server:

    sudo apt-get remove openssh-server 
    sudo apt-get install openssh-server
    

    Hello and Welcome to Stack Exchange! Please consider adding a more in depth explanation for the benefit of future readers.

  • I had the same problem and my solution was to disable and uninstall iptables.

    run those commands:

    (as sudoer)

    iptables -F iptables -X iptables -t nat -F iptables -t nat -X iptables -t mangle -F iptables -t mangle -X iptables -P INPUT ACCEPT iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT

    sudo apt-get remove --purge iptables

  • For me the following worked: went to /etc/ssh/ssh_config and /etc/ssh/sshd_config and then allowed access with no password.

    Restarted the service and voilà, working!

  • Updating Delgan's answer for Raspberry pi 3, RASPBIAN JESSIE WITH PIXEL OS

    In terminal

    sudo raspi-config 
    

    Select Interfacing options -> SSH. Press Enter and select Enable or disable ssh server

  • I am not sure if my tip is going to be helpful. I had the same problem and I am new to linux world. After reading Raspberry Pi documentation I found that the issue is in Pi Configuration where SSH was Disabled.

    Click on Raspberry Pi Menu --> Preferences --> Launch Raspberry Pi Configuration Navigate to the Interfaces tab Select Enabled next to SSH Click OK This resolved my issue. Please try and let me know.

  • with

    [email protected]:~$ sudo netstat -tlpn
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1817/apache2
    tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      2227/vsftpd
    tcp        0      0 127.0.0.1:3350          0.0.0.0:*               LISTEN      1784/xrdp-sesman
    tcp        0      0 0.0.0.0:23              0.0.0.0:*               LISTEN      3115/sshd
    tcp        0      0 0.0.0.0:3389            0.0.0.0:*               LISTEN      1781/xrdp
    tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      2397/mysqld
    

    It is a good start to check what the problem is, but not enough to answer the question :-(

  • I had the same problem but is was resolved after I ran the command

    sudo apt-get remove --purge iptables. and switching off the wireless connectivity.

  • There are 2 configuration files /etc/ssh/ssh_config and /etc/ssh/sshd_config

    Change the port from 22 to whatever you want in both files.

    restart the service sudo service ssh restart

    But... that will only work if your ssh:ing into the same machine. What would be the point of that?

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM