Red hat linux change hostname

How to change your hostname in Linux

Image by Pixabay

More 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).

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:

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:

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:

Читайте также:  Windows 10 x64 build 1607 что это

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:

GNOME tip

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

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.

Источник

7 ways to set your hostname in Fedora, CentOS, or Red Hat Enterprise Linux

More Linux resources

A hostname is a human-readable string that helps people refer to a computer by a familiar name, rather than by a number or unwieldy descriptors like, «the third one from the bottom of the second-to-last rack.» Often, a hostname is set during the installation process, but there are times when it needs to be changed. On Linux, there are many ways to set a hostname, and this article aims to cover them all.

Before exploring the tools related to hostnames, though, you must understand the different contexts in which the term is used. There are potentially two designators of a system’s hostname: the computer’s administrator (a laptop’s owner, or a server’s root user) and the network (depending on protocols and settings). This factor can lead to confusion, because you might look at your computer’s hostname and see one value, only to find that the same computer is referred to as something different over the network.

There’s a historical reason for this situation. Long ago, before DNS, hosts on a network had to be defined locally in the file /etc/hosts . If there were 31 hosts on a network and a new one was added, then 32 hosts had to update their /etc/hosts file to reflect the correct IP address and corresponding hostname for each of their neighbors. This process didn’t scale well for bigger networked systems such as the World Wide Web (the Internet), and so DNS was invented, and the concept of hostnames largely was abstracted away from local computers to instead be managed by the network.

Today, the important hostname is the one the network uses. The hostname value in /etc/hosts is often set to localhost by default.

With this context in mind, here are all the different ways to manipulate a hostname on Linux.

Change all three names with hostnamectl

The hostnamectl command from systemd can manipulate three varieties of hostnames:

  • Transient: Received from network configuration.
  • Static: Provided by the kernel.
  • Pretty: Provided by the user.

A transient hostname can change as needed to avoid name collisions. For instance, if you name your computer penguin but there’s already another host with that name on the network, your network hostname becomes penguin-1 .

Static and pretty hostnames are a little like local variables: They’re used for activities occurring on the local machine, mostly as a convenience for the user, along with applications that need to know whether they’re running locally or remotely (for example, over a forwarded X session).

When invoked without any arguments, hostnamectl returns the static and pretty names, plus some system information:

Change the static and pretty names in the GUI with GNOME Settings

If you want to set the static and pretty names in a desktop application, use GNOME Settings. To launch GNOME Settings, go to the Activities menu in the upper left corner of your GNOME desktop. Type Settings into the search field, or click the Show Applications icon on the left dock and find Settings in the application icons as shown below:

Change the static name with cockpit

Fedora, CentOS, and RHEL systems feature a web console application called Cockpit for monitoring and configuring local and remote machines. Using Cockpit, you can change the static hostname for your own machine, or any machine you administer (as long as it has Cockpit enabled).

Читайте также:  Что такое файловый менеджер far для windows

First, install and enable Cockpit:

In the web console, click the System tab in the left column. Click the entry for Host name and edit the Pretty Host Name and (static) Real Host Name:

Change the static name in the hostname file

You can also set the static hostname manually by editing /etc/hostname . This file contains exactly one line by default. If you have not changed the hostname yet, then that line probably reads localhost.localdomain . If you have used hostnamectl to change the hostname already, then this file reflects that change.

Editing this file manually is not necessarily recommended, because it doesn’t update all the other hostname values the way hostnamectl does. For example:

Change the static name with nmcli

Another way to change a static hostname is through the command line interface for Network Manager, nmcli :

Change the transient name with sysctl

The sysctl command allows you to configure kernel parameters while Linux is running (that is, instead of at boot time). Your computer’s transient hostname is a kernel parameter, so you can modify it with this command:

Change the transient name with hostname

The hostname command from the util-linux package is a simple tool to query and set the transient hostname.

To query your current hostname:

Be careful with /etc/hosts

The /etc/hosts file is mostly historic, although it is used by some applications and protocols, and can be a useful method for creating shortcuts to hosts you use often. You can use it to set a hostname, but usually, you’re just creating an alias to localhost at IP address 127.0.0.1 (your computer’s network loopback address).

For example, if you change /etc/hosts from the default entries like this:

Follow the conformity convention

As you can tell from the iterations within this article, a computer’s hostname can get confusing should you use too many methods to set it, or if you change it frequently. While nothing enforces uniformity across hostname types, it’s a convention to keep everything the same on each computer.

Choose a memorable name and use it for your pretty name. Let the static and transient names be derived from that. Just as importantly, in a large network, choose meaningful names. For instance, all upper management computers may be named after a D&D monster, while all computers in the IT department may be named after a famous starship, and so on. Using a naming schema helps avoid name collisions and provides context to an otherwise dizzying list of network hosts.

Источник

