How do I change the hostname without a restart?

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.)


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.

hostnamectl set-hostname new-hostname

This command is part of the systemd-services package (which, as of Ubuntu 14.04, also includes the timedatectl and localectl commands). As Ubuntu migrates to systemd, 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,

nano /etc/hostname 

Edit /etc/hosts and change the old 127.0.1.1 line to your new hostname

127.0.0.1   localhost
127.0.1.1   ubuntu.local    ubuntu   # change to your new hostname/fqdn

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:

sudo service hostname start

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).