Last active
January 9, 2019 11:42
-
-
Save codycodes/0f336bf832326c78624e037cb26e0038 to your computer and use it in GitHub Desktop.
Changes the hostname on your raspberry pi to what's set in $hostname (variable); can be passed in as a parameter or hard-coded into the script
This file contains 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
#!/bin/bash | |
# see LICENSE file on this gist at gist.github.com/codycodes | |
# Requires root permissions. An easy way to run this script is to simply use "sudo bash change_raspberry_pi_hostname.sh" | |
# Changes the hostname on your raspberry pi to what's set in $hostname (variable) | |
# Can be passed in as a parameter by calling this script or set explicitly in this file | |
# Makes a backup of the hosts file (/etc/hosts.bak) | |
# set the following variable to the hostname you'd like to use: | |
hostname='YOUR_CHOSEN_HOSTNAME' | |
# check if this is running as root | |
if [[ $EUID > 0 ]]; then echo "Please run this script as root (use sudo)"; exit; fi | |
if [ $# -eq 0 ]; then | |
echo -e -n "No parameters provided, please enter the hostname you'd like to use below:\nhostname: " | |
read hostname | |
elif [$# -gt 1]; then | |
echo "incorrect number of arguments. Please run script by using one passed in parameter as hostname or hadcoding the hostname directly into the $hostname variable in the script" | |
exit 1; | |
else | |
echo "setting hardcoded hostname in script: $hostname" | |
hostname="$1" | |
fi | |
cp /etc/hostname /etc/hostname.bak | |
echo "$hostname" > /etc/hostname | |
echo "hostname changed in /etc/hostname to: $(cat /etc/hostname)" | |
sed -i.bak 's/^127.0.1.1/#127.0.1.1/g' /etc/hosts | |
echo "127.0.1.1 $hostname" >> /etc/hosts | |
echo "successfully added the entry to /etc/hosts! your hosts file now looks like the following:" | |
cat /etc/hosts | |
echo "we need to reboot for changes to take effect; press CTRL and the C keys together to cancel; otherwise rebooting in 10 seconds..." | |
sleep 10 | |
reboot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment