How to Change Your Server Hostname on Ubuntu

Learn how to change your Ubuntu server hostname correctly using hostnamectl and avoid common configuration issues.

Setting a proper hostname is a small but important step in VPS setup. This guide shows how to change hostname on an Ubuntu server correctly so your system, services, and control panels work as expected.

Table of Contents

What Is a Hostname and Why It Matters

A hostname is the name assigned to your server. It helps identify your system on a network and is used by services like mail servers, control panels, and logs.

Using a generic hostname like ubuntu or localhost can cause issues later, especially when setting up email or tools like Virtualmin.

A proper hostname usually looks like:

				
					server.yourdomain.com
				
			

This is called a fully qualified domain name (FQDN).

This is part of our VPS Setup series. See more in VPS Basics & Preparation.

When Should You Set the Hostname?

You should set your hostname:

If you haven’t secured your server yet, follow Ubuntu Initial Server Security Guide first.

Check Current Hostname

First, check your current hostname:

				
					hostnamectl
				
			

You’ll see something like:

Set a New Hostname

Use hostnamectl to set your new hostname:

				
					sudo hostnamectl set-hostname server.yourdomain.com
				
			

Replace server.yourdomain.com with your actual domain.

Update /etc/hosts File

Edit the hosts file:

				
					sudo nano /etc/hosts
				
			

Find the line:

				
					127.0.1.1    old-hostname
				
			

Replace it with:

				
					127.0.1.1    server.yourdomain.com server
				
			
Ubuntu hosts file

This ensures your hostname resolves correctly locally.

Verify Changes

Check the hostname again:

				
					hostnamectl
				
			

Also verify:

				
					hostname
				
			

Both should return your new hostname.

Reboot (Optional but Recommended)

Some services may require a reboot:

				
					sudo reboot
				
			

Common Mistakes to Avoid

Avoid these common issues:

Your hostname should match your domain’s DNS settings if you plan to host websites or email.

What to Do Next

After setting your hostname, you are ready to install your web server stack.

Next step:

• Install Virtualmin for easier management
OR
• Set up a manual LEMP stack

Follow: Install Virtualmin + Nginx on Ubuntu 24.04 to continue.

Conclusion

Setting the correct hostname ensures your server behaves properly across services and avoids issues later. It’s a simple step, but important for stability and compatibility with hosting tools.

If you’re following the full setup process, continue here: VPS Setup Beginner’s Guide

Tharindu

Hey!! I'm Tharindu. I'm from Sri Lanka. I'm a part time freelancer and this is my blog where I write about everything I think might be useful to readers. If you read a tutorial here and want to hire me, contact me here.

Related Articles

5 Comments

  1. Hi Tharindu
    I have a new VPS and trying to run this command sudo nano /etc/hostname
    but it is showing
    sudo: nano: command not found

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button