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.
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:
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.
To print the hostname of the system :
# hostname output: # hostname testserver.looklinux.com
To print IP address of the computer :
# hostname -i output: 192.168.0.5
To print the domain name :
# hostname -d output: looklinux.com
To print short hostname :
# hostname -s output: looklinux