Monday, December 11, 2017

Raspberry Pi 3 Headless Server With SSH

If you wish to make a standalone router, file server, or satellite weather station, then you need a little computer to make things happen.  At this time, the most popular embedded Linux machine is the Raspberry Pi.  It is a delightful little design - tiny and very useful.

The default system is Raspbian, which is loosely based on Debian.  This is excellent.  Years ago, I tried a Beaglebone Black and it came with a cripple version of Angstrom Linux which I didn't like and the board is consequently lying somewhere in my junk box.

The Raspbian system is aimed at clueless newbies and the ssh daemon is disabled by default.  To use it as an embedded server, without having to plug in a keyboard and screen, you need to add one line to a configuration file, before you plug the SD card into it.  Here is how to do all that.

Get a Pi and a SD Card Image

Get your RPi3 from here:

Download a Raspbian image zip file from here:

Open a terminal and unzip with:
$ ark —batch

That will take a loooong time…
(On Linux machines, you probably have to do this on the command line.  A graphical tool will very likely run out of memory and crash.  Just double clicking it will work fine on a Mac since they usually have enough RAM installed.)

Become super user:
$ su -

Write the img file to a miniature SD card:
# dmesg (to see what is going on, then insert SD card)
# dmesg (again, to see what is the device name)

# dd if=filename.img of=/dev/mmcblk0p bs=1M
(If the device is busy, it was probably unhelpfully mounted - unmount it)

or on a Mac:
$ sudo dd if=filename.img of=/dev/disk2 bs=1000000 

Ensure that the buffers are flushed:
# sync

Make a Headless Pi with the SSH Daemon

If necessary, re-insert the SD card and mount the root directory (don’t bother with the boot directory).
# cd /run/media/username/root/etc
# vi rc.local

Insert mode:

Go to the bottom and just above the exit statement add:
systemctl start ssh

Save the file:

Flush the disk file buffers:
# sync

Umount and Eject the card.

First Login

Now when you hook the Pi to your router, it will get an IP address via DHCP and then run sshd automatically the way the Linux gods intended it to be.

You can then log in with for example:
$ ssh pi@

Change the password:
$ passwd

Expand the root filesystem to use all of the SD card

Since this is a new device, you need to expand the file system to fill the whole SD card:
$ sudo raspi-config

Now do Update followed by Advanced, Expand Filesystem.  Then do Finish, Reboot, Yes.

Solid State File Server

With four 256 GB USB flash memory widgets plugged in, you can make a 1 TB solid state file server for about 1100 Dirhams - something that was unimaginable just a few years ago.

A file server doesn't have to be fast.  It is limited by the network speed, not the processor.  So a little Rpi makes a very cost effective file server.

So far, I managed to get only two 256 GB Sandisk widgets.  They came in perfectly idiotic packages, so I had to break all the plastic off to get them to fit in the USB sockets.  For protection, I wrapped one layer of self vulcanizing tape around them.  I don't know what the clowns who designed the little enclosures were thinking, but it was clearly form over function - now it is function over form...

I always prefer doing things the simple way, so I formatted them with ext4 and mounted them using /etc/fstab in /mnt/sda1 and /mnt/sdb1 like this:
$ sudo fdisk /dev/sda1
-t 83

$ sudo mkfs.ext4 -L sda1ext4 /dev/sda1

$ sudo nano /etc/fstab /dev/sda1 /mnt/sda1 ext4 defaults,noatime 0 1

$ sudo mount -a

...and ditto for the other one.

I have never managed to figure out how to mount a disk so that a common user can write to it.  My workaround is to make a directory on the thing and assign the user name and group to that, so now I have /mnt/sda1/dataa and /mnt/sdb1/datab and chowned them to pi:. $ sudo mkdir -p /mnt/sda1/dataa
$ sudo chown pi: /mnt/sda1/dataa

So the access problem is solved without having to read another manual on disk mounting and with that, I now have 512 GB of online solid state storage, accessible over ssh and scp.


These Sandisk USB watchammacallits get very hot and there are long stories on the Sandisk forums about heat problems.  So removing them from their plastic packaging is actually a good idea, since that improves the air flow over the chips. The good news is that most people say they last for years, despite the heat.

Rsync Backup Script

I made a RSA key file with ssh-keygen and uploaded it with ssh-copy-id, as described here

The IP address of the Rpi was added to /etc/hosts, so I don't have to keep typing it in.

One's natural instinct is to make a backup script that includes all the files that one needs to backup - that is wrong!

The trick with a backup script, is to keep it simple and include everything in your home directory and then exclude a few generic things.   That way, the script is maintenance free and will always work, irrespective of how you move files around.

My Macbook rsync backup script now looks like this:
#! /bin/bash
rsync -avze ssh --progress --delete --max-delete=10 --max-size=20M --exclude '.Trash'  ~/. pi@rpi:/mnt/sda/dataa/

The max-delete protects against catastrophes and max-size prevents making backups of large ISO files and movies, since those things can always be downloaded again from wherever they came from.

Secure Shell SSH on Windows

Note that Windows 10 now finally supports OpenSSH, so you could also connect securely to a sshd server with sftp from the latest Win10, but not from Win8.1 or Win7.

Deduplication with Hard Links

Once you start to save backups, you will rapidly accumulate duplicate files on the server.  You can eliminate these with the hardlink utility and regain many gigabytes of storage space:
$ hardlink -c directoryname

A hard link is an additional directory entry for the same file.  When you delete hard links, the file will only disappear, once the last hard link is deleted.  This is much better than soft links, where you can delete the file, leaving a bunch of broken links all over the place, with no easy way to clean up the litter.  You can perhaps think of hard links as smart links and the other as soft in the head links.

La voila!


No comments:

Post a Comment

On topic comments are welcome. Junk will be deleted.