Ubuntu set hostname permanently (computer name) command

last updated in Categories ,

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

The procedure to change hostname is as follows on Ubuntu:

  1. Open the terminal application
  2. Login to remote Ubuntu server using the ssh user@server-ip-here
  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 display current hostname command

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
Ubuntu set hostname permanently command

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 vivek@aws-server-ip-here

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 16 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
  16. CentOS 8 Linux set hostname permanently (computer name) command

Posted by: Vivek Gite

The author is the creator of nixCraft 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.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

  1. How to change hostname in Ubuntu 18.04 LTS server?
    hostnamectl set-hostname "your-name-here"

    Still, have a question? Get help on our forum!