DEV Community

Cover image for How to Set or Change System Hostname in Linux
Ayekple Clemence
Ayekple Clemence

Posted on • Originally published at anansewaa.com

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 :

hostnamectl
Enter fullscreen mode Exit fullscreen mode

Sample outputs:

   Static hostname: host_name
         Icon name: computer
        Machine ID: bd8978fcd141f40f56c5759700000d24
           Boot ID: 4ce02f818334497a85bc81c962d6bf4b
  Operating System: Ubuntu 16.04.4 LTS
            Kernel: Linux 4.4.0-124-generic
      Architecture: x86-64
Enter fullscreen mode Exit fullscreen mode

Setting or Changing the hostname

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

hostnamectl set-hostname 'new_host_name'
Enter fullscreen mode Exit fullscreen mode

To verify it simply type:

hostnamectl
Enter fullscreen mode Exit fullscreen mode

You have just changed 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:

hostname

host_name
Enter fullscreen mode Exit fullscreen mode

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

hostname new_hostname
Enter fullscreen mode Exit fullscreen mode

Replace “new_hostname” with the hostname you want to set. The problem with this approach is the system hostname will change immediately, but after restarting, 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:

$ sudo hostnamectl set-hostname new_hostname
Enter fullscreen mode Exit fullscreen mode

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

# nano /etc/hostname
Enter fullscreen mode Exit fullscreen mode

Then, add the record for the hostname in:

# nano /etc/hosts
Enter fullscreen mode Exit fullscreen mode

For example:

127.0.0.1 new_hostname
Enter fullscreen mode Exit fullscreen mode

Then, restart the hostname by running the command below:

# /etc/init.d/hostname restart
Enter fullscreen mode Exit fullscreen mode

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

# nano /etc/sysconfig/network
Enter fullscreen mode Exit fullscreen mode

Here is a sample of that file:

/etc/sysconfig/network
NETWORKING=yes
HOSTNAME="new_hostname"
GATEWAY="192.168.0.214"
GATEWAYDEV="eth0"
FORWARD_IPV4="yes"
Enter fullscreen mode Exit fullscreen mode

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

Conclusion

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

Top comments (1)

Collapse
 
agakpe profile image
agakpe

Hi Clem

Would you like to touch base with me @agakpe@hotmail.com. Could use your services.

Thanks