How To Change Terminal Name In Ubuntu Command Line

This article explains how to change terminal name in Ubuntu command line. Users can use hostnamectl set-hostname command to change hostname on Ubuntu command line.

Read more to find out how to set or change the hostname on Ubuntu without restarting the system.

Changing the System Hostname with hostnamectl

NOTE: Before we begin, remember that only root users or users with sudo privileges can change/set/modify the system hostname using GUI or the hostnamectl command.

The hostnamectl command is used to control the system hostname. In addition to this, the hostnamectl may be used to query and change the system hostname and all its related settings.

This hostnamectl command distinguishes three different hostnames:

  1. the high-level “pretty” hostname which might include all kinds of special characters (e.g. “linuxledger’s Laptop”)
  2. the static hostname which is used to initialize the kernel hostname at boot (e.g. “linuxledger-laptop”)
  3. the transient hostname which is a fallback value received from network configuration.

It must be noted that if a static hostname is set, and is valid (something other than localhost), then the transient hostname is not used.

The hostnamectl command has the following 3 kinds of syntax for each of the three different hostnames respectively:

sudo hostnamectl set-hostname host.newname.com
sudo hostnamectl set-hostname "Your Pretty HostName" --pretty
sudo hostnamectl set-hostname host.example.com --static
sudo hostnamectl set-hostname host.example.com --transient

The command argument set-hostname NAME is sued to set the system hostname to NAME. By default, this will alter the pretty, the static, and the transient hostname alike; however, if one or more of –static, –transient, –pretty are used, only the selected hostnames are changed.

If the pretty hostname is being set, and static or transient are being set as well, the specified hostname will be simplified in regards to the character set used before the latter are updated. This is done by replacing spaces with “-” and removing special characters.

This ensures that the pretty and the static hostname are always closely related while still following the validity rules of the specific name.

Using hostnamectl set-hostname command to change hostname

For example, to change the system static hostname to linuxledger, you would use the following command:

sudo hostnamectl set-hostname linuxledger

Optionally, we can also set the pretty hostname to linuxledger’s laptop using the following command:

sudo hostnamectl set-hostname "linuxledger's laptop" --pretty

NOTE: hostnamectl does not produce output. On success, 0 is returned; a non-zero failure code otherwise. Also, the static hostname is stored in /etc/hostname, and the pretty hostname is stored in /etc/machine-info file.

Once executed, you can check the current hostname. To do so, run the hostnamectl command (without any argument) in terminal:

hostnamectl

Editing /etc/hostname File

We can also edit /etc/hostname file directly from CLI. Simply enter “sudo nano /etc/hostname” into your terminal and once the file is open, edit your existing hostname or add a new one on its own line. Save changes (CTRL + X), if asked type “yes” to save and apply the changes.

NOTE: When you change the hostname on Ubuntu system, it’s important to restart network services. This is done to ensure that the changes are applied correctly and no connectivity issues arises.

Run the following command to restart network services on Ubuntu (after changing your hostname):

sudo systemctl restart systemd-networkd.service.
sudo service networking restart

That’s all. Remember to choose a unique name which describes your identity.

Author: Team Linux Ledger

The team of Linux experts and open source enthusiasts who are passionate about sharing their expertise with you through in-depth tutorials, news, and updates on various aspects of open source in general and Linux in particular A team that helps you become a better Linux user.

Leave a Comment