Raspberry Pi: Moving Files in a Network

I’m gradually building a network of Raspberry Pis for different things and, I suppose, its the way that an Internet of Things (IoT) would get built.

At the moment, what I’ve got is a couple of Raspberry Pi Zeros with temperature sensors connected to them in different locations (one in the house and one in the polytunnel which measures the temperature inside the polytunnel and outside). My initial plan is to measure the temperatures and plot them out as graphs (just for interest) but, you could see how this could evolve into a central heating control system maintaining the temperature in various rooms by turning pumps on and off but also using knowledge about the outside temperature to influence the turn on/off times as the rate at which the house cools/heat is affectedby the outside temperature.

Anyway, at the moment, the simple thought is to use a single, more powerful, Raspberry Pi3 to plot the temperatures over time.

In order to do that, I have to move the files containing the temperature from the sensors on the PiZeros to the Pi3. It turned out to be easier than I initially thought. A simple command:

rsync -avz -e ssh /sourcefile pi@target.local:/targetdirectory/

will move “sourcefile” to the targetdirectory on the target machine (.local specifies its on the local network rather than having to know the IP address).

By putting that command into a cron job, the file is moved every time the cronjob is scheduled on the source machine. I’ve set it to run once a day so my temperature files (taken every 10 minutes) are uploaded to the target machine once a day.

The only additional issue is security. The rsync command is running on top of SSH and SSH is secured and requires the password of the target machine every time it runs. So it can’t be run as a cronjob.So what we have to do is to make the source machine an authorised user on the target machine. This again is relatively simple:

  1. Generate an Authorisation key on the host machine: ssh-keygen following the prompts without a passphrase
  2. Copy this Authorisation key to the target machine:
    ssh-copy-id -i ~/.ssh/id_rsa.pub target.local
    Note this will ask for the password for the target machine. Its also recommended to look at authorized_hosts on the target machine to make sure that no other authorised hosts have been added accidentally.

From then on you should be able to use ssh and rsync over ssh without being asked for a password. If you want to make your target machine secure again, delete the file authorized_hosts within the directory and that will do it. This will remove all authorised hosts and the hosts will have to re-authorise themselves.

Security is a thing you should think about at an early stage so I would recommend

  1. That you change the username of your Raspberry Pi;
  2. Use a non-trivial password;
  3. Make sure that your other machines on the network are firewalled from the Raspberry Pis

Security is a problem with the IoT. Networked central heating systems are becoming common (as are fridges, etc.) but the issue is that they are being connected to home networks which contain important computers by people who don’t (and shouldn’t need to) understand the complexities of networking security.

This entry was posted in Raspberry Pi and tagged , , , , , . Bookmark the permalink.