I want to change the OS hostname but I do not want to restart.
I have edited /etc/hostname
but it requires a restart to get implemented. How to avoid this?
I want to change the OS hostname but I do not want to restart.
I have edited /etc/hostname
but it requires a restart to get implemented. How to avoid this?
It's easy. Just click the Gear icon (located at upper right corner of the screen), open "About this computer" screen (located at Gear icon ) and edit "Device name".
Or, in a terminal, use the following command:
This will set the hostname to your-new-name until you restart. See
man hostname
and How do I change the computer name? for further information. Do not use_
in your name.Note
After a restart your changes in
/etc/hostname
will be used, so (as you said in the question), you should still use(or some other editor) so that file contains the hostname.
To test that the file is set up correctly, run:
You should also edit
/etc/hosts
and change the line which reads:so that it now contains your new hostname. (This is required otherwise many commands will cease functioning.)
Ubuntu 13.04 onwards
The
hostnamectl
command is part of the default installation on both Desktop and Server editions.It combines setting the hostname via the
hostname
command and editing/etc/hostname
. As well as setting the static hostname, it can set the "pretty" hostname, which is not used in Ubuntu. Unfortunately, editing/etc/hosts
still has to be done separately.This command is part of the
systemd-services
package (which, as of Ubuntu 14.04, also includes thetimedatectl
andlocalectl
commands). As Ubuntu migrates tosystemd
, this tool is the future.Without Restart
Changing the hostname or computer name in ubuntu without restart
Edit /etc/hostname and change to the new value,
Edit /etc/hosts and change the old 127.0.1.1 line to your new hostname
Note : i have read it on a forum > Edit /etc/hosts and change the old 127.0.1.1 line to your new hostname (if you dont do this, you wont be able to use sudo anymore. If you hav e already done it, press ESC on the grub menu, choose recovery, and edit your host file to the correct settings)
Now after a reboot, your hostname will be the new one you chose
Without Reboot
To change without a reboot, you can just use hostname.sh after you edit /etc/hostname. You must keep both your host names in /etc/hosts (127.0.0.1 newhost oldhost) until you execute the command below:
Note : Above command to make the change active. The hostname saved in this file (/etc/hostname) will be preserved on system reboot (and will be set using the same service).
The default name was set when you were installing Ubuntu. You can easily change it to whatever you want in both Desktop & Server by editing the hosts and hostname files. Below is how:
sudo hostname NEW_NAME_HERE
This will change the hostname until next reboot. The change won’t be visible immediately in your current terminal. Start a new terminal to see the new hostname.
To change the name permanently, run command to edit the host files:
sudo -H gedit /etc/hostname
andsudo -H gedit /etc/hosts
For Ubuntu server without a GUI, run
sudo vi /etc/hostname
andsudo vi /etc/hosts
and edit them one by one. In both files, change the name to what you want and save them.Finally, restart your computer to apply the changes.
Cloud-init (Ubuntu 18+) hostname persistence
Whilst the above approaches (
hostnamectl
,etc/hostname
, etc) work for immediate hostname change, with the advent of cloud-init - which can control setting of the hostname - amongst many other things. So it won't stick after a reboot if cloud-init is installed. If you want the change to stay after a reboot then you'll need to edit the cloud-init config files, disable cloud-init's hostname set/update module:or disable cloud-init entirely:
Here is a script that changes the hostname in the prescribed way. It ensures that not only
sudo
but also X11 applications continue to function with no restart required.Usage:
sudo ./change_hostname.sh new-hostname
Without restart:
/etc/hostname
/etc/hosts
accordinglysudo sysctl kernel.hostname=mynew.local.host
Check your current hostname with
hostname -f
To get your current hostname:
This can be changed in any text editor. You would also need to update entry other than localhost against 127.0.0.1 in /etc/hosts.
Ubuntu 16.04
Solution based on answer from the DigitalOcean Comunity.
Edit hosts file.
$ sudo nano /etc/hosts
Replace oldname with new one.
127.0.0.1 localhost newname
Setup new hostname.
$ sudo hostnamectl set-hostname newname
/etc/hostname
with the desired hostname (you can edit withsudo nano /etc/hostname
)/etc/hosts
, replace the entry next to 127.0.1.1 with the desired hostname (you can edit withsudo nano /etc/hosts
)sudo service hostname restart; sudo service networking restart