How to change hostname without reboot on Linux

In order to change hostname on Linux, you need to update a configuration file containing hostname, and then let the kernel know that hostname has been changed. The latter step is needed since the kernel still remembers an old hostname until the next reboot. In order to change hostname without reboot, do the following.

To change hostname on CentOS/RedHat without reboot:

$ sudo hostname [my_host_name]
$ sudo service syslog restart

If you want to make this hostname change permanent on CentOS/RedHat, you need to do the following.

$ sudo vi /etc/sysconfig/network
HOSTNAME=[my_host_name]

To change hostname on Debian/Ubuntu without reboot, you first put a new host name in /etc/hostname.

$ sudo vi /etc/hostname
[my_host_name]

Then, run the following to make the change effective.

$ sudo /etc/init.d/hostname.sh start

An alternative distro-independent method to change hostname at runtime is to update hostname via /proc pseudo-file system.

$ sudo echo [my_host_name] > /proc/sys/kernel/hostname

You can verify whether hostname has been changed successfully by running:

$ hostname

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.
Your name can also be listed here. Write for us as a freelancer.

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *