Linux Administrator

How to Change Hostname on Ubuntu and Debian System

Hostname is the program that is used to either set or display the current host, domain or node name of the system. These names are used by many of the networking programs to identify the machine.

In this article I will show how you can change your hostname on Ubuntu and Debian systems.

Change Hostname

In the latest operating system such as Ubuntu 18.04, 16.04, Debian 9/8 running with systemd uses hostnamectl command to manage computer’s hostname.

Using following command you can change the hostname of linux system, For example you need to set computer hostname to workstation.

$ sudo hostnamectl set-hostname workstation 

This will update /etc/hostname file on your system and load in the current environment. But you still need hostname binding with localhost IP.

Also edit the /etc/hosts file and add new hostname with localhost IP address like:

127.0.1.1 workstation

Now, run the below command to find the current hostname of your system.

$ hostnamectl 

   Static hostname: webhost1
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 844c6ba3394d4646bd27ba204be67f27
           Boot ID: 5030cf55956a4c32ad3d6b7f391a3c77
    Virtualization: xen
  Operating System: Ubuntu 16.04.3 LTS
            Kernel: Linux 4.4.0-1049-aws
      Architecture: x86-64

Change Hostname Manually

You can also change your system hostname manually by editing configuration file directory. Edit the /etc/hostname file write your system hostname there as show below.

# vim /etc/hostname
workstation

Save and close file. After editing the file, you can also set the hostname for the current session to avoid system reboot.

# hostname workstation

Now edit /etc/hosts file to bind new hostname with localhost IP address.

127.0.1.1  workstation

More Example

Example-1:

To print the hostname of the system :

# hostname
output:
# hostname
testserver.looklinux.com

Example-2:

To print IP address of the computer :

# hostname -i
output:
192.168.0.5

Example-3:

To print the domain name :

# hostname -d
output:
looklinux.com

Example-4:

To print short hostname :

# hostname -s
output:
looklinux
Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment