How to Change Your Hostname on Ubuntu 16.04

Posted on by Kurtis Foley | Updated:
Reading Time: 6 minutes

Image result for ubuntu logo

Times are changing, and quite possibly, your hostname is as well if you are reading this article.  You may have come across a scenario within the business that requires you to change your hostname.  You might ask yourself why you would need to change your hostname? The most common scenarios would be due to a domain name change, your business has changed its course, or because you have thought of something better.

Sometimes you might forget to renew the domain names before they expire. Unfortunately, this can be a time when a domain broker purchases the domain name out from under you.  These are agencies that take popular sites and purchase with the intent of holding the domain until their inflated price is met.  As unfortunate as this may be, sometimes it is best to purchase a new domain name for cost efficiency.

Auto Renew has been updated

When purchasing domains from Liquid Web you can always select the option to Auto-Renew within our portal Domains >> My Domains

Whatever the cause, it is worthwhile to change your server hostname to keep it in line with your current domain name. We have put together steps to change your hostname in Ubuntu 16.04.

Benefits to using a Fully Qualified Domain Name for your Hostname

It is good practice to use your FQDN Fully Qualified Domain Name as your hostname. Following this practice creates more options for securing your hostname with an SSL.  This will allow services like email to function using a secured connection. Using a hostname with a registered domain will allow you to add a corresponding DNS entry.  This will prevent unpredictable behavior by some services that use the hostname. This would allow you to set up a reverse lookup DNS entry. It can be very important especially with services like email verification.  For example, when an email is sent the receiving server runs a reverse lookup on the sender’s hostname. The reverse lookup allows the receiver’s server to ensure the hostname resolves to the matching IP address. This is just one preventive measure servers now used to reduce email spoofing incidents.

By using a unique domain name, you can reduce editing time. You may have a script that calls to the servers IP, instead of the hostname, to correctly function.  The best practice is to use the hostname because future migrations may change IP addresses/ranges.  Using the hostname can save you a lot of time eventually, depending on your infrastructure and coding.

Using SSH for Windows 10, 7/8, and Mac OS X

To change your server hostname on Ubuntu 16.04, we will need to connect to your VPS server.  For this article, we will be using SSH “Secure Shell” to access the server and issue commands.  SSH is a powerful tool that will allow us to establish a secure connection with your server, diagnose, and issue remote commands.  For more information on the SSH protocol, you can visit the following link for our New User SSH Tutorial.

There are a few ways to use SSH depending on your operating system. We’ve have included some examples below followed by links with more information.

Windows 10

Using SSH client in Windows 10

Note: Because the OpenSSH client was introduced in the Windows 10 Fall Creators Update, you’ll need to first update to at least that version of the operating system.

Windows 7/8

Unfortunately, for older versions of Windows, it is not exactly possible to set up an SSH natively to connect to your server.  Thankfully, applications were created to assist. We like to use MobaXterm, but Putty is a safe choice as well. Both of these applications are free to use and simple to set up. We’ve included links below with more information on these applications.

Mac OS X

Newer Mac operating systems come with an excellent utility to access SSH called Terminal. To access Terminal navigate to your Applications folder >> Utilities folder >> Terminal.

In case the Terminal is inefficient for your preference, there are other options available in the App Store or through a quick search on Google. Putty is also available on Mac!

Changing the Hostname in Ubuntu 16.04

At this point, you should be able to access your server using SSH.  Once you have accessed your server, you will want to either switch to the root user or run these commands using sudo.  The files you will be accessing are owned by root. Because of this, you will need root privileges.

To start things off, we will want to edit /etc/hostname and the /etc/hosts files.  You can do so by using a text editor of your choice. We will demonstrate how to accomplish this task using the text editor called VIM.  Some of these command-line text editors can seem complicated, we will include the sed command to make things even easier.

Here is the syntax for switching to the root user.

# su – root  

Here is the syntax for editing the hostname and hosts file.

# vim /etc/hostname  

# vim /etc/hosts  

# vim /etc/hosts  

Once you have opened these files, you will need to change your hostname in Ubuntu 16.04 as follows:

  1. Press the i key to insert.  This will allow you to edit.  You will notice the editor says Insert at the bottom of the page.
  2. Use the arrow keys to navigate the cursor to your old hostname.
  3. Backspace to delete single characters
  4. Replace with the new hostname.  Be sure the syntax is correct.editor with insert at the bottom
  5. When done editing hit the ESC key to exit insert mode.
  6. Then hold shift and press the : key
  7. Finally, type wq and press enter key. This will write to the file and quit the editor
  8. Repeat for /etc/hostname

As we mentioned earlier, the command line text editors can appear to be overly complicated, especially when you’re used to programs like Word and the Window’s text editor.  Because of this, we have included the command below.

Change to your old hostname. Change to your new hostname

# sed -i 's/' /etc/hosts

# sed -i 's/' /etc/hostname

After editing these files, you’ll need to reboot the server. If you wish to reboot at a later time but still want your new hostname to take immediate effect click on this sentence to skip ahead. Otherwise, you can do so by running the following.

# reboot

Your SSH session should be terminated.  Depending on your server it can take a few minutes to boot back up.  Once the server is back online you can check your changes by running the following command.

# hostname  

If all went well, the terminal should output your new hostname.

terminal output with new hostname

If you wish to reboot at a later time but still want your new hostname to take immediate effect, you can use the hostname command to temporarily set the hostname until the next reboot.  From there, the changes in /etc/hosts and /etc/hostname will take permanent effect.

# hostname

# hostname

# hostname output

There is also an alternative available.  The  hostnamectl  command is the default for both Desktop and Server versions. They combine setting the hostname via the  hostname  command, editing /etc/hostname and setting the static hostname. Unfortunately, editing  /etc/hosts still has to be done separately.


# hostnamectl set-hostname

Common Issue after Hostname Update

Failed to start hostname.service: Unit hostname.service is masked

The “Failed to start hostname.service: Unit hostname.service is masked” error can happen when there is a syntax error within the /etc/hostname or /etc/hosts files, or when the hostname does not match between these two files.  Be sure to check both of these files for mistakes and correct them as needed. In newer versions of Ubuntu, you will also want to use the hostnamectl command mentioned earlier.  

# hostnamectl set-hostname

Once corrected, be sure to start the hostname service to see if the issue has been corrected. You can do so by running the command that we have included below. Afterward, we would recommend rebooting your server.  This is not always necessary, but in some cases, it is required.

# systemctl restart hostname  

Changing your hostname in Ubuntu 16.04 is easy to do. As always, Liquid Web customers enjoy 24/7 technical support with changing your hostname. If you are not hosting with us, reach out to our sales team to see how you can get into our lightning-fast servers today!

About the Author: Kurtis Foley

Have Some Questions?

Our Sales and Support teams are available 24 hours by phone or e-mail to assist.


Latest Articles

What is Object Storage?

Read Article

What is Formik?

Read Article

Five Steps to Create a Robots.txt File for Your Website

Read Article

Premium Business Email Pricing FAQ

Read Article

Microsoft Exchange Server Security Update

Read Article