Linux change server name

How to Change Hostname on Ubuntu & Other Linux Distributions

The hostname is basically your computer’s name. It can be used to identify and communicate your system on the same network.

Some Linux distributions and cloud service providers let you choose the hostname while installing. Some will automatically create one for you but you’ll always have a computer name.

If you don’t like the one you’ve got, you can use the following systemd command to change hostname on major Linux distributions:

hostnamectl set-hostname new_host_name

Read the rest of the article to learn more about hostname and ways to change it.

Before you see various ways to change the hostname, you should first know what’s a hostname!

What is a hostname in Linux?

A hostname is a name assigned to a “host” i.e. a computer on a network. The hostname is basically just your computer’s name. It’s used to identify your computer on the network.

You can see the hostname in the terminal using the hostname command.

check the current hostname in terminal

As you can see in the above screenshot, the username is abhishek while the hostname is itsfoss.

When you install Ubuntu, it asks you to choose a name for your computer. That’s the “hostname” for your system.

If at any point in time you feel like your computer doesn’t have a fancy or relevant name, you can easily change the hostname in Linux.

Note that you shouldn’t use the same hostname on two different computers on the same network, as this can cause problems. This hostname is supposed to uniquely identify a computer on a network.

There are two ways to change the hostname in Ubuntu:

Here I am assuming you have Ubuntu on your system, but you can try the same command line trick on Linux Mint and some other Debian distributions as they use the same configuration as Ubuntu.

Change hostname in Ubuntu & other Linux using command line

If you want to change the hostname in Ubuntu or any other Debian based Linux distribution, you can use the typical Linux way to do that, which is using the command line.

Now, there are several commands for changing the hostname in Linux. I’ll show you two such methods here.

Method 1: Change hostname using systemd tools

If your Linux system uses systemd, you can use the tools provided by systemd to change the hostname. To check your current hostname and some other information, use the command:

Static hostname: itsfoss-ubuntu Icon name: computer-vm Chassis: vm Machine ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Boot ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Virtualization: kvm Operating System: Ubuntu 22.04.1 LTS Kernel: Linux 5.15.0-47-generic Architecture: x86-64 Hardware Vendor: QEMU Hardware Model: Standard PC _Q35 + ICH9, 2009_

To change the hostname, you can use this option with the hostnamectl command:

Of course, replace the with your new hostname in the above command.

Читайте также:  Перенаправление всего трафика linux

changing the hostname through terminal command

Method 2: Change hostname with hostname command

Open a terminal and use the command below to change the hostname. Replace with the new hostname you want to use:

change the hostname using hostname command

Now if you use the command hostname, you’ll see that your hostname has been changed.

But the changes you just made are not permanent. If you reboot your system, the old hostname will be back.

To change your hostname permanently, you’ll also need to edit your /etc/hostname and /etc/hosts file, which is where Ubuntu, Mint and other Debian-based distributions store the hostname.

Since you are in terminal, you need to use a command line based text editor to edit these files like Vi or Vim. Ubuntu has Nano editor installed by default so you can use that as well.

I am going to use nano here. You’ll only see one line here and that contains your system’s hostname. Change it to the hostname you want and save the file.

You must also edit the /etc/hosts file in the similar fashion and replace all the occurrences of your old hostname with the new hostname.

This is important otherwise you’ll start seeing “sudo: unable to resolve host” error.

hosts and hostname files containing current hostname

Though I used Ubuntu here, you can use these methods to change hostname in Debian or most other distributions.

Change hostname in Ubuntu Using GUI

The beginner-friendly Linux distribution Ubuntu often provides the graphical way of doing things.

To change the hostname in Ubuntu 22.04 or any other Linux distribution using GNOME desktop, just go to the System Settings and click on About.

In here, you’ll see the ‘Device name’ field that is editable. This ‘Device name’ is your system’s hostname. Change it to whatever you want. The changes you make here are permanent.

change the hostname through ubuntu settings gui

It’s the same process pretty much in any other desktop environment.

Bonus Tip: Mandatory rules for hostnames

There are some rules specified for hostname:

  • hostname can only contain letters, digits, and the hyphen character, and the period character
  • hostname must begin and end with a letter or number
  • hostname must be between 2 and 63 characters long

If your proposed hostname doesn’t follow the rules, you may see an error like this:

hostname: the specified hostname is invalid

invalid hostname because of unsupported characters

In the end…

I hope this article helped you to change hostname on Ubuntu and other Linux distributions. Feel free to provide your feedback.

Источник

How to Change Hostname in Linux

To differentiate devices, data packets use IP addresses; however, IP addresses may not be easy for humans to recognize. Luckily, in Linux, the hostname is used in places like the terminal prompt to identify the device being used. In this guide, we will show you how to change the hostname of a device in Linux.

Hostname in Linux

Devices communicate with each other over a network by using IP addresses. The IP address assigned to a particular device may change depending based on the situation. Therefore, it is important to use a memorable hostname that does not change based on the situation.

Several methods can be used to change the hostname in Linux, and these methods can be divided into two categories: command-line interface (CLI) methods and graphical user interface (GUI) methods.

CLI methods

