Ubuntu set hostname permanently (computer name) command

I am a new Ubuntu Linux server user. My server is running on AWS cloud. By default they set my VM name to ‘ip-172-26-12-173’ during creation of VM but now I would like to change the computer name to ‘wsl-us-west2-www1’. How do I set hostname permanently on Ubuntu Linux 18.04 LTS? Can you tell me more about Ubuntu command to set hostname permanently?

Introduction : You can use the hostnamectl command to see or set the 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 Linux.

Ubuntu set hostname permanently (computer name) command

kbd { display: inline-block;
margin: 0 .1em;
padding: .1em .6em;
font-family: Arial,”Helvetica Neue”,Helvetica,sans-serif;
font-size: 11px;
line-height: 1.4;
color: #242729;
text-shadow: 0 1px 0 #FFF;
background-color: #e1e3e5;
border: 1px solid #adb3b9;
border-radius: 3px;
box-shadow: 0 1px 0 rgba(12,13,14,0.2), 0 0 0 2px #FFF inset;
white-space: nowrap;}

The procedure to change hostname is as follows on Ubuntu:

  1. Open the terminal application
  2. Login to remote Ubuntu server using the ssh [email protected]
  3. To change hostname (Computer Name) in Ubuntu type: hostnamectl set-hostname wsl-us-west2-www1
  4. Verify new change by typing hostnamectl

Let us see all commands in details.

How to find current hostname on Ubuntu

Type the following hostnamectl command
$ hostnamectl

Ubuntu change hostname permanently

Simply type the following command:
$ sudo hostnamectl set-hostname wsl-us-west2-www1
Verify new changes with the following command:
$ hostnamectl
One can use the cat command too:
$ cat /etc/hostname

Do I need to reboot my Ubuntu VM to change take effect?

No. You do not need to reboot the Linux box. Just log in again using the ssh command and your PS1 (bash shell prompt) will be updated too:
$ ssh [email protected]

Ubuntu changed hostname permanently and verified with ssh

Conclusion

You learned how to use the hostnamectl command to query and change the Ubuntu system hostname and related settings.

This entry is 13 of 15 in the Linux/Unix Set hostname Tutorial series. Keep reading the rest of the series:

  1. Ubuntu Linux Change Hostname (computer name)
  2. Debian Linux: Change Hostname / Computer Name Permanently
  3. Linux change my hostname / computer system name
  4. Linux Change Hostname
  5. RHEL / Centos Linux 7: Change and Set Hostname Command
  6. OpenBSD Change Hostname
  7. CentOS Linux 5/6 Change Hostname Command
  8. Linux setting hostname and domain name of my server
  9. FreeBSD Change Hostname without reboot
  10. SuSe Linux Change Hostname without reboot
  11. How to set the hostname on Fedora Linux
  12. Ubuntu 18.04 LTS change hostname permanently
  13. Ubuntu set hostname permanently (computer name) command
  14. OpenSUSE Linux set hostname permanently (computer name) command
  15. RHEL 8 Linux set hostname permanently (computer name) command

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.