How to Set or Change System Hostname in Linux

Device or system hostnames are used to easily recognize a machine within a network in a human readable format. It is not much of a surprise, but on Linux system, the hostname can be easily changed by using simple command as “hostname“.

Read Also: How to Set Static IP Address and Configure Network in Linux

Running hostname on its own, without any parameters, will return the current hostname of your Linux system like this:

$ hostname
TecMint

If you want to change or set hostname of your Linux system, simply run:

$ hostname NEW_HOSTNAME

Of course, you will need to replace “NEW_HOSTNAME” with the actual hostname that you wish to set. This will change the hostname of your system immediately, but there is one problem – the original hostname will be restored upon next reboot.

There is another way to change the hostname of your system – permanently. You might have already figured it out that this will require change in some configuration files and you will be correct.

Set System Hostname Permanently in Linux

Newer version of different Linux distributions such as latest Ubuntu, Debian, CentOS, Fedora, RedHat, etc. comes with systemd, a system and service manager that provides a hostnamectl command to manage hostnames in Linux.

To set system hostname on SystemD based distributions, we will use hostnamectl command as shown:

$ sudo hostnamectl set-hostname NEW_HOSTNAME

For Older Linux distributions, which uses SysVinit in short init, can have their hostnames changed by simply editing the hostname file located in:

# vi /etc/hostname

You then have to add another record for the hostname in:

# vi /etc/hosts

For example:

127.0.0.1 TecMint

You then need to run:

# /etc/init.d/hostname restart

On RHEL/CentOS based systems that use init, the hostname is changed by modifying:

# vi /etc/sysconfig/network

Here is a sample of that file:

/etc/sysconfig/network
NETWORKING=yes
HOSTNAME="tecmint.com"
GATEWAY="192.168.0.1"
GATEWAYDEV="eth0"
FORWARD_IPV4="yes"

To keep a permanent hostname change the value next to "HOSTNAME" to the one of your hostname.

Conclusion

This simple article meant to show you a simple Linux trick and I hope that you learned something new.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Marin Todorov

I am a bachelor in computer science and a Linux Foundation Certified System Administrator. Currently working as a Senior Technical support in the hosting industry. In my free time I like testing new software and inline skating.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

4 Responses

  1. Zoiddy says:

    Great help. Very clear. Thanks.

  2. Rajesh Adhikari says:

    Thanks for the hostnamectl command

  3. ashok says:

    I want to delete the hostname.

  4. Alex says:

    Nice work, thanks

Leave a Reply to Rajesh Adhikari Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.