I want to create a shell script that can change the hostname of my Ubuntu permanently. Whenever I use the hostname New_hostname command, it returns to the original hostname after I restart the machine.
I found out that the only way I can change this permanently is by modifying the file in /etc/hostname and save it. Is there some way I can do this using a shell script only? I also have a password.
The hostnamectl combines setting the hostname via the hostname command and editing /etc/hostname. Unfortunately, editing /etc/hosts still has to be done separately.
hostnamectl set-hostname <new-hostname>
Type
echo "myNewHostName" > /etc/hostname
in any shell with root access near you..
You may also want to take a look at the file /etc/hosts, cf. http://pricklytech.wordpress.com/2013/04/24/ubuntu-change-hostname-permanently-using-the-command-line/.
Hostname changing via SSH is reverted after reboot in Ubuntu 18.04. Make permanent change as following way.
sudo nano /etc/cloud/cloud.cfg
Set preserve_hostname to true
preserve_hostname: true
hostnamectl set-hostname new-host-name
sudo reboot