<p>In this post I will show you how to take a default Raspbian Image and safely enable ssh by allowing remote access only with authorized keys.</p>
<p>Recently, and correctly, the official Raspbian Pixel distribution disabled ssh with the note that <em>from now on SSH will be disabled by default on our images.</em>To understand why this is a good thing please read <a href="https://www.raspberrypi.org/blog/a-security-update-for-raspbian-pixel/" target="_blank" rel="noopener">A security update for raspbian pixel</a>. In short, having 11 million computers out there in the hands of non security professionals, with a known username and password, is not a good idea.</p>
<p>That said there are many cases where you want to access your Pi remotely, and a key part of that is the ability to access it securely via ssh.</p>
<p>The Raspberry Pi site offers a <a href="https://www.raspberrypi.org/documentation/remote-access/ssh/">solution</a> for how to reactivate ssh. One option is via the GUI, <em>Preferences</em> &gt; <em>Interfaces</em>&gt; <em>SSH</em> &gt; <em>Enabled</em>. Another is via the console <em>sudo raspi-config</em> &gt; <em>Interfacing Options </em>&gt; <em>SSH</em> &gt; <em>Yes &gt; Ok &gt; Finish</em>. The third offers a more interesting option.</p>
<blockquote><p>For headless setup, SSH can be enabled by placing a file named ssh, without any extension, onto the boot partition of the SD card. 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.</p></blockquote>
<p>This is exactly what we want. Normally you would burn the image, then boot it in a Pi with a keyboard, screen and mouse attached, and then add the file. A shortcut to that would be to burn the image, eject it, insert it again, mount the sdcard boot partition, and then create a file called <em>ssh</em>.</p>
<p>I don't like either of these solutions as they involve varying amounts of user intervention. I want a solution that will automatically leave me with a modified image at the end without any intervention (aka human error) on my part.</p>
<p>So I want to build a script that can handle the following steps:</p>
<ul>
<li>Download the latest image zip file</li>
<li>Verify it is valid</li>
<li>Extract the image itself</li>
<li>Enable ssh</li>
<li>Change the default passwords for the root and pi user</li>
<li>Secure the ssh server on the Pi</li>
</ul>
<p>I could add to this list and customize every aspect of the image, but my experience has shown that the more you modify, the more maintenance you will need to do. When changes are made to the base Raspbian image, you will need to fix your scripts, and worse is the job of updating all those already deployed Pi's.</p>
<p>A better approach is to use the base images and control them with automation tools like <a href="https://www.ansible.com/">Ansible</a>, <a href="https://www.chef.io/chef/">chef</a>, <a href="https://puppet.com">puppet</a>, <a href="https://cfengine.com/">cfengine</a>, etc. This allows the images to be treated as Cattle rather than Pets, to see what that means see <a href="https://www.slideshare.net/randybias/architectures-for-open-and-scalable-clouds">Architectures for open and scalable clouds</a>, by Randy Bias, VP Technology at EMC, Director at OpenStack Foundation.</p>
<p>Another approach to consider would be to <a href="https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/net_tutorial.md">Network Boot</a> your Raspberry Pi and in that way the sdcard is barely used, and all traffic is run off the network. If you are deploying a lot of pi's in a area with a good physical network then this is a great option as well. This has the advantage that all the files are kept on the network and can be completely controlled from a central location.</p>
<p>If you can't be bothered to stick around and find out how I did it, you can download the script <a href="https://github.com/kenfallon/fix-ssh-on-pi">fix-ssh-on-pi.bash from git hub</a>. Remember that it is intended more as inspiration rather than a working tool out of the box. I deliberately wrote it so you must edit it to make it fit your needs.</p>
<p>See the  <a href="https://hackerpublicradio.org/eps/hpr2356/index.html">complete show notes</a> for the step by step instructions that lead to the creation of the script file, with credit been given to the sites that offered each part of the solution.</p>