Ubuntu – How to change the hostname without a restart

hostnamerestart

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?

Best Answer

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:

sudo hostname your-new-name

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

sudo -H gedit /etc/hostname

(or some other editor) so that file contains the hostname.

To test that the file is set up correctly, run:

sudo service hostname start

You should also edit /etc/hosts and change the line which reads:

127.0.1.1     your-old-hostname

so that it now contains your new hostname. (This is required otherwise many commands will cease functioning.)