552

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?

the
  • 805
  • 1
  • 9
  • 16
  • 3
    Warning: won't work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot. – null Sep 01 '18 at 07:06

14 Answers14

692

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

jasperado
  • 7,388
  • 6
    i dont want to restart – Deepak Rajput Dec 15 '11 at 03:12
  • 27
    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 00:31
  • 1
    thanks. that means after changing the hostname we need to restart right. – Deepak Rajput Dec 16 '11 at 04:25
  • 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
  • 21
    Note that you also have to change the /etc/hosts (see other answers) – JB. Feb 25 '13 at 15:50
  • 11
    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
  • I just did these steps on Lubuntu. Now I can't start any programs. Even the power button in the bottom right of the screen doesn't function. Nor Ctrl+Alt+Delete. I can post this only because firefox was already running. I'm going to press the hardware power button now. --update: Had to long press the power button. Everything seems fine now. Hostname successfully changed... but technically a reboot was still required. – Brent Jul 06 '15 at 03:21
  • How can I restore original host name w/o restart? – hellboy Jan 18 '16 at 10:51
  • You have to run "sudo hostname your-original-hostname" in a terminal. If you do not know it anymore and did not change the above mentioned files yet, you can look it up: "cat /etc/hostname" – jasperado Jan 27 '16 at 23:32
  • My /etc/hosts says 127.0.1.1 localhost. So you propose to change localhost to something? – Dims Jul 28 '17 at 11:07
  • That depends on the purpose of your system. If it is just a desktop, feel free to leave it as it is. If it is a server, which is available via an actual domain name, you would want to add this name to the line. 127.0.1.1 localhost localhost.localdomain your-domain.net – jasperado Jul 29 '17 at 22:01
  • If you're running postfix for emails then it's a good idea to check the /etc/postfix/main.cf and the variables for myhostname and mydestination. – forsvunnet Jan 04 '18 at 11:13
  • Warning: won't work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot. – null Sep 01 '18 at 07:05
  • 1
    Warning: won't work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot. See cloud-init answer(s) below. – null Sep 01 '18 at 07:06
313

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.

muru
  • 197,895
  • 55
  • 485
  • 740
  • 1
    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 06 '15 at 02:52
  • 1
    @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 06 '15 at 05:05
  • @muru, you show 2 items are needed: editing of /etc/hosts and invoking hostnamectl. I think there may have to be a 3rd item which would be similar to export HOSTNAME=xxx. Also the order might matter. I have problems invokingsudo if the order is wrong. I infer that sudo depends upon /etc/hosts so it is better to do that one first if either of the other 2 operations require sudo. – H2ONaCl Jan 31 '17 at 13:54
  • 2
    @H2ONaCl the shell variable is set on startup, just open another shell and the hostname variable should be fine. As for sudo, the error about hostname resolution has never actually prevented me from doing anything. – muru Jan 31 '17 at 13:57
  • @muru, I agree sudo works but it's better to eliminate the "error" message even if it is just a "warning" so I think the edit to /etc/hosts has to happen first. If we are interactive then yes the user can open a new terminal but if we are in a script I think it's better to get the 3rd change immediately so I suggested export HOSTNAME=xxx. – H2ONaCl Jan 31 '17 at 14:00
  • @H2ONaCl I only ever use a script for this, I haven't had any problems. Especially the shell variable - I don't think any external command cares for that. I always use the hostname command. – muru Jan 31 '17 at 14:04
  • @muru, yes, I should have said that my script depends upon $HOSTNAME so I need to do export HOSTNAME=xxx. Even if your script doesn't need it the most complete answer might need something like this to be generally applicable. I don't know if there is a better way so I did it this way. – H2ONaCl Jan 31 '17 at 14:07
  • 1
    @H2ONaCl ah, but the HOSTNAME variablle isn't standard - IIRC dash and ksh don't have it, zsh uses HOST, so as far as shell variables go, it's the bottom of the heap. – muru Jan 31 '17 at 14:14
  • thank you, this was the solution for me. top voted answer didn't work on Ubuntu 16.0.4 LTS – Sun May 15 '18 at 19:09
  • 4
    Still works in Ubuntu 18.04 LTS. – LucaM Jun 15 '18 at 15:30
  • If you run mDNS (the default is on for many Linux): sudo service avahi-daemon restart. The default hostname on Raspbian is raspberrypi.local and if you have more than one such device, renaming is a common task. – Scott Prive Dec 06 '21 at 14:59
66

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 don't do this, you wont be able to use sudo anymore. If you have 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).

Manuel Jordan
  • 1,768
  • 7
  • 30
  • 50
One Zero
  • 27,153
  • 26
  • 88
  • 109
  • 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
  • You refer to both 127.0.0.1 for localhost and 127.0.1.1 for a custom hostname. Right? Then, towards the end of your answer, for an action without a rebot, you reference /etc/hosts and 127.0.0.1 newhost oldhost. So, there is no need for a 127.0.1.1 address? – Nikos Alexandris Dec 19 '15 at 11:22
  • 1
    didn't work for me. i had to use sudo nano /etc/hosts for the change to take effect; without sudo, just nano /etc/hosts didn't save the change – musicman1979 Jan 30 '18 at 23:30
  • 3
    What's the difference between restart and reboot ? – storm Apr 18 '18 at 11:45
  • @storm I suppose he was referring to service restart – Manchineel Jun 18 '18 at 21:22
  • I love how this answer says "Without restart" then in that same section he says "Now AFTER A REBOOT, your hostname will be the new one you chose", nice ... but that's just what he/us try to avoid to restart/reboot :) – Melardev Sep 17 '21 at 14:53
