How to Change the Hostname in Ubuntu 16.04

Posted on by Leya Laiju | Updated:
Reading Time: 3 minutes

A hostname is a human-readable string that allows people to identify a server or computer on a network. It may include a fully qualified domain name (FQDN) but identifies a computing device in a unique and absolute manner.

A domain is a specific Internet address for websites. A fully qualified domain name provides its precise location in the domain name system (DNS) hierarchy of records.

There are some rules specified for hostname:

  • Only letters, digits, the hyphen character, and the period character are allowed in the hostname. 
  • The hostname must start and end with a letter or a number.
  • The length of the hostname must be between 1 and 63 characters.
  • Use a hostname that will not be used as a domain name for a specific website.
  • Check that the domain is registered and that it is socially acceptable.

There are many reasons you may need to change the hostname for your device in Ubuntu 16.04. This tutorial will provide several ways for how to change the hostname in your Linux device.

Prerequisites

  • Server running on Ubuntu 16.04.
  • A text editor such as Vim.
  • Root access or a user with sudo privileges.

How to Change the Hostname in Ubuntu 16.04

Change the Hostname in Ubuntu 16.04 Using VIM

Step 1: Check the Current Hostname

To check the current hostname of your Ubuntu system, there are two available commands to be run as root.

root:host.olddomain.com~# hostname
host.olddomain.com

or

root@host.olddomain.com:~# cat /etc/hostname
host.olddomain.com

Step 2: Edit the Hostname Configuration File

Edit the hostname configuration file. To open the hostname configuration file, type the following command.

# vim /etc/hostname

1. To enter Insert mode, press the i key.

2. Replace your old hostname with the new hostname.

root@ip-172-31-35-54:~# vim /etc/hostname
host.olddomain.com
~                                                                                                                                                                  ~                                                                                                                                                                   ~                                                                                                                                                                  ~                                                                                                                                                                   ~                                                                                                                                                                   ~                                                                                                                                                                   ~                                                                                                                                                                   ~                                                                                                                                                                   	 
~                                                                                                                                                                  ~                                                                                                                                                                   ~
-- INSERT --
host.newdomain.com
~                                                                                                                                                                   ~                                                                                                                                                                   ~                                                                                                                                                                   ~                                                                                                                                                                   ~                                                                                                                                                                   ~                                                                                                                                                                   ~                                                                                                                                                                   ~                                                                                                                                                                   ~                     	 
-- INSERT -- 

3. To exit insert mode, press the ESC key.

4. To write to the file and exit the editor, use the :wq! Command.

5. Next, open the /etc/hosts configuration file, using the vim /etc/hostscommand.

6. Replace the old hostname with the new hostname.

127.0.0.1 localhost
127.0.0.1 host.newdomain.com

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
~                                                                                                                                                                   ~                                                                                                                                                                  ~                                                                                                                                                               ~                                       	                                                                                                                                                               	 
-- INSERT --

7. To exit Insert mode, press the ESC key and save the changes using the :wq! command.

8. After you've finished editing the files, reboot your server to apply the changes.

root@host:~# reboot

9. Now, verify the new hostname using the command below.

root@host:~# hostname
host.newdomain.com

Change the Hostname in Ubuntu 16.04 Using the sed Command

Another way to change the hostname is using the sed command. In this example, host.olddomain.com is the old hostname of the server, and host.newdomain.com is the new hostname. 

You can use the command below to change the hostname.

# sed -i 's/host.olddomain.com/host.newdomain.com/g'/etc/hostname
# sed -i 's/host.olddomain.com/host.newdomain.com/g' /etc/hosts

You'll need to reboot the server after editing these files.

# reboot

When the server is back up and running, run the following command to verify your changes.

root@host:~# hostname
host.newdomain.com

Change the Hostname in Ubuntu 16.04 Using SSH

We'll need to connect to your server in order to change the hostname of your server in Ubuntu 16.04. For this example, we are using secure shell (SSH) to connect to the server and issue commands. SSH is a powerful tool that will allow you to establish a secure connection with your server, diagnose problems, and issue remote commands.

First, you will need to connect to your server using SSH.
Using the hostnamectl command, you can easily modify the hostname. Use the flag, set-hostname, with this command to change the hostname as seen in the example below.

# hostnamectl set-hostname host.newdomain.com

This command will change and set the /etc/hostname file automatically.  The only thing left to do is to update the /etc/hosts file with the new hostname. There is no need for a reboot with this method.
You can verify the hostname change using the hostnamectl command.

# hostnamectl

Common Issues After Changing the Hostname

A common error you may encounter after changing the hostname is the failed to start hostname.service unit hostname.service is masked error. This occurs when there is a mismatch in the hostname in the /etc/hostname file and the /etc/hosts file, causing the system to fail the hostname lookup.

The simplest approach to fix this mistake is to make sure that the hostname in both files is the same. You'll need to examine the contents of each of these files to do so. Use the hostnamectl command.

# hostnamectl set-hostname host.newdomain.com

After you've double-checked that the hostname in your respective files is the same, try restarting the hostname.service again.

# systemctl restart hostname

Wrapping Up

The process to change the hostname in Ubuntu 16.04 is very simple and easy. Use this tutorial to guide you through the process, ensuring your hostname stays current for your business purposes.

Liquid Web assists with this process for existing clients. If you have yet to set up your VPS Hosting, Cloud Dedicated Server, or Dedicated Server with us, contact our sales team for assistance.

Avatar for Leya Laiju

About the Author: Leya Laiju

Leya Laiju holds a bachelor's degree in computer science and has three years of experience as a system engineer. In addition, she has extensive experience in Linux administration, web hosting, and DNS management.

Latest Articles

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article