OSETC TECH

Ubuntu Linux Permanently Change Hostname

This post will guide you how to change the hostname of your Ubuntu Linux system. How to change the current hostname “devops-VirtualBox” to “devops-osetc” on your Ubuntu Linux 16.04? How do I set the Ubuntu hostname from A to B in the command line interface under Ubuntu system?

You will learn that how to change hostname of Ubuntu system temporarily and how to change the hostname of your Ubuntu system permanently from this tutorial.

Check the Current Hostname of Ubuntu

If you want to get the current hostname information, you can try to execute the hostname command in the command line interface of Ubuntu system. Or you can cat the /etc/hostname file to check the hostname of your system.

Type the following command:

$ hostname

or

$ cat /etc/hostname

Outputs:

devops@devops-VirtualBox:~$ hostname
devops-VirtualBox
devops@devops-VirtualBox:~$ cat /etc/hostname
devops-VirtualBox

Change Hostname of Ubuntu Temporarily

If you just want to change the hostname of ubunt system temporarily, or it only works in the current session, after you reboot the system, the changing will be lose. Type the following command in the command line interface:

$sudo hostname devops-osetc

Outputs:

devops@devops-VirtualBox:~$ sudo hostname devops-osetc
[sudo] password for devops:
devops@devops-VirtualBox:~$ hostname
devops-osetc

Change hostname of Ubuntu permanently

If you want to change the hostname permanently, you can modify the /etc/hostname configuration file, then add the newly hostname in. just do the following steps:

#1 edit /etc/hostname file using vi or vim command

#2 delete the current name and enter into the newly name

#3 reboot the system so that the changes can take effect.

Note: if you do not want to restart your Ubuntu system, just run the following command to restart hostname service to apply changes:

$systemctl restart systemd-logind.service

4# you shoud also edit the /etc/hosts file to do domain name resolution. And it will map ip address to host name, change the hostname and make it same as the name in /etc/hostname file.

Change Hostname Using Hostnamectl Command

You can also simply use the hostnamectl command to set the hostname of your Ubuntu system.

Check current system information

Type the following command:

$hostnamectl

Outputs:

root@devops-osetc:~# hostnamectl
Static hostname: devops-osetc
Icon name: computer-vm
Chassis: vm
Machine ID: 059a07d442e846d4b85243bd416818a8
Boot ID: 51e3098dcfe34b7dbcd5fe22d8ead885
Virtualization: oracle
Operating System: Ubuntu 16.04.2 LTS
Kernel: Linux 4.8.0-36-generic
Architecture: x86-64

If you want to change the current hostname from devops-Virtualbox to devops-osetc, just run the below command:

$ hostnamectl set-hostname devops-osetc