Skip to content

Instantly share code, notes, and snippets.

@rjhornsby
Last active August 8, 2021 05:21
Show Gist options
  • Save rjhornsby/348b3305394e11efd528701913c854f8 to your computer and use it in GitHub Desktop.
Save rjhornsby/348b3305394e11efd528701913c854f8 to your computer and use it in GitHub Desktop.
RaspberryPi after-first-boot config shell script
#!/bin/bash
# If you wish to have the pi change its default hostname from 'raspberrypi',
# pass the new hostname as an argument, ie:
# /boot/init.sh urmomspi
# Change these values for your environment, or set them to empty strings
# to leave at the default
LOCALE="en_US.UTF-8"
KEYB_LAYOUT="us"
TIMEZONE="America/Chicago"
# =========
if [ -n "$1" ]; then
echo "Setting hostname to $1"
sed -i "/^127.0.1.1\s*raspberrypi/s/raspberrypi/$1/" /etc/hosts
echo "$1" > /etc/hostname
touch /var/run/reboot-required
else
echo "No hostname provided, skipping"
fi
if [ -n "$LOCALE" ]; then
echo "Setting locale..."
sed -i "/^# $LOCALE/s/^#\s*//" /etc/locale.gen
locale-gen $LOCALE
update-locale $LOCALE
fi
if [ -n "$KEYB_LAYOUT" ]; then
echo "Setting keyboard layout..."
sed -i "/^XKBLAYOUT=.*/c XKBLAYOUT=\"$KEYB_LAYOUT\"" /etc/default/keyboard
fi
if [ -n "$TIMEZONE" ]; then
echo "Setting timezone"
rm -fv /etc/localtime
ln -sv /usr/share/zoneinfo/$TIMEZONE /etc/localtime
fi
if [ -e /boot/id_rsa.pub ]; then
echo "Copying public key to pi user..."
if [ ! -d ~pi/.ssh ]; then
mkdir ~pi/.ssh
chown pi:pi ~pi/.ssh
fi
cat /boot/id_rsa.pub >> ~pi/.ssh/authorized_keys
chown pi:pi ~pi/.ssh/authorized_keys
rm /boot/id_rsa.pub
else
echo "No public key found at /boot/id_rsa.pub, skipping"
fi
# Stop Microsoft from getting installed
echo "Excising microsoft"
cat /dev/null > /etc/apt/sources.list.d/vscode.list
cat /dev/null > /etc/apt/trusted.gpg.d/microsoft.gpg
# Prevent future Raspbian upgrades from re-populating the microsoft repo config
chattr +i /etc/apt/sources.list.d/vscode.list /etc/apt/trusted.gpg.d/microsoft.gpg
apt-get update
echo "Installing vim and friend"
apt-get -y install vim pcregrep
echo "Installing python env tools"
apt-get -y install python3-pip python3-setuptools
echo "Running updates, this may take a while"
apt-get -y upgrade
# If an upgrade actually happened, mark reboot
pcregrep -qM 'apt-get.*upgrade(\n|.)*^Upgrade:' /var/log/apt/history.log && touch /var/run/reboot-required
if [ -e /var/run/reboot-required ]; then
shutdown -r 6 "completing initial setup"
fi
@rjhornsby
Copy link
Author

not sure exactly when, but somewhere along the way the US locale string in /etc/locale.gen changed from en_US.UTF8 to en_US.UTF-8. Updated the script to match.

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