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?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityI 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?
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.)
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.
Jul 25, 2013 at 15:05
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.
hostnamectl
doesn't exist. Guess some flavours of Ubuntu aren't using systemd yet.
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
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).
sudo: unable to resolve host old-hostname
. For that part, @jesperado solution works well.
Nov 23, 2012 at 10:54
/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
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?
Dec 19, 2015 at 11:22
sudo nano /etc/hosts
for the change to take effect; without sudo, just nano /etc/hosts
didn't save the change
Jan 30, 2018 at 23:30
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:
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.
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.
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
/etc/cloud/cloud.cfg
. What is the package name? Just cloud-init
?
Oct 30, 2018 at 9:27
sudo cloud-init clean && sudo nano /var/lib/cloud/seed/nocloud*/user-data
(update "hostname:") -- reboot.
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}')"
su $SUDO_USER -c "xauth add $(xauth list | sed 's/^.*\//'"$NEW_HOSTNAME"'\//g' | awk 'NR==1 {sub($1,"\"&\""); print}')"
do? Just curious.
Mar 10, 2014 at 12:22
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.
Without restart:
/etc/hostname
/etc/hosts
accordinglysudo sysctl kernel.hostname=mynew.local.host
Check your current hostname with hostname -f
sudo: unable to resolve host oldname
, sysctl: setting key "kernel.hostname": Read-only file system
. Maybe because is a CT inside Proxmox.
May 22, 2018 at 22:44
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.
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 :)
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
/etc/hostname
with the desired hostname (you can edit with sudo nano /etc/hostname
)/etc/hosts
, replace the entry next to 127.0.1.1 with the desired hostname (you can edit with sudo nano /etc/hosts
)sudo service hostname restart; sudo service networking restart
# 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.
This is without restart and without any terminal use.
Open terminal. See for yourselves.
(For older versions, the text-box is not editable.)
sudo hostname your-new-name
sudo /etc/init.d/networking restart
That should do the job I think
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.
/etc/hosts
(I confirmed on 14.04).