How to Change Your Hostname in Ubuntu 16.04

Posted on by Ronald Caldwell | Updated:
Reading Time: 5 minutes

A hostname is the name of a server, computer, or device on a network. You may come across a scenario in business that requires a hostname change. You might also ask yourself why you would need to change your hostname. The most common situations would be due to a domain name change, a business course change, or because you simply thought of something better.

Sometimes you might forget to renew the domain names before they expire. The result is a domain broker purchasing the domain name out from under you. Domain brokers buy popular website names to hold the domain until a buyer pays their inflated price. 

No matter why you need to make the change, we have put together steps to change your hostname in Ubuntu 16.04. However, before we get into the steps, there are some key terms and concepts to understand.

What is a Fully Qualified Domain Name (FQDN)?

A fully qualified domain name (FQDN) is a domain name that gives its precise location in the hierarchy of the domain name system (DNS). It is good practice to use your FQDN as your hostname.

Why Use an FQDN?

An FQDN helps to increase your domain security, DNS predictability, and mitigate delays.

Increased Security

Using a Secure Sockets Layer (SSL) certificate helps secure your domain and hostname. SSLs offer a secured connection via Hypertext Transfer Protocol Secure (HTTPS), providing encryption and authentication for your web browsing sessions.

DNS Predictability

Using a hostname with a registered domain will allow you to add a corresponding DNS entry, preventing unpredictable behavior by some services that use the hostname. In addition, registered domain names used as the hostname will enable you to set up a reverse lookup DNS entry, essential with services like email verification.

For example, one preventive measure servers now use to reduce email spoofing incidents are reverse DNS lookups. When sending an email, 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.

Mitigate Delays

Additionally, you can reduce editing time by using a unique domain name. You may have a script that calls the server’s IP to function correctly instead of the hostname. 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 to Change Hostnames

To change your server hostname in Ubuntu 16.04, we’ll need to connect to your server. We will be using Secure Shell (SSH) to access the server and issue commands for this article. SSH is a powerful tool that will establish a secure connection with your server, help you diagnose problems, and allow you to issue remote commands.

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

Windows 10

Windows 10 uses OpenSSH, a suite of secure networking services based on the SSH protocol, to provide a secure connection to your server over an unsecured network. It was not introduced natively until the Windows 10 Fall Creators Update (version 1709).

If you wish to use OpenSSH natively, you’ll need to first update to at least that version of the operating system. Follow our Using SSH Client Natively in Windows 10 tutorial If, for some reason, you are not able to update to that version.

Windows 7/8

While older versions of Windows don't allow you to connect to your server via SSH, certain applications can help. MobaXterm and Putty are safe options that are free and simple to set up.

macOS

Newer macOS operating systems come with an excellent utility to access SSH called Terminal. Here are steps to access it:

  1. Navigate to your Applications folder.
  2. Navigate to the Utilities folder.
  3. Open Terminal.

While Terminal is a readily available utility, you can also download Putty or one of the options available in the App Store. 

How to Change Your Hostname

How to Change Your Hostname Using VIM

Now that you have accessed your server using SSH, switch to the root user or run these commands using sudo. The files you will be accessing are root user files that require root privileges.

Step 1

First, open your preferred text editor to switch to the root user. We are using the VIM text editor.

# su - root

Step 2

Update your hostname by editing the /etc/hostname and /etc/hosts files separately with the process below. 

.1. Open your hostname file using the # vim /etc/hostname command and press the i key to edit the file. The editor says Insert at the bottom of the file.

1-open-hostname-file

2. Navigate to your current hostname and replace it with your new hostname.

3. Press the ESC to exit insert mode.

4. Use the :wq command to write to the file and quit the editor.

5. Follow the same process to edit the hosts file by using # vim /etc/hosts.

Step 3

After editing the files, terminate your SSH session and reboot your server.

# reboot

Your server may take a few minutes to boot back up. Once the server is back online, check your changes by running the following command.

# hostname

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

2-new-hostname-terminal-output

How to Change Your Hostname Using the sed Command

Another way to change your hostname is to use the sed command. We have included the commands for using this method.

Note:
Your old hostname is host.oldhostname.com in our example. Change host.newhostname.com to your new hostname.

Input the following commands in the terminal window for the /etc/hostname and /etc/hosts files.

# sed -i 's/host.oldhostname.com/host.newhostname.com/g' /etc/hostname
# sed -i 's/host.oldhostname.com/host.newhostname.com/g' /etc/hosts

Once completed, reboot your server. You can confirm the changes using the hostname command.

Common Issue after Hostname Update

When there is a syntax error within either the /etc/hostname or /etc/hosts files, or when the hostname does not match between these two files, the Failed to start hostname.service: Unit hostname.service is masked error occurs, as shown below.

3-common-issue-after-hostname-update

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.

# hostnamectl set-hostname host.newhostname.com

Once corrected, be sure to start the hostname service to see if you resolved the issue. You can do so by running the systemctl command as seen below.

# systemctl restart hostname
Note:
It is recommended to reboot your server after making changes like these.

Conclusion

Changing your hostname is valuable and easy to do. We have discussed how to change your hostname and the tools you will use to do so. By following these steps and best practices, you will make quick work of changing your hostname so that you can get back to business.

Liquid Web customers can take advantage of 24/7/365 technical support to help change your hostname, amongst others. If you are not hosting with Liquid Web, reach out to our Sales team to see how you can get into our lightning-fast VPS, cloud dedicated, or dedicated servers today!

Avatar for Ronald Caldwell

About the Author: Ronald Caldwell

Ron is a Technical Writer at Liquid Web working with the Marketing team. He has 9+ years of experience in Technology. He obtained an Associate of Science in Computer Science from Prairie State College in 2015. He is happily married to his high school sweetheart and lives in Michigan with her and their children.

Latest Articles

How to Upload Information to VMware through SFTP

Read Article

Accessing Man Pages on Ubuntu 16.04 LTS

Read Article

TLS vs SSL: A Comparison

Read Article

Premium Business Email Pricing FAQ

Read Article

Microsoft Exchange Server Security Update

Read Article