Skip to content

Instantly share code, notes, and snippets.

@dweeber
Created November 11, 2012 15:58
Show Gist options
  • Save dweeber/4055331 to your computer and use it in GitHub Desktop.
Save dweeber/4055331 to your computer and use it in GitHub Desktop.
Set RPI hostname based on /boot/serial_hostname.txt file
#! /bin/sh
### BEGIN INIT INFO
# Provides: serial_hostname
# Required-Start: $local_fs
# Required-Stop:
# X-Start-Before hostname
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Set hostname based on /boot/serial_hostname.txt
# Description: Read the machines hostname from /boot/serial_hostname.txt,
# based on machine serial number
### END INIT INFO
. /lib/lsb/init-functions
log_daemon_msg "Starting serial_hostname"
if [ -f /boot/serial_hostname.txt ]
then
SERIAL=`cat /proc/cpuinfo | grep Serial | awk '{ print $3 }'`
NEWHOST=`grep $SERIAL /boot/serial_hostname.txt | awk '{ print $2 }'`
if [ -z "$NEWHOST" ]; then
sed -i "$ a${SERIAL} ${SERIAL}" /boot/serial_hostname.txt
NEWHOST=$SERIAL
fi
if [ ! -z "$NEWHOST" ]; then
OLDHOST=`cat /etc/hostname`
log_daemon_msg "Changing $OLDHOST to $NEWHOST..."
echo $NEWHOST > /etc/hostname
sed -i "s/127.0.1.1.*/127.0.1.1\t$NEWHOST/g" /etc/hosts
fi
fi
log_daemon_msg "serial_hostname complete"
exit 0
@dweeber
Copy link
Author

dweeber commented Nov 11, 2012

Moved log_daemon_msg complete message outside of if statements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment