Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

5 Answers 5

up vote 190 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.

Note

After a restart your changes in /etc/hostname will be used, so (as you said in the question), you should still use

sudo 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:

127.0.1.1     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
5  
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
5  
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
9  
Note that you also have to change the /etc/hosts (see other answers) –  JB. Feb 25 '13 at 15:50
3  
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

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

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 /etc/init.d/hostname.sh 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 hostname.sh).

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
3  
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

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 at 18:18

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

#!/usr/bin/env bash
NEW_HOSTNAME=$1
echo $NEW_HOSTNAME > /proc/sys/kernel/hostname
sed -i 's/127.0.1.1.*/127.0.1.1\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 at 12:22
2  
@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 at 21:36
    
Thanks for the explanation. I had always rebooted for the changes to take effect. +1 –  Fr0zenFyr Mar 12 at 4:02
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 at 15:54

Your Answer

 
discard

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.