How To Change Hostname In Linux

In Linux you may want to change the hostname of your server or desktop PC from time to time. A common example is if you’re re-purposing a server or you made a typo when installing the operating system and want to reset the hostname.

Here we will cover how the hostname can be edited in Red Hat Enterprise Linux (RHEL)/CentOS and Debian flavors of Linux, the process generally differs slightly depending on the distribution of Linux in use.

Before you begin

This information applies regardless of Linux distribution covered below.

  • Only root has the permissions required to change the hostname.
  • Running the ‘hostname’ command by itself will display the current hostname.
  • Running the ‘hostname name‘ command will temporarily rename the running systems hostname to what ever is specified as the first aguement, in this instance ‘name‘. This will not persist over reboot however, to persist over a reboot a file on disk will need to be modified.

How to change the hostname in RHEL/CentOS based Linux distributions

This process differs slightly between RHEL/CentOS 6 and 7 as the process in 7 has been greatly simplified, steps for both are provided below.

RHEL/CentOS 6

  1. Edit the /etc/sysconfig/network file with your favourite text editor. This file contains the hostname of the server after the ‘HOSTNAME=’ variable. Simply replace the old hostname with the new hostname and save the file.
  2. Edit the /etc/hosts file so that the local hostname will resolve to the localhost IP address. Just add the hostname on the end of the 127.0.0.1 and ::1 lines, if you have any other IP addresses here referring to the old hostname update these to the new hostname.
  3. Run the ‘hostname name‘ command, replacing name with your new hostname.
  4. Next you can either take the easy way out and simply reboot the system to apply the changes, or alternatively run the below command. Note that running this command will briefly interrupt network connectivity, however this process should be faster than rebooting the system.
    /etc/init.d/network restart
    
  5. After the reboot has completed or you have run the above command, run the ‘hostname’ command which will display the currently set hostname to confirm that your new hostname appears.
  6. Don’t forget to update any other specific references of the old hostname to the new hostname, for instance DNS and any other application specific configuration which may exist on your system.

RHEL/CentOS 7

  1. Run the ‘hostnamectl set-hostname name‘ command, where name is the new hostname for the system. You can now either run the traditional ‘hostname’ command to view the hostname, or run the newer ‘hostnamectl status’ command which will display additional information. No reboot or network restart should be required.
  2. Edit the /etc/hosts file so that the local hostname will resolve to the localhost IP address. Just add the hostname on the end of the 127.0.0.1 and ::1 lines, if you have any other IP addresses here referring to the old hostname update these to the new hostname.
  3. Don’t forget to update any other specific references of the old hostname to the new hostname, for instance DNS and any other application specific configuration which may exist on your system.

You can find further information regarding the use of hostnamectl here.

How to change the hostname in Debian based Linux distributions

  1. Edit the /etc/hostname file with your favorite text editor. This is the file that contains the hostname of the server, simply edit the current hostname to the new one and save the file.
  2. Edit the /etc/hosts file so that the local hostname will resolve to the localhost IP address. By default there should already be an entry with your current hostname so just edit this to the new hostname.
  3. Next you can either take the easy way out and simply reboot the system to apply the changes, or alternatively run the below commands. Note that running these commands will briefly interrupt network connectivity, however this process should be faster than rebooting the system.
    invoke-rc.d hostname.sh start
    invoke-rc.d networking force-reload
    invoke-rc.d network-manager force-reload
    
  4. After the reboot has completed or you have run the above commands, run the ‘hostname’ command which will display the currently set hostname to confirm that your new hostname appears.
  5. Don’t forget to update any other specific references of the old hostname to the new hostname, for instance DNS and any other application specific configuration which may exist on your system.

Summary

As shown setting the hostname in Linux is quite simple. Just be aware that over time the hostname may have inserted itself into many places throughout the system such as various configuration files and DNS settings, all of which should also be updated appropriately.

  1. no use of above description

  2. Thanks for posting this, but I still have an issue on a Ubuntu VM.
    Whenever I reboot, the hostname reverts to the original hostname.
    Still searching.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>