RHEL 8 Change Hostname (computer name) command

Display the current RHEL 8 hostname

Type any one of the following command including the cat command:
$ cat /etc/hostname
OR
$ hostname
OR
$ hostnamectl

RHEL 8 change hostname command

The procedure to change the computer name on Red Hat Linux:

  1. Type the following command to edit /etc/hostname using nano or vi text editor:
    sudo vi /etc/hostname
  2. Delete the old name and setup new name.
  3. Next Edit the /etc/hosts file:
    sudo vi /etc/hosts
  4. Replace any occurrence of the existing computer name with your new one.
  5. Reboot the system to changes take effect:
    sudo reboot

How to change the RHEL 8 server hostname without a system restart

Type the following commands:
$ sudo hostname nixcraft-rhel8
Next edit the /etc/hostname file and update hostname:
$ sudo vi /etc/hostname
Finally, edit the /etc/hosts file and update the lines that reads your old-host-name:
$ sudo vi /etc/hosts
From:
127.0.1.1 localhost
To:
127.0.1.1 nixcraft-rhel8
Save and close the file.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard
Читайте также:  Linux чем объединить диски

Join Patreon

A note about hostnamectl command

Systemd based Linux distributions such as RHEL 8 can simply use the hostnamectl command to rename hostname. To see current setting just type:
$ hostnamectl
Sample outputs:

To change hostname from linux-mig6 to suse-rhel8, enter:
$ sudo hostnamectl set-hostname nixcraft-rhel8
$ hostnamectl

Set or change RHEL 8 server hostname using the hostnamectl command

Conclusion

On RHEL 8 one can use the hostnamectl command to control the system hostname and set a new name as well. This is recommended method for all RHEL 8 users.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

RHEL / Centos Linux 7: Change and Set Hostname Command

I ‘m a new RHEL (Red Hat Linux) and/or CentOS Linux 7 server user. How can I change the hostname in CentOS 7 using a command line option?

On a CentOS Linux 7 server you can use any one of the following tool to manage hostnames:[donotprint]

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements None
Est. reading time 2m

[/donotprint]

  1. hostnamectl command : Control the system hostname. This is recommended method.
  2. nmtui command : Control the system hostname using text user interface (TUI).
  3. nmcli command : Control the system hostname using CLI part of NetworkManager.

Types of hostnames

The hostname can be configured as follows

  1. Static host name assigned by sysadmin. For example, “server1”, “wwwbox2”, or “server42.cyberciti.biz”.
  2. Transient/dynamic host name assigned by DHCP or mDNS server at run time.
  3. Pretty host name assigned by sysadmin/end-users and it is a free-form UTF8 host name for presentation to the user. For example, “Vivek’s netbook”.

Method #1: hostnamectl command

Let us see how to use the hostnamectl command.

How do I see the host names?

$ hostnamectl
## OR ##
$ hostnamectl status
Sample outputs:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

How do I set the host name?

The syntax is:
# hostnamectl set-hostname Your-New-Host-Name-Here
# hostnamectl set-hostname «Your New Host Name Here» —pretty
# hostnamectl set-hostname Your-New-Host-Name-Here —static
# hostnamectl set-hostname Your-New-Host-Name-Here —transient
To set host name to “R2-D2”, enter:
# hostnamectl set-hostname R2-D2
To set static host name to “server1.cyberciti.biz”, enter:
# hostnamectl set-hostname server1.cyberciti.biz —static
To set pretty host name to “Senator Padme Amidala’s Laptop”, enter:
# hostnamectl set-hostname «Senator Padme Amidala’s Laptop» —pretty
To verify new settings, enter:
# hostnamectl status
Sample outputs:

How do I delete a particular host name?

The syntax is:
# hostnamectl set-hostname «»
# hostnamectl set-hostname «» —static
# hostnamectl set-hostname «» —pretty

How do I change host name remotely?

Use any one of the following syntax:
# ssh root@server-ip-here hostnamectl set-hostname server1
OR set server1 as host name on a remote server called 192.168.1.42 using ssh:
# hostnamectl set-hostname server1 -H root@192.168.1.42

Method #2: nmtui command

You can set host name using nmtui command which has text user interface for new users:
# nmtui
Sample outputs:

Fig.01: Use nmtui to set hostname on a CentOS 7

Fig.02: Set hostname

Fig.03: New hostname confirmed

Fig.04: CentOS 7 / RHEL View Host Name Details Using hostnamectl Command

Method #3: nmcli command

The nmcli is a command line tool for controlling NetworkManager and reporting network status.

To view the host name using nmcli command:

The syntax is:
# nmcli general hostname

To set the host name using nmcli command:

The syntax is:
# nmcli general hostname R2-D2
# nmcli general hostname server42.cyberciti.biz
Finally, restart the systemd-hostnamed service:
# systemctl restart systemd-hostnamed

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

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