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

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 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 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.


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

🐧 1 comment so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
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 jobs killall kill pidof pstree pwdx time
Searchinggrep 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
1 comment… add one
  • Po May 13, 2018 @ 13:13

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

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