Skip to content

Instantly share code, notes, and snippets.

@bradparks
Last active February 24, 2021 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bradparks/0e3444415993990c16eef0dd66aa37cf to your computer and use it in GitHub Desktop.
Save bradparks/0e3444415993990c16eef0dd66aa37cf to your computer and use it in GitHub Desktop.
NOTE: Instead of using a static IP, consider using avanhi as detailed here to setup a DNS name for your pi super easy: http://elinux.org/RPi_Advanced_Setup#Setting_up_for_remote_access_.2F_headless_operation If you'd rather try the static IP, then the following is a script to automatically set your raspberry pi to using a static IP address.
# This script MAY or MAY NOT WORK ;-) Really, untested. But you can cancel before it does anything
# and it actually shows you what you need to add to your dhcpcd.conf file without making
# any changes.
#
if [ "$EUID" -ne 0 ]
then echo "Please run as root, by putting 'sudo' before the command."
exit
fi
# Figure out the info we'll need to overwrite the /etc/dhcpcd.conf file
CARDID=$(ifconfig | head -n1 | awk -F : '{print $1}')
STATIC_IP=$(ip -4 addr show dev $CARDID | grep inet | awk -F / '{print $1}' | awk '{print $2}')
SIZE=$(ip -4 addr show dev $CARDID | grep inet | awk -F / '{print $2}' | awk '{print $1}')
BROADCAST=$(ip -4 addr show dev $CARDID | grep inet | awk '{print $4}')
GATEWAY=$(ip route | grep default | awk '{print $3}')
DNS_SERVERS=$(cat /etc/resolv.conf | grep nameserver | grep [.] | awk '{print $2}')
FILE_POSTFIX=$(date +%Y-%M-%d__%H_%M_%S)
DHCP_FILE="/etc/dhcpcd.conf"
DHCP_OUT="/tmp/__dhcp_temp_$FILE_POSTFIX"
DHCP_BACKUP_FILE="$DHCP_FILE.$FILE_POSTFIX"
# output new file to temporary location
echo "# original of this file at: $DHCP_BACKUP_FILE" > $DHCP_OUT
echo "# " >> $DHCP_OUT
echo "interface $CARDID" >> $DHCP_OUT
echo "static ip_address=$STATIC_IP/$SIZE" >> $DHCP_OUT
echo "static_routers=$GATEWAY" >> $DHCP_OUT
echo "static domain_name_servers=$DNS_SERVERS" >> $DHCP_OUT
echo "About to backup your /etc/dhcpcd.conf file to $DHCP_BACKUP_FILE"
echo "and replace it with the following:"
echo "###############################################################"
cat $DHCP_OUT
echo "###############################################################"
echo
read -p "Press any key to continue, or CTRL+C to abort"
cp -fp $DHCP_FILE $DHCP_BACKUP_FILE
cp -fp $DHCP_OUT $DHCP_FILE
echo
echo "dhcp file replaced!"
echo "You should ensure networking is enabled."
echo "We can do this automatically for you by running the following commands:"
echo " sudo systemctl enable networking"
echo
read -p "Press any key to continue and run the commands, or CTRL+C to abort"
sudo systemctl enable networking
echo
echo "Process complete. To start using your static IP, you'll need to reboot."
read -p "Press any key to continue and reboot, or CTRL+C to abort"
reboot now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment