How to Change Hostname in Linux: A Guide

  • Last updated: Nov 05, 2024
  • Views: 30
  • Author: Admin

When working with the Linux operating system, you may need to change the hostname. hostname is a unique name for a device that identifies it on the network. A correct and convenient hostname not only helps improve the network structure, but also simplifies administration, especially if there are several Linux devices on the network. In one of the most popular versions of Linux, you can change the hostname in several ways, each of which has its own advantages.

In this article we will carry out all the work on the Ubuntu operating system, but do not worry if you have another system installed, for example, CentOS or Fedora, all the actions that I will show are also suitable for other operating systems.

The article on the page is based on my many years of experience with this technology and is presented to you as a short instruction.

To avoid problems when changing the hostname, it is best to have sudo privilege or perform all actions as root.

 

Contents of the article:

  1. Checking the current hostname.
  2. The hostnamectl command.
  3. Hostname command.
  4. File /etc/hostname.

 

1. Checking the current hostname.

Before changing the hostname to a new name, let's first see what our server is currently called. To view the current name, use the command:

~# hostnamectl status

hostnamectl status

We are interested in the first line - "Static hostname" - this will be our current server name.


 

2. The hostnamectl command.

The hostnamectl command provides a convenient interface for changing the hostname. It allows you to specify three types of hostname:

  • static hostname - Specifies a permanent device name that is saved in configuration files and used across system reboots.
  • transient hostname - Specifies a temporary name that can change during operation and is reset after reboot.
  • pretty hostname - Specifies a pretty name that can contain spaces and special characters for ease of perception.

Let's say we want to change the server hostname to a new name, for example new-ubuntu. To do this, use the command:

~# hostnamectl set-hostname new-ubuntu

hostnamectl set hostname

As a result, we set a new server name without rebooting the system.


 

3. Hostname command.

The hostname command allows you to temporarily change the server name. After the server reboots, the server name will be reset to the previous name. To temporarily change the server, use the command:

~# hostname ubuntu

hostname


 

4. File /etc/hostname.

The last option would be to change the file /etc/hostname. The hostname file stores the current server name. Open the file and write the new server name there. For the changes to take effect, you will need to reboot the system or run the command:

~# systemctl restart systemd-hostnamed


 

In summary, changing the hostname in Linux can be done in a variety of ways, depending on your needs. You can make temporary changes with the hostname command, make permanent changes by editing the /etc/hostname files, or use more modern commands such as hostnamectl.

 

SIMILAR ARTICLES