An Ubuntu hostname is a unique identifier for your server. Despite what the name implies, these hostnames are primarily for internal consumption, and you won’t need to resolve it via a DNS server. So technically, there’s nothing stopping you from using a ridiculous hostname that doesn’t make sense. You could also use a hostname with a domain name that’s already taken if you want – though it might not be a good idea to do so.
In this article, I’ll show you how to change your hostname in Ubuntu, and how to verify that the hostname has indeed changed.
We’ll also look at different ways to do the same thing, in case you’re unable to use SSH, for example.
- Getting The Existing Hostname
- Changing The Ubuntu Hostname
- Using SSH to Change The Ubuntu Hostname
- GUI vs Command Line – Changing the Ubuntu Hostname Differences
- InterWorx Also Performs Additional Steps like WHM
Getting The Existing Hostname
To see what your current hostname is, you have a few options. For example, if you’re using WHM with your VPS server, you can log in to WHM and you’ll see your hostname displayed on top like this:
In the above example, my hostname is “vps51107.nodevm.com”. Note that I have no problems making this information public since my VPS hostname is for personal consumption only.
Another way to find the hostname is through SSH. Open an SSH terminal and use the “hostname” command as shown here:
You see that we get the same result as shown in the first example. If you want, you can get greater detail about your hostname and server using the following command:
As you can see, the “hostnamectl” command gives you more than just the hostname. It also shows you the OS you’re running, the kernel version, and more. Now that you know how to view the Ubuntu hostname, it’s time to see how to change it.
Changing The Ubuntu Hostname
You can change the Ubuntu hostname either through the command line or via the GUI. If you have access to WHM, then changing it from there might be the easiest option. Let’s examine both options one by one.
Using the GUI to Change The Ubuntu Hostname
First, log into WHM, and on the dashboard, use the search function to type in “hostname” and click the first result as shown here:
You can also navigate directly to the screen where you change your hostname on WHM by going to the “Networking Setup” section and clicking “Change Hostname”. Once you’re on the screen, you’ll see the option to change the hostname at the bottom, as shown here:
Save your changes, and you’re done. The change will take a few seconds, as the system reconfigures itself, stopping and restarting services. Once it’s over, you’ll get a summary report of what happened as shown here:
But you can accomplish the same thing via SSH if you don’t have access to WHM or are using a different web hosting control panel. Here’s how.
Using SSH to Change The Ubuntu Hostname
Using SSH, there are several ways and commands to change the Ubuntu hostname. One of these is only a temporary measure, and the hostname will be reset with the system restarts. The others are more permanent. Here’s a breakdown of how to use each tool.
Using “hostname” for a Temporary Change
We can use the “hostname” command to change the Ubuntu hostname temporarily. It’ll be reset once the server restarts. Why bother, you ask? Perhaps you need to debug an issue and want to see whether the hostname is the culprit. Or maybe you’re testing a new environment and want to make sure that all the services are working properly with the new hostname. Other reasons can include creating a temporary hostname while you clone the server, or maybe you’re giving a demonstration to an audience and want to use a different hostname for a while. There are any number of reasons why you might want to temporarily change the hostname, and the “hostname” command is perfect for that.
Using the “hostname” command to change the Ubuntu hostname is easy. Just use:
sudo hostname new-hostname
Here’s the command in action.
As you can see, the new hostname is “tempnewhostname”, and this will revert back to the old hostname once the system reboots.
Permanently Change the Ubuntu Hostname with hostnamectl
To permanently change the hostname in Ubuntu, we can use the “hostnamectl” command along with the “set-hostname” option. Here’s what it looks like:
sudo hostnamectl set-hostname new-hostname
This changes the hostname permanently instead of temporarily, compared to using the “hostname” command.
Update the /etc/hosts File
After changing the hostname using the command line to change the Ubuntu hostname, you should also update the entry in the /etc/hosts file. Open the file and change:
Unfortunately, not all Linux operating systems manage the hostname in the /etc/hosts file similarly. So while this might work for Ubuntu, it doesn’t work for others like CloudLinux.
Not changing the hostname in associated files like /etc/hosts can cause many problems if services like Apache attempt to resolve the old-hostname instead of the new one. And while it’s not technically necessary for a hostname to be a fully qualified domain name and under your control, it can cause problems if it isn’t.
GUI vs Command Line – Changing the Ubuntu Hostname Differences
In the above sections, I’ve shown you how to change the Ubuntu hostname using the WHM interface as well as two options when using the command line. However, while both of these change the hostname, they are not equal. Specifically, if you can, change the hostname in the GUI instead of the command line.
The reason why it’s better to change the Ubuntu hostname using WHM is that WHM doesn’t just change the hostname. It also carries out a number of associated operations that help with system stability and can ensure that existing services use the new hostname instead of the old one. For example, WHM does the following when you change the hostname:
- Updates system settings
- Restarts services like Apache, MySQL and sshd
- Re-issues SSL certificates
- Updates mail settings
- and more…
When you change the hostname using the command line, however, it does none of these things. If you want more, you have to restart the important services manually. Also, WHM uses some proprietary logic and domain-specific scripts to ensure that the new hostname is propagated everywhere.
While it’s possible to create a bash script for changing the hostname and restarting important services, you shouldn’t have to.
InterWorx Also Performs Additional Steps like WHM
WHM isn’t the only hosting control panel that takes additional steps to ensure system stability when you change the Ubuntu hostname. For example, NameHero offers InterWorx for free on new VPS systems, and when you change the hostname on InterWorx, the system performs many of the same tasks that WHM performs in the background in addition to changing the hostname. They’re not exactly the same tasks, of course, but both control panels do more than if you were to have used the command line, instead.
So that’s how we change the hostname in Ubuntu. If you’re using a web hosting control panel like WHM, I suggest you use that instead of changing it via the command-line, because the system performs a lot of associated operations that are useful. However, if you don’t have a GUI, you can change it using the command line as well. Just make sure you’ve covered all your bases!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!