4

Im looking at using 4-5 Pi 2 in the house front rooms bedrooms etc

What I was wondering is if I take out my current setup and put into a brand new Pi 2 would it boot up just the same?

  • Yep, it should work fine. – dhruvvyas90 Dec 2 '15 at 9:21
  • Thanks so I could just clone the SD card and have all 5 with same setup? – Richard Carlton Dec 2 '15 at 9:23
  • 1
    Yes. You might need to modify individual configs such as static IPs or something which might lead to network conflicts ie, you can't have the same IP on all 5 RPi operating on the same network. Other than that, it works without any problem since there is no binding between an RPi and SD card as such. – dhruvvyas90 Dec 2 '15 at 9:24
  • 1
    @SteveRobillard In fact I disagree. Provided hostname is unique there is no problem having identical keys. In fact I find it desirable, as I regularly swap cards between Pi, it causes problems with hosts if the same IP address (based on MAC) is used with a different key. I have been using identical host keys on all my PI, running different distributions for 2 yesrs. – Milliways Dec 2 '15 at 11:19
  • 1
    @millways, that may well work but it will result in a security warning when connecting via SSH and if using things which rely on SSH for communication (puppet, fabric etc.) can cause bigger issues. It also abuses the idea of trust inherent in SSH – Steve Robillard Dec 2 '15 at 11:26
3

Yes you can (I routinely do this). You should ensure that each is unique if on the same network. If using (the default) DHCP the IP addresses will be. It is also desirable to ensure hostname is unique. I use the following script (once only) to set this.

#!/bin/bash
# script to set Pi hostname based on MAC (or Serial number)
# 2017-08-18
# This script should be run as root (or with sudo) to change names
# If run by a user it will report changes, but will NOT implement them
# Works for PiB (all models), Pi2, Pi3, PiZeroW with on board networking
# PiA models will set a unique Name based on Serial number

PDIR="$(dirname "$0")"  # directory containing script
CURRENT_HOSTNAME=$(cat /etc/hostname)
# Find MAC of eth0, or if not exist wlan0
if [ -e /sys/class/net/eth0 ]; then
    MAC=$(cat /sys/class/net/eth0/address)
elif [ -e /sys/class/net/enx* ]; then
    MAC=$(cat /sys/class/net/enx*/address)
else
    MAC=$(cat /sys/class/net/wlan0/address)
fi

# NOTE the last 6 bytes of MAC and CPUID are identical
CPUID=$(awk '/Serial/ {print $3}' /proc/cpuinfo | sed 's/^0*//')
echo "Current Name" $CURRENT_HOSTNAME
echo "MAC" $MAC
# If you want to specify hostnames create a file PiNames.txt with MAC hostname list e.g.
# b8:27:eb:01:02:03 MyPi
# If not found a unique Name based on Serial number will be set
NEW_HOSTNAME=$(awk /$MAC/' {print $2}' $PDIR"/PiNames.txt")
echo "Name found" $NEW_HOSTNAME
if [ $NEW_HOSTNAME == "" ]; then
    NEW_HOSTNAME="pi"$CPUID
fi

if [ $NEW_HOSTNAME = $CURRENT_HOSTNAME ]; then
    echo "Name already set"
else
    echo "Setting Name" $NEW_HOSTNAME
    echo $NEW_HOSTNAME > /etc/hostname
    sed -i "/127.0.1.1/s/$CURRENT_HOSTNAME/$NEW_HOSTNAME/" /etc/hosts
fi
  • wow that's gone over my head, what is the host name? my first PI i just set up i put "Front Room" as a name this was setup using openelec off noobs – Richard Carlton Dec 2 '15 at 15:16
  • hostname is by default raspberrypi. It can be changed through raspi-config and displayed by entering hostname. This is used for many purposes to identify systems e.g. ssh. You don't have to use the script, this can be set manually, but I do it so often, it was easier to automate. By "gone over my head" I presume you mean the script. Like many system scripts, you don't have to understand how they work (this can often be cryptic). Apart from the sed and awk this is pretty ordinary bash scripting. – Milliways Dec 2 '15 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.