Ubuntu Server 18.04 – Setting the hostname

How to build a Docker Compose YAML files

During installation, you were asked to create a hostname for your server. The default during installation is ubuntu, but you can (and should) come up with your own name. If you left the default, or if you want to practice changing it, we’ll work through that in this section.

In most organizations, there is a specific naming scheme in place for servers and networked devices. I’ve seen quite a few variations, from naming servers after cartoon characters (who wouldn’t want a server named daffy-duck?), to Greek Gods or Goddesses. Some companies choose to be a bit boring and come up with naming schemes consisting of a series of characters separated by hyphens, with codes representing which rack the server is in, as well as its purpose. You can create your own naming convention if you haven’t already, and no matter what you come up with, I won’t judge you.

Your hostname identifies your server to the rest of the network. While the default ubuntu hostname is fine if you have just one host, it would get confusing really quickly if you kept the default on every Ubuntu Server within your network. Giving each server a descriptive name helps you tell them apart from one another. But there’s more to a server’s name than its hostname, which we’ll get into in Chapter 7, Setting Up Network Services, when we discuss DNS. But for now, we’ll work through viewing and configuring the hostname, so you’ll be ready to make your hostname official with a DNS assignment, when we come to it.

So, how do you view your hostname? One way is to simply look at your shell prompt; you’ve probably already noticed that your hostname is included there. While you can customize your shell prompt in many different ways, the default shows your current hostname. However, depending on what you’ve named your server, it may or may not show the entire name. Basically, the default prompt (known as a PS1 prompt, in case you were wondering) shows the hostname only until it reaches the first period. For example, if your hostname is dev.mycompany.org, your prompt will only show dev. To view the entire hostname, simply enter the hostname command:

Output from the hostname command

Changing the hostname is fairly simple. To do this, we can use the hostnamectl command as root or with sudo. If, for example, I’d like to change my hostname from dev.mynetwork.org to dev2.mynetwork.org, I would execute the following command:

sudo hostnamectl set-hostname dev2.mynetwork.org 

Simple enough, but what does that command actually do? Well, I’d love to give you a fancy outline, but all it really does is change the contents of a text file (specifically,
/etc/hostname). To see this for yourself, feel free to use the cat command to view the contents of this file before and after making the change with hostnamectl:

cat /etc/hostname 

You’ll see that this file contains only your hostname.

The hostnamectl command didn’t exist before Ubuntu switched to systemd (versions 15.04 and earlier). If you’re using a version earlier than that, you’ll need to edit /etc/hostname manually.

Once you change your hostname, you may start seeing an error message similar to the following after executing some commands:

unable to resolve host dev.mynetwork.org 

This error means that the computer is no longer able to resolve your local hostname. This is due to the fact that the /etc/hostname file is not the only file where your hostname is located; it’s also referenced in /etc/hosts. Unfortunately, the hostnamectl command doesn’t update /etc/hosts for you, so you’ll need to edit that file yourself to make the error go away. Here’s what a /etc/hosts file looks like on a typical server:

Sample contents from a /etc/hosts file

By using a text editor and editing this file, we can change this occurrence of the hostname. Considering the extra work of changing our hostname, I see little benefit in using hostnamectl over manually editing /etc/hosts and /etc/hostname, so the choice is pretty much yours. You’ll end up using a text editor to update /etc/hosts anyway, so you may as well perform the update the same way for both.

Comments are closed.