Skip to content

Instantly share code, notes, and snippets.

@dweeber
Created November 2, 2012 01:40
Show Gist options
  • Save dweeber/3998116 to your computer and use it in GitHub Desktop.
Save dweeber/3998116 to your computer and use it in GitHub Desktop.
Script to change /etc/hosts and /etc/hostname if /boot/newhost is found
#! /bin/sh
### BEGIN INIT INFO
# Provides: anewhost.sh
# Required-Start:
# Required-Stop:
# Should-Start:
# Default-Start: S
# Default-Stop:
# Short-Description: Changes /etc/hostname /etc/hosts if /boot/newhost found
# Description: /boot/newhost file to change the
# /etc/hosts and /etc/hostname on start
### END INIT INFO
PATH=/sbin:/bin
. /lib/init/vars.sh
. /lib/lsb/init-functions
do_start () {
if [ -f /boot/newhost ]
then
OLDHOST=`cat /etc/hostname`
NEWHOST=`cat /boot/newhost`
echo "Changing $OLDHOST to $NEWHOST..."
echo $NEWHOST > /etc/hostname
sed -i "s/127.0.1.1.*$OLDHOST/127.0.1.1\t$NEWHOST/g" /etc/hosts
rm /boot/newhost
fi
}
case "$1" in
start|"")
do_start
;;
restart|reload|force-reload)
echo "Error: argument '$1' not supported" >&2
exit 3
;;
stop)
# No-op
;;
*)
echo "Usage: hostname.sh [start|stop]" >&2
exit 3
;;
esac
:
@dweeber
Copy link
Author

dweeber commented Nov 2, 2012

Copy this script to /etc/init.d/ahostname.sh
sudo update-rc.d anewhost.sh start 01 S

If you create a new file /boot/newhost and put a hostname you want the RPi to become and then reboot, the anewhost.sh will change the /etc/hosts and /etc/hostname to match, remove the /boot/newhost file after the reboot.

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