User avatar
micksulley
Posts: 166
Joined: Sat Mar 03, 2012 11:48 am
Location: Melton Mowbray, England

Enable SSH when creating SD card

Mon Jan 02, 2017 1:39 pm

Is there any way to enable SSH on a newly created SD card while it is still in my PC?

All of my Pi's run headless and when I create a new SD card I have to go fetch a TV and USB keyboard just so I can enable SSH, everything else I can then do from my PC via SSH. It would be much more convenient if I could edit the SD card while it is still in my PC to enable SSH. I use Raspbian Lite and Linux Mint on my PC.

Thanks
Mick

fruitoftheloom
Posts: 21067
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: Enable SSH when creating SD card

Mon Jan 02, 2017 1:42 pm

micksulley wrote:Is there any way to enable SSH on a newly created SD card while it is still in my PC?

All of my Pi's run headless and when I create a new SD card I have to go fetch a TV and USB keyboard just so I can enable SSH, everything else I can then do from my PC via SSH. It would be much more convenient if I could edit the SD card while it is still in my PC to enable SSH. I use Raspbian Lite and Linux Mint on my PC.

Thanks
Mick
https://www.raspberrypi.org/blog/a-secu ... bian-pixel
Last edited by fruitoftheloom on Mon Jan 02, 2017 2:01 pm, edited 1 time in total.
Retired disgracefully.....
This at present is my daily "computer" https://www.asus.com/us/Chrome-Devices/Chromebit-CS10/

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Enable SSH when creating SD card

Mon Jan 02, 2017 1:48 pm

Yes.

See the first line of http://downloads.raspberrypi.org/raspbi ... _notes.txt

Just create a simple empty file named ssh on the Windows-accessible part of the SD card after writing Raspbian to it.

[Just about to crack open a pork pie for lunch... ]

User avatar
micksulley
Posts: 166
Joined: Sat Mar 03, 2012 11:48 am
Location: Melton Mowbray, England

Re: Enable SSH when creating SD card

Mon Jan 02, 2017 2:32 pm

Great!! That will save me having to bring the TV downstairs every time I create a new SD!

Thanks guys
Mick

JohnMemo
Posts: 1
Joined: Thu Aug 31, 2017 6:50 am

Re: Enable SSH when creating SD card

Thu Aug 31, 2017 10:37 am

Incredicle, I tried every instruction on the internet in order to enable SSH via SD card and failed.
Only your advice to create this SSH file on the windows accessible partition worked.
So to write a SSH file in /boot/ is not valid anymore on the new Raspbian?

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Enable SSH when creating SD card

Thu Aug 31, 2017 12:47 pm

JohnMemo wrote:
Thu Aug 31, 2017 10:37 am
Incredicle, I tried every instruction on the internet in order to enable SSH via SD card and failed.
Only your advice to create this SSH file on the windows accessible partition worked.
So to write a SSH file in /boot/ is not valid anymore on the new Raspbian?
Hi and welcome to the forum.
The 'windows accessible partition' IS the same as the '/boot/' partition, so not sure what you did wrong....
Do a 'dir' from the CLI, power down the pi cleanly, remove the SD card, put it in your windows machine and you'll see the same files......
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
DougieLawson
Posts: 36544
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Enable SSH when creating SD card

Thu Aug 31, 2017 3:18 pm

JohnMemo wrote:
Thu Aug 31, 2017 10:37 am
Incredicle, I tried every instruction on the internet in order to enable SSH via SD card and failed.
Only your advice to create this SSH file on the windows accessible partition worked.
So to write a SSH file in /boot/ is not valid anymore on the new Raspbian?
It needs to be called ssh or ssh.txt. Linux filenames are case sensitive SSH <> ssh.

To create it with Windows open a command.exe window with [WIN]+[R] then type in command and press [ENTER]

F:
cd \
echo "foo" > ssh.txt
exit
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

W. H. Heydt
Posts: 11070
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Enable SSH when creating SD card

Thu Aug 31, 2017 4:01 pm

micksulley wrote:
Mon Jan 02, 2017 2:32 pm
Great!! That will save me having to bring the TV downstairs every time I create a new SD!
I realize that this is from last January, and that the issue has been solved by other means...but wouldn't it have been easier to take the Pi upstairs--even allowing for taking a keyboard, mouse and PSU along--than to bring the TV downstairs?

quang le
Posts: 1
Joined: Wed Jun 12, 2019 6:08 pm

Re: Enable SSH when creating SD card

Wed Jun 12, 2019 6:15 pm

Hi
I use raspbian version april 2019, and you can create a folder ssh in boot and take SD card to Pi.
You can't create before write .img to SD card.
:))
Good night

deepo
Posts: 251
Joined: Sun Dec 30, 2018 8:36 pm

Re: Enable SSH when creating SD card

Thu Jun 13, 2019 7:50 pm

quang le wrote:
Wed Jun 12, 2019 6:15 pm
Hi
I use raspbian version april 2019, and you can create a folder ssh in boot and take SD card to Pi.
You can't create before write .img to SD card.
:))
Good night
Actually you can, but it involves a lot of mounting, unpacking, partition operations i.e. bash gymnastics :)
But it is possible to create your own "pre baked" image to burn onto micro SD cards.
https://medium.com/platformer-blog/crea ... cb43ff3630

/Mogens

andrew.dudash
Posts: 2
Joined: Wed Nov 13, 2019 5:10 am

Enable SSH when creating SD card

Wed Nov 13, 2019 6:39 am

I'm adding my own answer.

I wanted to enable 'ssh' on startup, connect via Ethernet, and not use Windows or OSX.

