Config Server Firewall

How to Change Hostname on Ubuntu Server and Desktop

Changing your computer's hostname is a simple yet important step for identifying your machine on a network. Whether you're working with an Ubuntu server or a desktop, the process is straightforward using the hostnamectl command.

1. Check Current Hostname

To begin, you'll want to see your current computer name. You can run hostnamectl status to check the current computer name.

hostnamectl status

Also, you can check your current hostname by simply typing hostname in the terminal.

Check Hostname on Ubuntu
Check Hostname on Ubuntu

2. Change the Hostname

To change the hostname, type hostnamectl set-hostname followed by the new hostname you want.

sudo hostnamectl set-hostname <new-hostname>

The hostname can be a short name like myserver, or a fully qualified domain name like myserver.example.com if you have one.

Examples

sudo hostnamectl set-hostname myserver
sudo hostnamectl set-hostname myserver.example.com

Also, instead of set-hostname, you can just use hostname. Both commands work on Ubuntu and other Debian-based Linux systems.

sudo hostnamectl hostname myserver.example.com

After changing the hostname, a reboot isn’t strictly required, but it ensures all services pick up the new name.

3. Update the /etc/hosts File

After changing the hostname with hostnamectl, you should also update the /etc/hosts file.

127.0.1.1 myserver myserver.example.com
Update the hosts File

When Will the Change Take Effect?

And in case you're wondering — changing the hostname doesn’t update the shell prompt right away. You’ll need to log out and back in, or start a new terminal session to see the new hostname.

/etc/hostname File (Optional Info)

The hostname in Linux is stored in the /etc/hostname file. When you use the hostnamectl command to change it, this file gets updated automatically. It's not recommended to edit it manually — always use hostnamectl when possible.