How to get SSH access to headless Raspbian Stretch Lite? Official way seems not to work

5,385

Solution 1

Finally i was able to find an alternate way to enable ssh. I will try to summarize it here. I tried PiBakery also but it was too heavy with nodejs and keeps on crashing in every attempt.


The problem: Ext4 format is not readable form windows

So once you burn Raspbian through any img burning tool it creates two partition Boot & rootfs. The boot partition is formatted in FAT and will be only visible to windows whereas the rootfs was with Ext4 and will not be visible at all (you can confirm it by viewing it in MiniTool partition Partition Wizard)


Mission: Editing the configuration from windows to force enable ssh

My mission was to force enable ssh as it was not working with the official instructions (putting ssh file in the boot partition). So I downloaded the Paragon ExtFS for Windows which enables Windows 7 to browse/edit files in Ext4partition by mounting it as new drive.

Before that i also tried Ext2Fsd-0.69 but it was not compatible with Windows 7 and the other ext2explore-2.2.71 is only capable to view files not to edit them. Nothing worked for me


The hack: Edit /etc/rc.local file from windows 7

After googling for almost the whole day i got to know two thing

  1. Commands can be executed by editing the file /etc/rc.local in the rootfs partition
  2. Command to start ssh is /etc/init.d/ssh start

So i edited the rc.local file by placing the /etc/init.d/ssh start just before the exit 0 so that it executes on every boot, as follows

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

/etc/init.d/ssh start
exit 0

And it worked!! I know it's not a neat way but it can be helpful for someone in need. I must thank @KamilMaciorowski for his kind help & edits.

raspbian with ssh

Please comment below if you see any issue that may arise because of it.

Solution 2

I had a similar problem. I had several Raspberry Pi's that I was reimaging after I plugged the first one in I realized I forgot to set it up properly. I then made changes to all of the SD Cards. The first Raspberry Pi did not work, but all of the other ones did. I had to reimage the SD Card, insert the ssh file, and then it worked.

You have to create the ssh file before the first boot. If you boot the SD Card once and it isn't customized, it will not work as expected.

  • Image the SD Card
  • Make the necessary changes
  • Then first boot
Share:
5,385

Related videos on Youtube

Vinod Srivastav
Author by

Vinod Srivastav

Code is poetry don't compile until it rhymes

Updated on September 18, 2022

Comments

  • Vinod Srivastav
    Vinod Srivastav almost 2 years

    I was not able to enable ssh by putting the file into the boot folder, and yes it was created without extension from a Windows 7 machine and gets deleted after boot.

    This is what should have happened:

    For headless setup, SSH can be enabled by placing a file named ssh, without any extension, onto the boot partition of the SD card from another computer. When the Pi boots, it looks for the ssh file. If it is found, SSH is enabled and the file is deleted. The content of the file does not matter; it could contain text, or nothing at all.

    (Source)

    I don't want to go for Keyboard & Monitor as I can get the IP assigned to Respberry Pi 3 when I connect with ethernet and can also ping with 0% lost.

    After looking for an answer I got to know that the ssh is not enabled and that's the reason putty returns Connection refused on every attempt.

    Now I see there is cmdline.txt in the boot folder is it something that runs on every boot? Can I use to enable ssh? If yes, how to do it?

    Here are all the details, let me know if something else is required.

    • My System: Windows 7 Update 1
    • Raspberry model: Raspberry Pi 3 B
    • Raspberry OS: Raspbian Stretch Lite
    • Monitor: No
    • Keyboard: No
    • Ping Request: responded with no packet loss
    • Connection Type: Ethernet
    • SSH: not enabled (How to?)

    EDIT: Now i am able to mount the 'rootfs' to my windows 7 machine with Paragon ExtFS for Windows, but don't know how to enable ssh yet, any help is appreciated

    • Vinod Srivastav
      Vinod Srivastav over 6 years
      @KamilMaciorowski Thanks for the edit buddy, yeah i got it wrong i guess
    • Kamil Maciorowski
      Kamil Maciorowski over 6 years
      If you got it wrong about command.txt/cmdline.txt then please edit your question and fix it.
    • Vinod Srivastav
      Vinod Srivastav over 6 years
      @KamilMaciorowski for sure, but now i mounted rootfs in Windows 7 and able to see/edit all the files.
  • Kamil Maciorowski
    Kamil Maciorowski over 6 years
    Few things: (1) SSH daemon should work fine the way it is. (2) However if you want to set it up in "the right way", run sudo systemctl enable ssh.service (see this), next remove your hack from rc.local. (3) You asked about cmdline.txt and I didn't even comment on that because of this command.txt/cmdline.txt confusion. The answer is: you cannot start a daemon from within this file because it's for passing arguments to the kernel. (4) I'm glad you found a solution; it's good you posted it here. Upvoted.
  • Vinod Srivastav
    Vinod Srivastav over 6 years
    Thanks, but it was the latest img in a new card itself which is again formatted with Mini Tool as FAT32. I know the official way but in the questionnaire i was trying to answer how to do it when the official version don't work for you