Ubuntu 20.04 LTS change hostname permanently

I am a brand-new Ubuntu Linux 20.04 LTS VM user. By default, my server name to ‘localhost’ during cloud installation, but now I would like to change the computer name to ‘ubuntu-2004-nixcraft’. Can you tell me how do I remove the localhost and set it to ubuntu-2004-nixcraft on Ubuntu 20.04 Linux? How do I change the Ubuntu computer name from ‘localhost’ to ‘ubuntu-2004-nixcraft’? Can you tell me more about Ubuntu Linux change hostname command?

One can use the hostnamectl command to view or set the Ubuntu 20.04 LTS server’s host name. The host name or computer name is usually at system startup in /etc/hostname file. Open the Terminal application and type the following commands to set or change the hostname or computer name on Ubuntu.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Ubuntu 20.04
Est. reading time 5 minutes

How to display the current Ubuntu hostname

Run the following command:
Of course, you can use the echo command/printf command to print hostname too:

echo "$HOSTNAME"
printf "%s\n" $HOSTNAME
cat /etc/hostname

Finally, try the cat command on /etc/hostname cat /etc/hostname

Ubuntu 20.04 LTS change hostname permanently

The procedure to change the computer name on Ubuntu 20.04 LTS:

  1. Type the hostnamectl command :
    sudo hostnamectl set-hostname newNameHere
    Delete the old name and setup new name.
  2. Next Edit the /etc/hosts file:
    sudo nano /etc/hosts
    Replace any occurrence of the existing computer name with your new hostname.
  3. Reboot the system to changes take effect:
    sudo reboot

Did you know? All Linux hostname must be from 1 to 63 characters long, and the entire hostname, including the dots, can be at most 253 characters long. Valid names for hostnames are letters from a to z, the digits from 0 to 9, and the hyphen (-). A hostname may not start with a hyphen.

How to change hostname on Ubuntu 20.04 without a system restart

Let us view current computer name setting, just type the following command:
Next change hostname from localhost to ubuntu-2004-nixcraft, enter:
sudo hostnamectl set-hostname ubuntu-2004-nixcraft
Verify new changes:

Find your public or private IP address by running the ip command:
ip a
ip a s eth0

12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:16:3e:24:b3:08 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet brd scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::216:3eff:fe24:b308/64 scope link 
       valid_lft forever preferred_lft forever

Update /etc/hosts file with that IP address such as using a text editor such as vim command/nano command:
$ sudo vi /etc/hosts
$ sudo nano /etc/hosts
Update/append: ubuntu-2004-nixcraft
Make sure you replace all occurrence of the old computer name with your new one too. Save and close the file in vim.

How to set pretty hostname on Ubuntu 20.04 LTS Linux

A pretty hostname is nothing bug human-readable UTF-8 machine name. For example, a name like “Vivek's ThinkPad Laptop” is a pretty hostname. It is useful to display to the user and does not suffer from internet domain names’ syntax limitations. You must configure static hostname in /etc/hostname for fallback purposes. For example:
$ hostnamectl set-hostname nixcraft-wks01 --static
$ hostnamectl set-hostname "nixCraft' workstation 01" --pretty

Verify it by typing the any one of the following hostnamectl command:
$ hostnamectl
$ hostnamectl status
$ hostnamectl status --static
$ hostnamectl status --pretty
## or use the cat command ##
$ cat /etc/machine-info
$ cat /etc/hostname

How to Change Hostname on Ubuntu 20.04 LTS Linux

Note: We can pass the empty string “” as the hostname to reset the selected hostnames to their default (usually “localhost”):
hostnamectl set-hostname ""

Changing computer name using GUI method

First, open the overview by pressing the Super key. Start typing the name of an application. In other words type “Settings” and you will see an app windows follows:

Next scroll down (left pane) and click on the “About“. Now, find the “Device Name” field:

Clicking the “Device Name” opens rename device box as follows:

Finally, in the Rename Device box, find and replace the current hostname with a new one. Make sure you confirm changing device name (computer name) by clicking the Rename button to permanently changes the hostname on Ubuntu 20.04 LTS laptop or desktop:

Summing up hostnamectl commands and options

Command Description Examples
status Show current hostname settings hostnamectl status
set-hostname NAME Set system hostname hostnamectl set-hostname server1
set-icon-name NAME Set icon name for host. An icon identifying this machine must set as per the XDG Icon Naming Specification hostnamectl set-icon-name computer-tablet
set-chassis NAME Set chassis type for host. Currently, we can use “desktop”, “laptop”, “convertible”, “server”, “tablet”, “handset”, “watch”, and “embedded”, as well as the special chassis types “vm” and “container” for virtualized systems. hostnamectl set-chassis tablet
set-deployment NAME Set deployment environment for host. Can be “development”, “integration”, “staging”, or “production”. hostnamectl set-deployment production
set-location NAME Set location for host. Typically values are, “home”, “office”, “data center racke 1, 2nd shelf”, “London, UK” hostnamectl set-location "My home office, NYC"
--help Show this help hostnamectl --help
--version Show package version hostname --version
--no-ask-password Do not prompt for password hostnamectl --no-ask-password
--host=[USER@]HOST Operate on remote host. This will use SSH to talk to the remote machine manager instance. hostnamectl --host=vivek@server1 status hostnamectl --host=vivek@server1 set-hostname www-server-1
--machine=CONTAINER Operate on local container. In other words, Execute operation on a local container. Specify a container name such as ‘app’ to connect to hostnamectl --machine=app status
--transient Only set transient hostname. The “transient” hostname is a dynamic host name maintained by the kernel. It is initialized to the static host name by default, whose value defaults to “localhost”. It can be changed by DHCP or mDNS at runtime. Hence, we avoid setting up transient hostname manually. hostnamectl set-hostname "box1" --transient
--static Set static hostname hostnamectl set-hostname "server1.cyberciti.biz" --static
--pretty Only set pretty hostname hostnamectl set-hostname "server1 at linode data center, mumbai" --pretty


You learned how to change hostname permanently when using Ubuntu 20.04 LTS server or desktop computer as per your project needs. Please see the following man pages:
$ man hostname
$ man 7 hostname
$ man machine-info
$ man hostnamectl
$ hostnamectl --help

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • Christopher Feb 28, 2021 @ 3:37

    Thanks for the help. I was able to change my computer name to ‘desktop’. Keep up the good work!

  • Jeret Lendman May 1, 2021 @ 22:05

    Hello. I have done all of what you show, but after rebooting, the host name is back to what it was.

    • 🐧 Vivek Gite May 1, 2021 @ 22:20

      Are you using cloud computing? If so make sure following set to false in /etc/cloud/cloud.cfg
      preserve_hostname must be false:

      preserve_hostname: false

      Save and close the file. Now set up hostname again:
      sudo hostnamectl set-hostname your-hostname-here
      Reboot the box:
      sudo reboot
      Test it:

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum