Linux Change Hostname

To change the hostname of a Linux system, you can follow these steps:

  1. Log in to the system with root or sudo access.
  2. Open the /etc/hostname file with a text editor such as nano or vi.
    sudo nano /etc/hostname
  3. In the file, replace the current hostname with the new hostname that you want to set. Save and close the file.
  4. Open the /etc/hosts file with a text editor.
    sudo nano /etc/hosts
  5. In the file, look for the line that contains the current hostname and update it with the new hostname. For example:
    127.0.0.1 localhost old-hostname

    should be updated to:

    127.0.0.1 localhost new-hostname
  6. Save and close the file.
  7. Restart the networking service to apply the changes.
    sudo systemctl restart network

    If the above command does not work, you may need to use the appropriate command for your distribution, such as sudo service network restart.

  8. Verify the new hostname by running the hostname command.
    hostname

    This should output the new hostname that you just set.

Leave a Comment