Last active
August 10, 2024 12:57
-
-
Save IanMercer/fbdd16567158180b133d43b120ec3972 to your computer and use it in GitHub Desktop.
Automatic hostnames for Raspberry Pi instead of all 'raspberrypi'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If you have many Raspberry Pis that you want to setup from a common image, | |
# add this to `/etc/rc.local` before you create the image, but do not run it. | |
# On first-run it will change the name from `raspberrypi` to `crowd-XXXXXX` where XXXXXX | |
# is the serial number of the Raspberry Pi. (Change 'crowd' in the line below.) | |
MAC="crowd-""$(grep 'Serial' /proc/cpuinfo | sed 's/^Serial.*000\([1-9a-f][0-9a-f]*\)$/\1/')" | |
CURRENT_HOSTNAME=$(cat /proc/sys/kernel/hostname) | |
if [ $CURRENT_HOSTNAME != $MAC ] | |
then | |
echo "Changing name to" $MAC "from" $CURRENT_HOSTNAME | |
chattr -i /etc/hostname | |
echo "$MAC" > "/etc/hostname" | |
# If you are using SaltStack include this next line also | |
echo "$MAC" > "/etc/salt/minion_id" | |
chattr -i /etc/hosts | |
sed -i "s/127.0.1.1\s*$CURRENT_HOSTNAME/127.0.1.1\t$MAC/g" /etc/hosts | |
hostname $MAC | |
#chattr +i /etc/hostname | |
#chattr +i /etc/hosts | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment