Jun 112018


Os / Linux

How to change the hostname in Ubuntu

  • 11 June 2018
  • ADM




How to change the hostname in Ubuntu - images/logos/ubuntu.jpg


Here is how to change the hostname in Ubuntu.

Option 1 - Temporary

To change the hostname temporary, until the first restart run the following command in the terminal:

[adm@web1]$ sudo hostname admfactory.com

To check the result run the hostname command without any parameters

[adm@web1]$ sudo hostname

Option 2 - Permanent

To make the change permanent you need to edit the /etc/hostname file. To do this you can use any editor from the command line:

$ sudo vi /etc/hostname

Additionally, you need to edit the /etc/hosts file and change all the lines that refer to the old hostname:  admfactory.com
::1        admfactory.com

Note: not changing the /etc/hosts file you might end up having some commands and application to stop working.

To test this run the following command:

[adm@web1 ~]$ sudo service hostname start

if you restart the server now you should be able to to see the change in the terminal:

[adm@admfactory ~]$

By applying first tho options you don't need to restart the server, the first option will ensure that the hostname is changed until the first restart and second option will ensure that after restart the server will have the proper hostname set up.

Option 3 - since Ubuntu 13.04

You can change the hostname using the hostnamectl command.

[adm@web1 ~]$ sudo hostnamectl set-hostname admfactory.com

To check the result run the command:

[adm@web1 ~]$ sudo hostnamectl
   Static hostname: admfactory.com
         Icon name: computer-vm
           Chassis: cm
        Machine ID: 300e1439545546cca7521ae681aca56f
           Both ID: 84c3731be8b442f6a736e705e97a8e4d
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-862.3.2.el7.x86_64
      Architecture: x86-64

Note that the command might not be available to all distributions as is part of the systemd-services.