To change the hostname of a device in Linux, we recommend using CLI tools. These methods are universal and will work on almost any Linux distro. Furthermore, there is also no need to install any additional packages.

Читайте также:  Управление компьютером linux android

Check hostname

First, check the hostname of the current system as follows.

Change hostname using hostname

The hostname command can be used to change the hostname of the system by using the following command structure.

Finally, verify the change.

Note that the hostname in only temporarily changed using this method. If the system is rebooted, then it will revert back to the original hostname. This method should be used only when you do not intend to make a permanent hostname change.

Change hostname using hostnamectl

The hostnamectl tool is controls the hostname of the system. This tool can perform a wide range of actions; for example, it can be used to show the current hostname, to change the hostname, to configure the environment description, and to manipulate the system LOCATION string.

To use hostnamectl to change the hostname, run the following command.

Next, we have to manually update the host file. To do so, open the file using a text editor.

Change the old hostname to the new hostname.

If you are using an older Linux distro, then after making the change, you may need to run the following command.

Change hostname (for init system)

A dedicated file holds the hostname of the system. We can manually update the file to change the hostname. To do so, open the file using a text editor.

The image below shows the old hostname.

Edit the content to update the hostname.

Save the file and close the editor. For the changes to take effect, reboot the system. After the system reboots, verify that the action was successful as follows.

GUI Methods

In this section, we will use GNOME to change the hostname of a device. However, you can use YaST to perform hostname changes in openSUSE.

Change hostname in GNOME

GNOME is a powerful desktop environment. If you use GNOME, then you can change the hostname without running any commands by accessing the “Settings” app.

From the left panel, scroll to the bottom and select “About”. In this example, the hostname of the system is in the “Device name” field.

Click “Device name” to open a prompt for changing the hostname.

Enter the new hostname and click “Rename” to save the change.

Final thoughts

In this tutorial, we showed you several simple methods for changing the hostname of your system. Make sure to use a proper hostname to help to identify the system.

If you are using CentOS/RHEL or a similar distro, then changing the hostname may be more difficult. Check out how to change hostname permanent on CentOS 7.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.

Источник

How to change your hostname in Linux

What’s in a name, you ask? Everything. It’s how other systems, services, and users «see» your system.

military identification tag

Great Linux resources

Your hostname is a vital piece of system information that you need to keep track of as a system administrator. Hostnames are the designations by which we separate systems into easily recognizable assets. This information is especially important to make a note of when working on a remotely managed system. I have experienced multiple instances of companies changing the hostnames or IPs of storage servers and then wondering why their data replication broke. There are many ways to change your hostname in Linux; however, in this article, I’ll focus on changing your name as viewed by the network (specifically in Red Hat Enterprise Linux and Fedora).

Читайте также:  Astra linux rsyslog настройка

Background

A quick bit of background. Before the invention of DNS, your computer’s hostname was managed through the HOSTS file located at /etc/hosts . Anytime that a new computer was connected to your local network, all other computers on the network needed to add the new machine into the /etc/hosts file in order to communicate over the network. As this method did not scale with the transition into the world wide web era, DNS was a clear way forward. With DNS configured, your systems are smart enough to translate unique IPs into hostnames and back again, ensuring that there is little confusion in web communications.

Modern Linux systems have three different types of hostnames configured. To minimize confusion, I list them here and provide basic information on each as well as a personal best practice:

  • Transient hostname: How the network views your system.
  • Static hostname: Set by the kernel.
  • Pretty hostname: The user-defined hostname.

It is recommended to pick a pretty hostname that is unique and not easily confused with other systems. Allow the transient and static names to be variations on the pretty, and you will be good to go in most circumstances.

Working with hostnames

Now, let’s look at how to view your current hostname. The most basic command used to see this information is hostname -f . This command displays the system’s fully qualified domain name (FQDN). To relate back to the three types of hostnames, this is your transient hostname. A better way, at least in terms of the information provided, is to use the systemd command hostnamectl to view your transient hostname and other system information:

output of hostname -f and hostnamectl

Before moving on from the hostname command, I’ll show you how to use it to change your transient hostname. Using hostname (where x is the new hostname), you can change your network name quickly, but be careful. I once changed the hostname of a customer’s server by accident while trying to view it. That was a small but painful error that I overlooked for several hours. You can see that process below:

changing hostname with hostname x

It is also possible to use the hostnamectl command to change your hostname. This command, in conjunction with the right flags, can be used to alter all three types of hostnames. As stated previously, for the purposes of this article, our focus is on the transient hostname. The command and its output look something like this:

hostname change with hostnamectl

The final method to look at is the sysctl command. This command allows you to change the kernel parameter for your transient name without having to reboot the system. That method looks something like this:

hostname change using sysctl

GNOME tip

Using GNOME, you can go to Settings -> Details to view and change the static and pretty hostnames. See below:

hostname change with GNOME

Wrapping up

I hope that you found this information useful as a quick and easy way to manipulate your machine’s network-visible hostname. Remember to always be careful when changing system hostnames, especially in enterprise environments, and to document changes as they are made.

Want to try out Red Hat Enterprise Linux? Download it now for free.

Источник

Оцените статью
Adblock
detector