If you’ve worked in the admin world for any length of time, you’ve probably run into an instance where you needed to change the hostnames on your server to match some corporate naming standard, but you can’t have downtime either. So how do you change the hostname without rebooting?
Changing the Hostname
First, you must change the config file that controls this. The actual file and its location will vary across distributions. In the Redhat derivatives, the file to modify is /etc/sysconfig/network so vi this file and change the line that reads HOSTNAME=
For SuSE distributions you would similarly change the /etc/HOSTNAME file while Debian admins would modify the /etc/hostname file to set the new name.
Next, the /etc/hosts file needs to be changed. This is identical across all flavors and consists of using your favorite text editor and modifying the old hostname within. Such as from this:
“But”, you mutter under your breath, “I just typed in `hostname` and got ‘Snoopy’ as a result. It doesn’t work!”.
What you need to know is that the previous steps are to permanently set the new name. You are much too quick to judge, obviously. So, continue on by entering (at the command line as root or a sudoer):
This will set the hostname for now but the change is not saved (that is what the above steps are for, remember?). Go ahead – test it. Type in `hostname` (without the tickmarks ) and the command should return ‘mdmvr14s9db’ (without the single quotes).
Now let’s set it in stone. If you are using chkconfig and service commands (RedHat family folk, usually), you can simply type
which will restart the network with the new host name. The rest of you should be able to type
to obtain the same results.
Then test again with the hostname command – you should get the new name returned.
This has been the easy part. Hopefully, prior to starting this, you checked all application conf files for any hard coded reference to the new name and changed that as well. And you need to then change your DNS ‘A’ record to also reflect the new name. And once you have restarted named with the new name on the DNS you will have succeeded in temporarily irritating around half of those 18,000 users previously mentioned who now can’t get to the server since the name has not propagated around the network yet. But this too shall pass as long as you hide long enough.
And at least you can keep the small stuffed plush Snoopy sitting at your desk to remind you of that gentler, simpler time.
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?
14 Answers 14
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:
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.
After a restart your changes in /etc/hostname will be used, so (as you said in the question), you should still use
(or some other editor) so that file contains the hostname.
To test that the file is set up correctly, run:
You should also edit /etc/hosts and change the line which reads:
so that it now contains your new hostname. (This is required otherwise many commands will cease functioning.)
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.
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.
Changing the hostname or computer name in ubuntu without restart
Edit /etc/hostname and change to the new value,
Edit /etc/hosts and change the old 127.0.1.1 line to your new hostname
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:
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).