commit 82dd1e4f9108c422bce75c7e964bbafb767b028c Author: adrien Date: Tue Dec 22 23:24:25 2020 +0100 First commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ae88d09 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Adrien + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6541464 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# ssh-at-boot +This script modifies a Raspberry Pi OS image to enable SSH at boot. For security reasons, SSH is disabled. + +## Introduction +If you are using Raspberry Pi OS (previously called Raspbian), at first startup you cannot connect directly via SSH to your Raspberry Pi because the SSH daemon (`sshd`) is not started. If you want to understand why this choice was made, it's over there: https://www.raspberrypi.org/blog/a-security-update-for-raspbian-pixel. + +Two choices are available to you. You have a HDMI cable that fits your Raspberry Pi and a keyboard. You log in with the user `pi` and password `raspberry`, and you execute the following command. + +``` +sudo systemctl enable --now sshd.service +``` + +Otherwise you modify the image of the Raspberry Pi OS by adding an empty file called `ssh` in the first partition and the system will understand that it must start the SSH daemon at boot. + +This script allows you to do this second option very quickly. + +## Requirements + +### System +This script can run on any GNU/Linux machine and uses standard commands. Normally all of them are already installed on your system. + +Basically, only the superuser can mount filesystems, so it is necessary to run this script with root rights. + +## Installation +As soon as you have downloaded (https://www.raspberrypi.org/software/operating-systems/#raspberry-pi-os-32-bit) the version that suits your needs and unzipped it, you can do the following. + +``` +curl -Lo ssh-at-boot -sSf https://gitea.illuad.fr/adrien/ssh-at-boot/raw/branch/master/ssh-at-boot +chmod +x ssh-at-boot +sudo ./ssh-at-boot /path/to/raspberry-pi-os.img +``` + +If all goes well, the message `File created.` appears. You can now copy as you are used to do the image to the SD card and enjoy life. + +## Automation +Suppose you are in a hurry and you don't want to use the method mentioned above. You can do the following. This example assumes that you have already downloaded and unzipped the Raspberry Pi OS image. + +**Be careful. It is a practice that I strongly advise against. Running a script without even checking it is a bad practice. You know very well what I'm talking about.** + +``` +curl -sSf https://gitea.illuad.fr/adrien/ssh-at-boot/raw/branch/master/ssh-at-boot | sudo bash -s -- 2020-12-02-raspios-buster-armhf-lite.img +File created. +``` diff --git a/ssh-at-boot b/ssh-at-boot new file mode 100644 index 0000000..a927f3a --- /dev/null +++ b/ssh-at-boot @@ -0,0 +1,45 @@ +#! /usr/bin/env bash + +# abort on nonzero exitstatus +set -o errexit + +# abort on unbound variable +set -o nounset + +# don't hide errors within pipes +set -o pipefail + +if [ $# -eq 0 ] +then + echo "No argument supplied. Please specify the unziped Raspberry Pi OS file." + exit 1 +fi + +mkdir -p /mnt/sdcard_1 + +# extract the start offset +sector_offset=$(fdisk --list ${1} | awk '/W95 FAT32/{print $2}') + +# multiply it with the sector size +byte_offset=$((${sector_offset} * 512)) + +# mount the first partition +mount --options loop,offset=${byte_offset} ${1} /mnt/sdcard_1 + +if [ $? -eq 0 ] +then + # create the file + touch /mnt/sdcard_1/ssh + + if [ $? -eq 0 ] + then + echo "File created." + else + echo "Unable to create the file." + fi + + # unmount + umount /mnt/sdcard_1 +else + echo "Can't mount the file." +fi