Changing a hostname permanently in Ubuntu
Solution 1
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>
Solution 2
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/.
Solution 3
In Ubuntu 18.04 LTS
Hostname changing via SSH is reverted after reboot in Ubuntu 18.04. Make permanent change as following way.
1. Edit /etc/cloud/cloud.cfg
sudo nano /etc/cloud/cloud.cfg
Set preserve_hostname to true
preserve_hostname: true
2. Run hostnamectl
hostnamectl set-hostname new-host-name
3. Reboot
sudo reboot
Solution 4
Change hostname permanently without reboot
/etc/hosts
127.0.0.1 persistent_host_name
/etc/hostname
persistent_host_name
Apply changes Immediately
$ sudo hostname persistent_host_name
Check changes
$ hostname
persistent_host_name
Solution 5
To chaneg the Hostname permanet in ubuntu machine
Go to :
#vim /etc/hostname
Type the hostname inside the file you want to set for the machine
Then save and the file
After saving the document run this command
# hostname -F /etc/hostname
Then edit the /etc/hosts file
#vim /etc/hosts
type the ip hostname inside the file
Then Logout of of the machine and relogin into the machine
Related videos on Youtube
user1548960
Updated on September 18, 2022Comments
-
user1548960 over 1 year
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. -
Mr-Programs about 5 yearswhat is the use of setting hostname in production When in production, I have seen some people do hostnamectl set-hostname <what-ever> later to put it in
nano etc/hosts
99% tutorials omit this tho, what is the use of this? Found this link on the matter but still don't quite understand it. learn.akamai.com/en-us/webhelp/adaptive-media-delivery/… -
zzr99 about 4 yearsThis is the only answer that actually works. Thanks