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 (Credit Card) or Bitcoin (BTC Wallet: 1M161JGAkz3oaHNvTiPFjNYkeABox8rb4g).

Xmodulo © 2020 ‒ About ‒ Powered by DigitalOcean