Last active
April 14, 2023 21:31
-
-
Save ansgar-forestier/3436036 to your computer and use it in GitHub Desktop.
Ubuntu change hostname 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
if [ ! -n "$1" ] | |
then | |
echo 'Missed argument : hostname' | |
exit 1 | |
fi | |
if [ "$(id -u)" != "0" ]; then | |
echo "Sorry, you are not root." | |
exit 1 | |
fi | |
hostname $1 | |
chmod 777 /etc/hostname /etc/hosts | |
sed "s/template/$1/g" /etc/hosts > /home/user/hosts.out | |
rm /etc/hosts | |
mv /home/user/hosts.out /etc/hosts | |
echo "$1" > /etc/hostname | |
chmod 644 /etc/hostname /etc/hosts | |
service hostname down | |
service hostname start | |
dhclient |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DO NOT RUN THE ABOVE SCRIPT!!! The below script was tested on Ubuntu 16.04 several times with no issues.