How to Change Hostname on Ubuntu 18.04

  • Terminal access to Server, normally via SSH.
  • You will need to be logged in as a User with Superuser access.

Display Current Hostname

hostnamectl

# Hostnamectl returns the following

Static hostname: simpletimes
Icon name: computer-container
Chassis: container
Machine ID: 6d9c7cc322ee4c48af7c0ec3b492b5cc
Boot ID: 4d7e014869e54614ad71ee868e33837d
Virtualization: openvz
Operating System: Ubuntu 18.04 LTS
Kernel: Linux 4.20.0-042000-generic
Architecture: x86-64

As you can tell the current hostname is simpletimes and we are going to change it to allthingsnerd.

 

Change the Hostname

In Ubuntu 18.04 you can use hostnamectl to change system hostname and other various settings. You can read more here.

1. In this example I want to change the hostname to allthingsnerd.

sudo hostnamectl set-hostname allthingsnerd

This will return nothing, except when an error has occurred.

2. Now lets edit the /etc/hosts file.

sudo nano /etc/hosts

# sudo nano /etc/hosts returns the below. I have added the line <span style="background-color: #d3d3d3;">127.0.0.1 allthignsnerd</span> change this to your hostname.

127.0.0.1 localhost
127.0.0.1 allthingsnerd

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

3. Verify the change.

hostnamectl

# You should see your new server name printed on the console.

Static hostname: allthingsnerd
Icon name: computer-container
Chassis: container
Machine ID: 6d9c7cc322ee4c48af7c0ec3b492b5cc
Boot ID: 4d7e014869e54614ad71ee868e33837d
Virtualization: openvz
Operating System: Ubuntu 18.04 LTS
Kernel: Linux 4.20.0-042000-generic
Architecture: x86-64

That is it, you have configured your hostname. If you have any questions leave a comment below.

(Visited 6 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *