How to Set or Change System Hostname in Linux

If you’ve worked as a system admin for any length of time, you’ve probably run into a situation where you have to change the hostnames on your server to match some corporate naming convention, but you can’t have downtime either. So the question is how do you change the hostname without rebooting the server?

Luckily, for us changing the hostname of a Linux system is easy. It can be changed by using “hostname” command. 

Changing Hostname Using hostnamectl command in Ubuntu

Current Linux distros use systemd an init system used in Linux distributions to manage system processes and the user space.

Display the current hostname

Simply use this command :

Sample outputs:

Setting or Changing the hostname

You can change or set hostname of your Linux system by  running:

To verify it simply type:

You have just change your hostname using hostnamectl command.

Also read: How To Set Up mod_rewrite for Apache on Ubuntu

Setting or Changing Hostname in Linux Using hostname Command

Running hostname command without any parameters will display the current hostname of your Linux system like this:

You can change or set hostname of your Linux system, by running the command below:

Replace “new_hostname” with the hostname you want to set. The problem with this approach is the system hostname will change immediately, but after restart the old hostname will be restored.

There is a solution to that effect, thus changing the hostname permanently.

Change or Set Permanent Hostname on Linux Distributions

The current versions of Linux distributions like lastest Ubuntu, Debian, CentOS, Fedora, RedHat …, comes with systemd which is a service manager that gives hostnamectl command to manage hostnames on the various Linux distributions.

Setting hostname on systemd based distributions, use hostnamectl command as shown:

Setting hostname on earlier Linux distributions, which uses SysVinit in short init, you can edit the hostname file located in:

Then, add the record for the hostname in:

For example:

Then, restart the hostname by running the command below:

For RHEL/CentOS Linux distributions that use init, the hostname can be changed by editing:

Here is a sample of that file:

Changing the value next to "HOSTNAME" will make the hostname permanent.


I hope this article helped you in changing your hostname and you probably have learnt something new to add to your knowledge.


Clemence Ayekple

Let's grab a cup of coffee and talk about programming

Add comment

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

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Most discussed

%d bloggers like this: