The hostname of a computer is its network identifier. In Ubuntu, it is visible in the terminal and is set during installation. Changing the hostname might be necessary to reflect a new role or adhere to a naming convention.
The hostname is stored in the /etc/hostname file. You can update it by editing this file or using the hostnamectl command. Using hostnamectl is the preferred method because it updates all necessary configurations.
After changing the hostname, confirm the update by checking the /etc/hostname file and running the hostname command. Rebooting the system ensures the new hostname is applied system-wide.
Steps to change system hostname in Ubuntu:
- Open the terminal.
- Check the current hostname using the hostname command.
user@host:~$ hostname host
- Change the hostname using the hostnamectl command.
user@host:~$ sudo hostnamectl set-hostname newhostname [sudo] password for user:
- Verify the new hostname in the /etc/hostname file.
user@host:~$ cat /etc/hostname newhostname
- Confirm the hostname change by running the hostname command.
user@host:~$ hostname newhostname
- Run a new shell to see the change in the shell prompt.
user@host:~$ bash user@newhostname:~$
This only works if $PS1 is set to display the hostname.
- Reboot the system to apply the change across the entire system (optional).
user@newhostname:~$ sudo reboot
This guide is tested on Ubuntu:
Version | Code Name |
---|---|
22.04 LTS | Jammy Jellyfish |
23.10 | Mantic Minotaur |
24.04 LTS | Noble Numbat |
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.