44

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:

  1. Press CtrlAltt on keyboard to open the terminal. When it opens, run the below command: 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.

  1. To change the name permanently, run command to edit the host files:

    sudo -H gedit /etc/hostname and sudo -H gedit /etc/hosts

For Ubuntu server without a GUI, run sudo vi /etc/hostname and sudo 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.

muru
  • 197,895
  • 55
  • 485
  • 740
20

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:

sudo sed 's/preserve_hostname: false/preserve_hostname: true/' /etc/cloud/cloud.cfg

or disable cloud-init entirely:

sudo touch /etc/cloud/cloud-init.disabled
Pierz
  • 3,063
  • 1
  • 25
  • 15
  • 1
    Please upvote this answer. This worked great for me. Other answers don't work with the adoption of cloud-init. – null Sep 01 '18 at 07:08
  • 1
    @nslntmnx does it come installed by default on 18.04 desktop, only only server? Desktop does not have /etc/cloud/cloud.cfg. What is the package name? Just cloud-init? – Ciro Santilli OurBigBook.com Oct 30 '18 at 09:27
  • 1
    Also, is there any way to change hostname with cloud-init instead of just disabling its override? – Ciro Santilli OurBigBook.com Oct 30 '18 at 09:33
  • 1
    Yes you can change many settings using cloud-init - see the docs link in my answer - here's a link to setting the hostname: https://cloudinit.readthedocs.io/en/latest/topics/modules.html#set-hostname – Pierz Oct 30 '18 at 10:32
  • If you do not want to disable it, but want hostname changed: sudo cloud-init clean && sudo nano /var/lib/cloud/seed/nocloud*/user-data (update "hostname:") -- reboot. – B. Shea Apr 05 '20 at 18:11
17

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}')"
Lucas
  • 665
  • 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
  • 4
    @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 04: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 07 '15 at 21:25
15

Without restart:

  1. change hostname in /etc/hostname
  2. update /etc/hosts accordingly
  3. sudo sysctl kernel.hostname=mynew.local.host

Check your current hostname with hostname -f

Tombart
  • 903
  • With 16.04, sysctl sudo: unable to resolve host oldname, sysctl: setting key "kernel.hostname": Read-only file system. Maybe because is a CT inside Proxmox. – Pablo Bianchi May 22 '18 at 22:44
7

To get your current hostname:

cat /etc/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.

muru
  • 197,895
  • 55
  • 485
  • 740
tagMacher
  • 650
  • 8
  • 14
6

The classical answer to the original poster's question is that, once you've edited /etc/hostname, you make it apply without restart by running hostname(1) with the -F (--file) option as root:

sudo hostname -F /etc/hostname

The hostname(5) handling with /etc/hostname and the said program has been the same in Debian and its derivatives for over twenty years now, and the package providing it has been tagged essential and required, and IIRC the init scripts have literally used the same thing for decades (/etc/init/hostname.conf still contains it), so I have to say I am genuinely puzzled how nobody had mentioned this already :)

  • Warning: won't work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot. – null Sep 01 '18 at 07:04
  • @nslntmnx erm, what? What new dependency did Ubuntu 18 introduce to pull in cloud-init by default? You may be thinking of some cloud-tailored images, in which case it's should not come as a surprise that they run custom cloud provisioning tools. – Josip Rodin Sep 01 '18 at 11:02
5

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

Jekis
  • 258
  • Warning: won't work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot. – null Sep 01 '18 at 07:08
4
  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 127.0.1.1 with the desired hostname (you can edit with sudo nano /etc/hosts)
  3. Execute sudo service hostname restart; sudo service networking restart
George
  • 417
  • Warning: won't work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot. – null Sep 01 '18 at 07:04
  • @nslntmnx are you sure about this? I see in the cloud-config.txt `# if you do nothing you'll end up with:

    * /etc/hostname (and 'hostname') managed via: 'preserve_hostame: false'

    if you do not change /etc/hostname, it will be updated with the cloud

    provided hostname on each boot. If you make a change, then manual

    maintenance takes over, and cloud-init will not modify it.`

    I currently don't have Ubuntu 18 so I cannot test it until the weekend.

    – George Sep 05 '18 at 20:27
4

Ubuntu 16.04

This is without restart and without any terminal use.

  • Go to System Settings -> Details.
  • There it is. Beside Device name, there is a text-box.
  • Edit text-box and close window.

Open terminal. See for yourselves.

(For older versions, the text-box is not editable.)

Zanna
  • 70,465
shonku
  • 49
3
sudo hostname your-new-name
sudo /etc/init.d/networking restart

That should do the job I think

OmarQunsul
  • 47
  • 2
3

I have read the answers, But I think Probably you are looking for this:

Just execute these two commands after editing the /etc/hostname file.

$ sudo service hostname restart
$ exec bash

That's all. No need to restart.Also make sure you also change the name in /etc/hosts file.

Raman
  • 855
  • 1
  • 9
  • 24
  • Unlike many other answers, this doesn't update /etc/hosts (I confirmed on 14.04). – IsaacS May 22 '18 at 00:19
  • Warning: won't work with Ubuntu 18+ which is running cloud-init by default, which controls hostname on boot. – null Sep 01 '18 at 07:04
  • while this does work, it should be noted it only applies to the current terminal window and not the overall X session – Tcll Jul 18 '22 at 22:01