I downloaded an image file, '2019-09-26-raspbian-buster-lite.img' and then attempted to edit it. However, I couldn't mount the file. The file contains file systems, but is not a file system itself. The file systems can be listed out with a call to 'fdisk'.

Code: Select all

/sbin/fdisk -l 2019-09-26-raspbian-buster-lite.img
Disk 2019-09-26-raspbian-buster-lite.img: 2.1 GiB, 2248146944 bytes, 4390912 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6c586e13

Device                               Boot  Start     End Sectors  Size Id Type
2019-09-26-raspbian-buster-lite.img1        8192  532479  524288  256M  c W95 FAT32 (LBA)
2019-09-26-raspbian-buster-lite.img2      532480 4390911 3858432  1.9G 83 Linux
There are four useful things to note.
  • First, each sector is 512 bytes.
  • Second, there are two devices.
  • Third, the start sector of each device is listed.
  • Fourth, the file system type of each device is listed.
The individual offsets file systems can be loaded by providing mount an offset. The following code will create an empty directory and then mount the first device to it. The python code calculates the offset. The 'mount' command expects its offset in bytes, so we have to multiply the start sector by the amount of bytes in a sector. After this, you should now be able to 'cd' into the device and make changes. This first device is the boot device. To add 'ssh' support, create an empty file at the root of the device called 'ssh'.

Code: Select all

mkdir pi-1
mount -o offset=$(python -c 'print 8192*512,') 2019-09-26-raspbian-buster-lite.img pi-1
When you're done, you can unmount the file.

Code: Select all

umount pi-1
You can also mount the pi's file system and change values in there. You need to do this if you want to connect to your Pi over Ethernet. The code is almost identical, but the start sector has changed and I've created a new directory to mount to.

Code: Select all

mkdir pi-2
mount -o offset=$(python -c 'print 532480*512,') 2019-09-26-raspbian-buster-lite.img pi-2
To switch to Ethernet, change your '/etc/network/interfaces' file in 'pi-2' to look like this.

Code: Select all

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
Your 'interfaces' file doesn't have to look exactly like this. The main thing is that you need the last two lines.

After these changes, you should be able to copy your image to your SD card, plug it into your pi, and connect over 'ssh' and Ethernet. Give your pi a few minutes to boot before you start losing your mind. If you have 'nmap' installed and using 'nmap' isn't a crime, you can run the following code to find your open 'ssh' port.

Code: Select all

nmap -p 22 10.0.0.0-255
That says, check if port 22 is open on every address that starts with 10.0.0 This will work as long as the machines on your local network all have addresses prefixed with 10.0.0, but your home network may look different. Try using 'ip a' to see what your current machine's IP address or use the first 3 parts of your router's IP address.

I hope this helps a little.

iznobe
Posts: 289
Joined: Sun Feb 05, 2017 4:14 pm
Location: Avignon , FRANCE

Re: Enable SSH when creating SD card

Wed Nov 13, 2019 2:24 pm

hi ,

it is very simple to use ans OS like ubuntu or linux mint .

when you have downloaded to your desktop , a simple right click , choose mount the image .

enter in /boot as root user , create your new empty file named ssh , close all and burn it :D

Lewis-H
Posts: 15
Joined: Thu Oct 31, 2019 12:45 pm

Re: Enable SSH when creating SD card

Wed Nov 13, 2019 2:36 pm

To enable SSH automatically every time the Pi starts you need to :
Prepare a fresh SD card with the latest version of Raspbian.
Insert the SD card in your PC.
Create a file on the boot partition named “ssh”
Eject the SD card, insert it into your Pi and power it up.
Hope this helps!
Regards,
Lewis

andrew.dudash
Posts: 2
Joined: Wed Nov 13, 2019 5:10 am

Re: Enable SSH when creating SD card

Sat Nov 16, 2019 3:29 pm

iznobe wrote:
Wed Nov 13, 2019 2:24 pm
when you have downloaded to your desktop , a simple right click , choose mount the image .
I don't have a Desktop Ubuntu or Mint machine running right now, but I'd love to see screenshots of this. I was not able to mount my Raspberry Pi image automatically on Debian. As I stated earlier, I had to find the offsets of filesystems within the image file and then mount with explicit offsets.

RonR
Posts: 641
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: Enable SSH when creating SD card

Sun Nov 17, 2019 12:17 am

andrew.dudash wrote:
Sat Nov 16, 2019 3:29 pm
I was not able to mount my Raspberry Pi image automatically on Debian. As I stated earlier, I had to find the offsets of filesystems within the image file and then mount with explicit offsets.

Take a look at image-mount contained in Image File Utilities.

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Enable SSH when creating SD card

Sun Nov 17, 2019 9:08 am

The recent contributions in this resurrected thread seem over complex.

The original question, nearly 3 years ago was -
Is there any way to enable SSH on a newly created SD card while it is still in my PC?

And that was answered succinctly to the satisfaction of the OP within 10 minutes -
Just create a simple empty file named ssh on the Windows-accessible part of the SD card after writing Raspbian to it.
That solution is probably Operating System agnostic if 'Windows-accessible' is understood to mean the small fat-formatted boot partition on the newly-created card. No messing about with mounting images or finding partition offsets is required.

User avatar
DougieLawson
Posts: 36544
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Enable SSH when creating SD card

Sun Nov 17, 2019 9:37 am

This is another case where locking ancient threads (after six months of inactivity) becomes desirable.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
HawaiianPi
Posts: 4860
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Enable SSH when creating SD card

Sun Nov 17, 2019 10:04 am

andrew.dudash wrote:
Wed Nov 13, 2019 6:39 am
I hope this helps a little.
Thanks, but already covered here: STICKY: Making your own custom burn-n-boot Raspbian image
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

Return to “General discussion”