4

I saw many posts having same problem but without answer or real solution. I have raspberry pi 3 and I installed latest version of rasbian. I have neither monitor nor keyboard, so I would like to setup my raspberry through SSH. It's been 3 days I'm trying to connect but I'm always getting this error:

Connection closed by {ip} port 22

Any help would be greatly appreciated!

Niroda
  • 320
  • 1
  • 3
  • 13
  • The initial SSH configuration should be done with monitor and keyboard or preconfigured in the image (which is not the case apparently). – Yaron Jun 19 '16 at 11:11
  • Not necessarily!, I used rc.local to start SSH and I can see SSHD works using nmap and port 22 is open. Take a look at: https://www.youtube.com/watch?v=toWBmUsWD6M – Niroda Jun 19 '16 at 11:23
  • Since the port is probably open we should probably try another method of connecting to the machine and see why this is happening, you can see the failed attempts using this command: `cat /var/log/auth.log | grep 'sshd.*Invalid'`, of course you'll have to establish some sort of direct terminal session. – Yaron Jun 19 '16 at 11:32
  • I tried that but there is no log in auth.log, it's completely empty – Niroda Jun 19 '16 at 11:36
  • So can you access the machine? Can you try and run `tcpdump port 22`? that might give you some insights about the connection process. – Yaron Jun 19 '16 at 11:51
  • No I can't access the machine, I'm using linux to mount the SD card, but I can't use chroot for some reason. I can only edit system files in order to do something during startup. I tried to delete existing `/etc/ssh/ssh_host_*` and then `dpkg-reconfigure ssh-server` but it didn't work. – Niroda Jun 19 '16 at 12:07
  • If you didn't manage to `chroot`, what was the `dpkg-reconfigure` for? – Yaron Jun 19 '16 at 12:10
  • I meant I edited rc.local and added both delete and reconfigure to it, you know `rc.local` executes commands during startup :) – Niroda Jun 19 '16 at 12:14
  • Sure, yes the fact that is running is not critical in this case because we know it does, we need to see errors to understand what might cause this issue. – Yaron Jun 19 '16 at 12:27

2 Answers2

2

It seems newer versions of Raspbian disable ssh by default:

In the past, SSH was enabled by default, so people using their Pi headless could easily update their SD card to a new image. Switching SSH on or off has always required the use of raspi-config or the Raspberry Pi Configuration application, but to access those, you need a screen and keyboard connected to the Pi itself, which is not the case in headless applications. So we’ve provided a simple mechanism for enabling SSH before an image is booted.

The boot partition on a Pi should be accessible from any machine with an SD card reader, on Windows, Mac, or Linux. If you want to enable SSH, all you need to do is to put a file called ssh in the /boot/ directory. The contents of the file don’t matter: it can contain any text you like, or even nothing at all. When the Pi boots, it looks for this file; if it finds it, it enables SSH and then deletes the file. SSH can still be turned on or off from the Raspberry Pi Configuration application or raspi-config; this is simply an additional way to turn it on if you can’t easily run either of those applications.

So to enable sshd for one boot touch /mnt/raspi/boot/ssh.

To then enable sshd permanently you can run raspi-config and select to start SSHd. There should be a way to do that manually, too, but I haven't yet figured that out.

Frederick Nord
  • 1,246
  • 1
  • 14
  • 31
  • 1
    Note that the current firmware (2017-09-07) seems to be having an issue with this: https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=168310 – Salami Oct 02 '17 at 10:31
  • regarding that post above - what fixed it for me was: `sudo rm /etc/ssh/ssh_host_*` then `sudo dpkg-reconfigure openssh-server` on the pi box – JohnZaj Jun 17 '19 at 20:31
0

After many tries, I connected a monitor and discovered in the end that my image was broken. The raspberry stopped with a kernel panic.

Felix
  • 1,097
  • 1
  • 10
  • 16