- This topic has 0 replies, 1 voice, and was last updated Apr 22-10:04 pm by antixcat.
Viewing 1 post (of 1 total)
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.
Forum for users of antiX Linux. Mean and Lean and Proudly anti-fascist.
Forum › Forums › General › Tips and Tricks › Simple script to change the hostname permanently
Hello all,
I found from this post:
https://www.antixforum.com/forums/topic/changing-hostname-safely/#post-100814
That there is a particular sequence of steps needed to succesfully change the hostname permanently. I have written a simple script to do this automatically for all who might find it useful:
#!/bin/bash
echo "WARNING: This script will force a logout on completion"
read -p "Enter new hostname: " new_hostname
# check if hostname is valid
pattern='^[a-zA-Z0-9\-]+$'
if ! [[ $new_hostname =~ $pattern ]]; then
echo "Not a valid hostname"
exit 1
fi
# get ip address from /etc/hosts
ip=$(awk 'NR==1{print $1}' /etc/hosts)
# edit /etc/hosts
# ADD the line (at line 3):
# ip-address new_hostname
# WITHOUT removing the old hostname (at line 2):
# ip-address current_hostname
sudo sed -i "3i\\$ip $new_hostname" /etc/hosts
# REPLACE the only line in the file /etc/hostname
echo $new_hostname | sudo tee /etc/hostname
# again edit /etc/hosts and now REMOVE the old hostname (at line 2)
sudo sed -i '2d' /etc/hosts
# set new hostname
sudo hostname -F /etc/hostname
# logout
pkill -KILL -u $USER
| Cookie | Duration | Description |
|---|---|---|
| cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
| cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
| cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
| cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
| cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
| viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |