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.
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.
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.
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 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.
Newer macOS operating systems come with an excellent utility to access SSH called Terminal. Here are steps to access it:
- Navigate to your Applications folder.
- Navigate to the Utilities folder.
- 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.
First, open your preferred text editor to switch to the root user. We are using the VIM text editor.
# su - root
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.
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.
After editing the files, terminate your SSH session and reboot your server.
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.
If all went well, the terminal should output your new hostname.
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.
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.
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
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.
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.