Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 350 down vote accepted

It's not too hard. Use the following command in a terminal:

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.


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.

You should also edit /etc/hosts and change the line which reads:     your-old-hostname

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

share|improve this answer
i dont want to restart – Deepak Rajput Dec 15 '11 at 3:12
I know. that's why you should use the 'hostname' command. I just wanted to clarify, that the 'hostname' command does only change the hostname until you restart/crash/etc. Afterwards it will read the name from the file again. – jasperado Dec 16 '11 at 0:31
Depends. If you entered the command you do not need to restart. The hostname is changed already. But only UNTIL your next restart. – jasperado Dec 16 '11 at 14:32
Note that you also have to change the /etc/hosts (see other answers) – JB. Feb 25 '13 at 15:50
In short, although hostname will cause the new name to take immediate effect, it is not "permanent" unless you also change /etc/hostname and /etc/hosts, since those are the files that will be read after a restart or a crash, which of course, you may not be expecting ahead of time. – Tim Parenti Jul 25 '13 at 15:05

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.

share|improve this answer
Confirmed - nice one. – DavidC Aug 31 '14 at 18:18
Note this isn't available everywhere. Just looked on my Ubuntu 14.04 LTS ARM-7 installation, and hostnamectl doesn't exist. Guess some flavours of Ubuntu aren't using systemd yet. – Stéphane Jul 6 '15 at 2:52
@Stéphane the commands are part of the systemd suite, but they are available independently. In this case, 13.04-14.10 don't use systemd as init, by the command is available. – muru Jul 6 '15 at 5:05

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 line to your new hostname   localhost   ubuntu.local    ubuntu   # change to your new hostname/fqdn

Note : i have read it on a forum > Edit /etc/hosts and change the old 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 after you edit /etc/hostname. You must keep both your host names in /etc/hosts ( newhost oldhost) until you execute the command below:

sudo /etc/init.d/ 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 script we used

share|improve this answer
On Ubuntu 12.10, the last part doesn't work with the following error message sudo: unable to resolve host old-hostname. For that part, @jesperado solution works well. – Frédéric Grosshans Nov 23 '12 at 10:54
If you don't update /etc/hosts, mant things start to fail on 12.04.2 LTS, including the w command, Apache, /sbin/reboot and much more. Make sure to update /etc/hosts – Josh Aug 13 '13 at 20:10
You refer to both for localhost and for a custom hostname. Right? Then, towards the end of your answer, for an action without a rebot, you reference /etc/hosts and newhost oldhost. So, there is no need for a address? – Nikos Alexandris Dec 19 '15 at 11:22

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 ./ new-hostname

#!/usr/bin/env bash
echo $NEW_HOSTNAME > /proc/sys/kernel/hostname
sed -i 's/*/\t'"$NEW_HOSTNAME"'/g' /etc/hosts
echo $NEW_HOSTNAME > /etc/hostname
service hostname start
su $SUDO_USER -c "xauth add $(xauth list | sed 's/^.*\//'"$NEW_HOSTNAME"'\//g' | awk 'NR==1 {sub($1,"\"&\""); print}')"
share|improve this answer
What does the last line in script su $SUDO_USER -c "xauth add $(xauth list | sed 's/^.*\//'"$NEW_HOSTNAME"'\//g' | awk 'NR==1 {sub($1,"\"&\""); print}')" do? Just curious. – Fr0zenFyr Mar 10 '14 at 12:22
@Fr0zenFyr: it takes a valid X11 authentication token (xauth list) and replaces the old hostname with the new hostname (sed). Then awk puts quotes around the first argument to xauth add because xauth's input and output format are not symmetric. – Lucas Mar 11 '14 at 21:36
Thanks for the explanation. I had always rebooted for the changes to take effect. +1 – Fr0zenFyr Mar 12 '14 at 4:02
@trakz: Actually 127.a.b.c whatever the number, is all localhost as per IETF RFC. (most people just don't know this, but as most don't, edit approved) – Fabby Jan 7 '15 at 21:25
sudo hostname your-new-name
sudo /etc/init.d/networking restart

That should do the job I think

share|improve this answer
I don't think this will survive a restart – Collin Anderson Sep 15 '14 at 15:54

Without restart:

  1. change hostname in /etc/hostname
  2. update /etc/hosts accordingly
  3. sudo sysctl

Check your current hostname with hostname -f

share|improve this answer
  1. Replace the contents of /etc/hostname with the desired hostname (you can edit with sudo nano /etc/hostname)
  2. In /etc/hosts, replace the entry next to with the desired hostname (you can edit with sudo nano /etc/hosts)
  3. Execute sudo service hostname restart; sudo service networking restart
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.