Ubuntu Linux Change Hostname

On Ubuntu, the hostname is the name used to identify your computer or server on the system and, depending on your network configuration, on the local network. You can view the current hostname with hostname or hostnamectl and change it permanently from the Terminal.

For current Ubuntu systems that use systemd, the simplest method is hostnamectl set-hostname. This tutorial also retains the manual method of editing /etc/hostname and /etc/hosts, which is useful for understanding where the hostname configuration is stored and referenced.

Check the Current Hostname in Ubuntu

Before changing the hostname, check the current value. The hostname command prints the system hostname:

</>
Copy
hostname

You can also use hostnamectl to display hostname information maintained by systemd:

</>
Copy
hostnamectl

Look for the Static hostname value in the output. In the example used throughout this tutorial, the existing hostname is ubuntu-desktop and the new hostname will be ubuntu-pc.

Change Ubuntu Hostname Permanently with hostnamectl

On Ubuntu versions that use systemd, including modern Ubuntu desktop and server releases, use hostnamectl set-hostname to set the static hostname.

</>
Copy
sudo hostnamectl set-hostname new-hostname

For example, to change the hostname from ubuntu-desktop to ubuntu-pc, run:

</>
Copy
sudo hostnamectl set-hostname ubuntu-pc

The command normally produces no output when it succeeds. Verify the new hostname with:

</>
Copy
hostnamectl --static
ubuntu-pc

You can also run hostname to verify the active hostname. A terminal prompt that still displays the previous hostname may need a new terminal session or a new login before the prompt itself is refreshed.

Update /etc/hosts After Changing the Ubuntu Hostname

Changing the hostname with hostnamectl updates the system’s static hostname, but it does not necessarily update hostname entries that you have in /etc/hosts. If that file contains the old hostname, update the matching entry so local name resolution remains consistent.

Open the file with:

</>
Copy
sudo nano /etc/hosts

On many Ubuntu installations, you may see an entry similar to:

</>
Copy
127.0.1.1    ubuntu-desktop

If so, replace only the old hostname with the new one:

</>
Copy
127.0.1.1    ubuntu-pc

Do not remove unrelated addresses or host mappings from /etc/hosts.

Steps to Change Hostname Manually in Ubuntu

The following steps show the manual method of changing the Ubuntu hostname by editing /etc/hostname and the corresponding hostname entry in /etc/hosts. The screenshots use Ubuntu 20.04 LTS and change the hostname from ubuntu-desktop to ubuntu-pc.

Step 1: Open Terminal by using Ctrl+Alt+T shortcut key or from Applications.

Ubuntu Change Hostname

The Terminal title and prompt in this example read sony@ubuntu-desktop. Here, sony is the username and ubuntu-desktop is the hostname. You can also check the hostname using the hostname command.

Step 2: Change the static hostname stored in /etc/hostname. In this example, we will change it from ubuntu-desktop to ubuntu-pc. Open /etc/hostname with nano using administrator privileges.

$ sudo nano /etc/hostname
Ubuntu Change Hostname

Enter your password if prompted. When entering a password for sudo, the Terminal normally does not display characters or asterisks. Type the password and press Enter.

Ubuntu Change Hostname

The /etc/hostname file opens in nano. It normally contains the static hostname. Replace ubuntu-desktop with ubuntu-pc.

Ubuntu Change Hostname

Step 3: Change the hostname to your desired name.

Ubuntu Change Hostname

Press Ctrl+X to exit nano. When nano asks whether to save the modified buffer, choose to save the change.

Ubuntu Change Hostname

Type y or Y and press Enter.

Ubuntu Change Hostname

Press Enter again to confirm the filename. The new static hostname is now stored in /etc/hostname.

Step 4: Update the old hostname in /etc/hosts if an entry for it is present.

Ubuntu Change Hostname

Open the file using nano editor with root access (sudo). Run the following command in Terminal.

$ sudo nano /etc/hosts
Ubuntu Change Hostname

Locate the line that maps the old hostname. In the example shown in the screenshot, replace ubuntu-desktop with ubuntu-pc. The hostname referenced in /etc/hosts should match the new hostname when that entry is intended to represent the local machine.

Ubuntu Change Hostname

Change the hostname and press Ctrl+X.

Ubuntu Change Hostname

Press y or Y, and then press Enter.

Ubuntu Change Hostname

Press Enter again to confirm the filename. The matching hostname entry in /etc/hosts is now updated.

Step 5: When using this manual file-editing method, rebooting is a straightforward way to ensure services and sessions start with the new hostname. Run the following command in Terminal, or reboot using the desktop interface.

Ubuntu Change Hostname

After the system starts again, verify the hostname using hostname or hostnamectl.

Verify the New Ubuntu Hostname

After changing the hostname, use more than one check if you want to confirm both the active hostname and the configured static hostname.

</>
Copy
hostname
hostnamectl --static
cat /etc/hostname

For the example in this tutorial, all three should report ubuntu-pc.

If you updated /etc/hosts, you can also check the relevant local mapping with:

</>
Copy
grep ubuntu-pc /etc/hosts

Ubuntu Hostname Naming Rules to Follow

For a simple static hostname, use a short DNS-compatible name consisting of letters, digits, and hyphens. Avoid spaces and shell-special characters. A practical example is ubuntu-pc, while a name such as Ubuntu PC should not be used as the static hostname.

If the computer participates in DNS, Active Directory, configuration management, cloud-init, or another network-management system, check that system’s naming requirements before changing the hostname. Such services can depend on the configured name or may manage it themselves.

Change Ubuntu Hostname Without Rebooting

When you use hostnamectl set-hostname, the active and persistent hostname can normally be changed without rebooting the entire Ubuntu system:

</>
Copy
sudo hostnamectl set-hostname ubuntu-pc

Run hostname immediately afterward to check the active value. Existing applications, remote sessions, or shell prompts may continue displaying a cached or previously established hostname until they are restarted or a new login session is created.

Ubuntu Hostname Change Command Summary

TaskCommand or file
Show the current hostnamehostname
Show systemd hostname informationhostnamectl
Change the hostname permanentlysudo hostnamectl set-hostname new-hostname
Show the configured static hostnamehostnamectl --static
Store the static hostname manually/etc/hostname
Update a local hostname mapping when required/etc/hosts

Ubuntu Hostname Change Tutorial Summary

In this Linux Tutorial, we learned how to check and change the hostname of an Ubuntu Linux PC. For current systemd-based Ubuntu installations, sudo hostnamectl set-hostname new-hostname is the direct method for changing the static hostname permanently. We also covered the manual /etc/hostname and /etc/hosts method and commands for verifying the resulting hostname.

Ubuntu Hostname Editorial QA Checklist

  • Confirm that hostnamectl set-hostname is presented as the primary method for current systemd-based Ubuntu releases.
  • Confirm that the tutorial does not claim hostnamectl automatically rewrites hostname entries in /etc/hosts.
  • Confirm that the old and new example hostnames remain consistently identified as ubuntu-desktop and ubuntu-pc.
  • Confirm that the manual method distinguishes /etc/hostname from the local name mappings stored in /etc/hosts.
  • Confirm that rebooting is not incorrectly described as mandatory after a successful hostnamectl set-hostname command.
  • Confirm that verification includes both the active hostname and the persistent static hostname.
  • Confirm that hostname examples avoid spaces and use DNS-compatible letters, digits, and hyphens.
  • Confirm that systems managed by cloud-init, DNS, directory services, or configuration-management tools are not assumed to behave like an unmanaged desktop installation.