Need to change your Ubuntu PC/laptop or server’s computer name? Here’s how to do the trick in 4 different ways!
Computer name (aka hostname) is a label on computer network that is used to identify the device. After upgrading system edition or moving server location, you may change the hostname. It’s harmless and useful to make it easy to identify your host.
Linux hostname is consist of letters, numbers, "-", "_" and/or ".". But, can not begin or end in dot. And, it has at most 64 characters!
Option 1: Change Computer Name in Ubuntu Desktop:
For Ubuntu with default GNOME desktop, it’s easy to change the computer name using system settings (aka Gnome Control Center).
Just open settings and navigate to About page in the left. You may then click on ‘Device Name‘ and set a new name via pop-up dialog.
Option 2: Change Hostname Temporarily until reboot
It’s possible to change computer temporarily, so it works until you restart the Ubuntu machine.
Simply open terminal (Ctrl+Alt+T) or connect to your Ubuntu server, then paste the command below and hit Enter:
hostname NEW_NAME
Option 3: Change Hostname Permanently Without Reboot
Most Linux, including Ubuntu, Debian and Fedora, has a command line tool hostnamectl
to control system hostname. With it, you may change the Linux computer name permanently without restarting system.
While command in “Option 2” changes the dynamic (aka transient) hostname, the command below will change the static hostname in Linux:
hostnamectl set-hostname NEW_NAME
There’s also an OPTIONAL “pretty” hostname. It is human readable name present to the user, not the machine. If you want, change (or set, Ubuntu does not have one out-of-the-box) it via:
hostnamectl set-hostname "NEW_NAME" --pretty
The pretty hostname does not have above limitation. You may use any valid UTF-8 name
Option 4: Change hostname by editing config file (need restart)
User may also change the computer name by editing the configuration file directly, though it needs system restart to take effect!
For static hostname, edit the “/etc/hostname” file via the command below, and replace the content with your new name:
sudo nano /etc/hostname
To save changes, press Ctrl+X, type y, and hit Enter.
(IMPORTANT) Edit /etc/hosts
No matter which method you did to change the hostname, it’s better to adjust “/etc/hosts” accordingly.
The file contains IP mappings, old hostname present in the file will make that line not working properly.
sudo nano /etc/hosts
Also save changes by pressing Ctrl+X, follow with y and hit Enter.
That’s all. Enjoy!