- How to Set or Change Hostname in CentOS/RHEL 7/8
- How to Set or Change a Hostname in CentOS 7
- How to Change Centos Hostname
- Step 1: Check Existing Hostname
- Step 2: Set a New Static Hostname
- Step 3: Check the Hostname
- Step 4: Edit the /etc/hosts File
- Step 5: Reboot and Check CentOS 7 machine hostname
- Step 6 (Optional): Using a Pretty Hostname
- Step 7 (Optional): Setting a Transient Hostname
- How to Change Hostname in CentOS/RHEL 8
- Setting a Hostname in RHEL 8
How to Set or Change Hostname in CentOS/RHEL 7/8
A computer hostname represents a unique name that gets assigned to a computer in a network in order to uniquely identify that computer in that specific network. A computer hostname can be set to any name you like, but you should keep in mind the following rules:
- hostnames can contain letters (from a to z).
- hostnames can contain digits (from 0 to 9).
- hostnames can contain only the hyphen character ( – ) as a special character.
- hostnames can contain the dot special character ( . ) .
- hostnames can contain a combination of all three rules but must start and end with a letter or a number.
- hostnames letters are case-insensitive.
- hostnames must contain between 2 and 63 characters long.
- hostnames should be descriptive (to ease identifying the computer purpose, location, geographical area, etc on the network).
In order to display a computer name in CentOS 7/8 and RHEL 7/8 systems via console, issue the following command. The -s flag displayed the computer short name (hostname only) and the -f flag displays the computer FQDN in the network (only if the computer is a part of a domain or realm and the FQDN is set).
# hostname # hostname -s # hostname -f
You can also display a Linux system hostname by inspecting the content of /etc/hostname file using the cat command.
In order to change or set a CentOS 7/8 machine hostname, use the hostnamectl command as shown in the below command excerpt.
# hostnamectl set-hostname your-new-hostname
In addition to hostname command, you can also use hostnamectl command to display a Linux machine hostname.
In order to apply the new hostname, a system reboot is required, issue one of the below commands in order to reboot a CentOS 7 machine.
# init 6 # systemctl reboot # shutdown -r
A second method to set up a CentOS 7/8 machine hostname is to manually edit the /etc/hostname file and type your new hostname. Also, a system reboot is necessary in order to apply the new machine name.
A third method that can be used to change a CentOS 7/8 machine hostname is by using the Linux sysctl interface. However, using this method to change machine name results in setting-up the machine transient hostname.
The transient hostname is a special hostname initialized and maintained only by the Linux kernel as an auxiliary machine name in addition to he static hostname and doesn’t survive reboots.
# sysctl kernel.hostname # sysctl kernel.hostname=new-hostname # sysctl -w kernel.hostname=new-hostname
To display machine transient hostname issue the below commands.
# sysctl kernel.hostname # hostnamectl
Finally, the hostnamectl command can be used to achieve the following hostname setups: –pretty, –static, and –transient.
Although there are other more specific ways to change a Linux machine hostname, such as issuing nmtui command or manually editing some configuration files specific to each Linux distribution (/etc/sysconfig/network-scripts/ifcfg-ethX for CentOS), the above rules are generally available regardless of the used Linux distribution.
How to Set or Change a Hostname in CentOS 7
A hostname is simply the name a server goes by on a network. Setting a unique hostname is an excellent way to quickly find and identify different servers on your network.
There are three 3 types of hostnames.
- The statichostname is the most important one, and it’s stored in the /etc/hostname file. This hostname is used among machines to identify a particular server.
- The pretty hostname got its name because it allows for more characters and punctuation. It’s more user-friendly, but since it uses non-standard characters, it is not permitted for machine code. The pretty hostname is stored in the /etc/machine-info directory.
- The transienthostname is one maintained in the Linux kernel. It is dynamic, meaning it will be lost after a reboot. This approach might be useful if you have a minor job requiring a temporary hostname, but you don’t want to risk making a permanent change that might be confusing.
- Server running CentOS 7
- Access to a user account with root privileges
- Terminal window (Menu > Applications > Utilities > Terminal)
- A text editor, like Vim
How to Change Centos Hostname
Step 1: Check Existing Hostname
Before you start, it is advised to check what your current hostname is. Type the following command in the console to find out:
The output should return the static hostname, as well as a list of other information about your network configuration and operating system.
Step 2: Set a New Static Hostname
As CentOS 7 only allows Fully Qualified Domain Names (FQDNs), double-check the hostname you plan to use.
Acceptable values include:
Type in the following command in the terminal:
hostnamectl set-hostname my.new-hostname.server
Note: Make sure to replace my.new-hostname.server with your chosen hostname.
Step 3: Check the Hostname
Next, verify the hostname by using the following command again:
The console should display the new hostname.
Step 4: Edit the /etc/hosts File
Start by opening the hosts file by typing:
In the text editor, look for the line that begins with 127.0.0.1 (the IP address that refers to the system you are working on). It should read:
127.0.0.1 localhost localhost.localdomain localhost 4 localhost4.localdomain4 old.hostname
Change the entry old.hostname to my.new-hostname.server – and spell it the same as in Step 2.
Save the file and exit.
Note: To learn more about hosts file, check out our article on how to edit hosts file on Linux, Windows and Mac.
Step 5: Reboot and Check CentOS 7 machine hostname
Restart your system. Open a console window, and run:
It should display your new hostname.
You can also use your text editor to open and verify your /etc/hostsfile . It should still have your new hostname listed.
Step 6 (Optional): Using a Pretty Hostname
To use a «pretty» hostname type the following command:
hostnamectl set-hostname "My Boss's Hostname"
Make sure you have the quotation marks.
Once that completes, check the hostname:
The console should return a list of information. Check the Static hostname and Pretty hostname lines – they should look like this:
Static hostname: mybossshostname
Pretty hostname: My Boss's Hostname
By putting the hostname in quotes, you’re telling the system to use the complex characters inside the quotes as a pretty hostname.
This enables you to avoid character restrictions for static hostnames.
But you still need a FQDN hostname for the machine to understand. Fortunately, CentOS is smart enough to remove or change any unacceptable characters and render the static hostname automatically.
Step 7 (Optional): Setting a Transient Hostname
Fist, open the console and type the following:
sudo hostnamectl -transient set-hostname temporary.hostname
You can check the hostname in the same way you did earlier, with the hostnamectl or hostnamectl status command.
This change will last until you reboot the machine.
You can use this command with any type of hostname (Static, Pretty, or Transient) as an option with the double-hyphen.
Just use the double-hyphen to indicate what you want:
sudo hostnamectl --prettyset-hostname "Pretty Hostname"
sudo hostnamectl --staticset-hostname temporary.hostname
Note: If you are using CentOS 8, please refer to our guide on how to change or set the hostname on CentOS 8 / RHEL 8.
To set or change a hostname in CentOS 7 is fairly straightforward.
If you are a more advanced user, you can try out other methods as well. One way is editing the /etc/hostname file with a text editor. Alternatively, you can run the nmtui tool at the command prompt, which will launch a little graphical tool to change the hostname.
A guide on how to change or set the hostname is also available for other Linux distributions like Ubuntu 18.04.
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
If you decide to change your hostname, you have multiple available options to do so. Follow the methods in.
Learn The Simple Way How to Change Hostname on Ubuntu 18.04. Multiple options, including using Linux command.
DNS cache can be corrupted for a number of different reasons, including network attacks or viruses. When that.
The hosts file in Windows, Mac, or Linux maps hostnames to IP addresses. This could be in an intranet, like a.
How to Change Hostname in CentOS/RHEL 8
Setting up a hostname is one of the quintessential tasks when setting up a server. A hostname is a name that is assigned to a PC in a network and helps in uniquely identifying it.
There are various ways of setting up a hostname in CentOS/RHEL 8 and we are going to look at each in turn.
To display the system’s hostname, run the command.
Additionally, you can execute the hostnamectl command as shown:
Setting a Hostname in RHEL 8
To configure a hostname, login and use the hostnamectl command as shown:
$ sudo hostnamectl set-hostname
For example, to set the hostname to tecmint.rhel8 execute the command:
$ sudo hostnamectl set-hostname tecmint.rhel8
You can later verify if the new hostname has been applied to your system by running the hostname or hostnamectl commands.
Next, add the record for the hostname in the /etc/hosts file.
This automatically adds an entry by default to the /etc/hostname file.
Save and exit the text editor.
Finally, restart the networking service for the changes to come into effect.
$ sudo systemctl restart NetworkManager
Alternatively, you can use nmtui command to set or change the hostname of your system as shown.
Finally, restart the systemd-hostnamed service to apply recent changes.
$ sudo systemctl restart systemd-hostnamed
And this concludes this guide on how to change or set a hostname on CentOS/RHEL 8. We hope you found this guide helpful.