Ubuntu 18.04 LTS change hostname permanently

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

You need to use the hostnamectl command to see or set the Ubuntu 18.04 LTS system’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 hostname or computer name on Ubuntu.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Ubuntu Linux version 18.04 LTS
Est. reading time 2 minutes

How to display the current Ubuntu hostname

Simply type the following command:
$ hostnamectl
Ubuntu 18.04 LTS see current hostname command

Ubuntu 18.04 LTS change hostname permanently

The procedure to change the computer name on Ubuntu 18.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 one.
  3. Reboot the system to changes take effect:
    sudo reboot

How to change hostname on Ubuntu 18.04 without a system restart

Let us see current setting just type the following command:
$ hostnamectl
Next change hostname from localhost to u1804-nixcraft, enter:
$ hostnamectl set-hostname u1804-nixcraft
Verify new changes:
$ hostnamectl

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

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 40:9f:38:28:f6:b5 brd ff:ff:ff:ff:ff:ff
    inet brd scope global noprefixroute enp6s0
       valid_lft forever preferred_lft forever
    inet6 fe80::429f:38ff:fe28:f6b5/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: u1804-nixcraft
Make sure you replace any occurrence of the old computer name with your new one too. Save and close the file in vim.


You learned how to change hostname permanently when using Ubuntu 18.04 LTS as per your needs.

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

🐧 4 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
4 comments… add one
  • DOES NOT WORK Aug 24, 2020 @ 6:31


    • 🐧 Vivek Gite Aug 24, 2020 @ 8:47

      First, cursing doesn’t work. Are you using cloud server like AWS? Are you using Ubuntu Linux? There might be settings in /etc/cloud/cloud.cfg file and Append the following line to the same file:
      preserve_hostname: true

  • Andrew Podkhvatilin Feb 14, 2021 @ 18:36

    It’s works. Thanks a lot.

  • Flemming Mar 31, 2021 @ 15:34

    Fairly odd that *not* using sudo will change it for the running instance.
    Just what I needed, thanks.

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