How to change hostname without reboot on Linux

Last updated on November 21, 2020 by Dan Nanni

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.

Change Hostname without Reboot on CentOS or RHEL

On CentOS/RHEL 6 or earlier:

You can change hostname temporarily with the following commands:

$ sudo hostname [my_host_name]
$ sudo service syslog restart

If you want to make this hostname change permanent, you need to edit the following configuration.

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

On CentOS/RHEL 7 or later:

Refer to this guideline to change hostname with hostnamectl command.

Change Hostname without Reboot on Ubuntu or Debian

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

Change Hostname Without Reboot in Other Linux Systems

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

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean