change hostname ubuntu

The hostname is a name that identifies a device on a network. Ubuntu has some basic requirements when it comes to hostname validity.

It can be 253 characters long at most, and you may only use letters, numbers, or hyphens. Assuming these conditions are fulfilled, changing your hostname on Ubuntu is very easy.

System Settings

The first way to change your hostname is from the control center.

  1. Search and open ‘About’ from the Activities overview.
    ubuntu about system
  2. Click on the Device Name field. Set the new hostname here and press Rename
    change ubuntu device name

Hostnamectl

Hostnamectl is the standard tool for managing the system hostname and related settings on Ubuntu. We’ll use it to set anupspc as the new hostname. By default, this will alter the Static, Transient, and Pretty hostnames.

hostnamectl set-hostname anupspc

The static hostname is the standard hostname that we just set. It’s stored in the /etc/hostname file. The pretty hostname has fewer naming restrictions (UTF-8) as it’s intended to be read by users. The transient hostname is usually the same as static too, but it can change based on your network configuration (due to DHCP and mDNS servers).

If you want to set any of these individually, you can use the --static, --transient, or --pretty options. For instance, let’s set anup’spc as the pretty hostname for better readability. 

hostnamectl set-hostname --pretty “anup’spc” 

You can verify the change with hostnamectl or you can specify the exact hostname type like so

hostnamectl --pretty

Network Manager

NetworkManager’s CLI tools (nmtui/nmcli) also work fine for changing the static hostname. Nmcli is very straightforward.

sudo nmcli general hostname anupspc

If you want to use nmtui instead, start it with

sudo nmtui

Select Set System Hostname and press Enter. Set the new hostname and press Ok. Then, press Esc or select Quit to exit nmtui.

Edit Config Files

You can also directly modify the /etc/hostname and /etc/hosts files to change your hostname. 

sudo nano /etc/hostname

Set the new hostname here and write out the changes.

sudo nano /etc/hosts

Once again, replace the old hostname here and save the changes.

The downside to this method is that such configs are supposed to be modified using the utilities shown earlier. Due to this, NetworkManager doesn’t pick up manual changes to the config files until you restart.

Hostname

If you only need to change the hostname temporarily, you can use the hostname command like so  

sudo hostname anupspc

This will set anupspc as the transient hostname for the current session. Your system hostname will revert to the previous name upon restarting.

Anup Thapa

Senior Writer

Anup Thapa is a Linux enthusiast with an extensive background in computer hardware and networking. His goal is to effectively communicate technical concepts in a simplified form understandable by new Linux users. To this end, he mainly writes beginner-friendly tutorials and troubleshooting guides. Outside of work, he enjoys reading up on a range of topics, traveling, working out, and MOBAs.