- CentOS 8 Change Hostname (computer name) command
- Display the current CentOS 8 hostname
- CentOS 8 change hostname command
- How to change the CentOS 8 server hostname without a system restart
- A note about hostnamectl command
- Conclusion
- How to Set or Change Hostname in CentOS/RHEL 7/8
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- RHEL / Centos Linux 7: Change and Set Hostname Command
- Types of hostnames
- Method #1: hostnamectl command
- How do I see the host names?
- How do I set the host name?
- How do I delete a particular host name?
- How do I change host name remotely?
- Method #2: nmtui command
- Method #3: nmcli command
- To view the host name using nmcli command:
- To set the host name using nmcli command:
- 4 Methods to Change Hostname in RHEL/CentOS 7/8
- 1) By Editing /etc/hostname File
- 2) Using Hostnamectl Command
- 3) Using Nmtui Utility
- 4) Using Nmcli Utility
- Conclusion
- More Articles You May Like
- 9 Comments. add one
CentOS 8 Change Hostname (computer name) command
Display the current CentOS 8 hostname
Type any one of the following command:
$ hostname
OR
$ hostnamectl
CentOS 8 change hostname command
The procedure to change the computer name on CentOS Linux:
- Type the following command to edit /etc/hostname using nano or vi text editor:
sudo vi /etc/hostname - Delete the old name and setup new name.
- Next Edit the /etc/hosts file:
sudo vi /etc/hosts - Replace any occurrence of the existing computer name with your new one.
- Reboot the system to changes take effect:
sudo reboot
How to change the CentOS 8 server hostname without a system restart
Type the following commands:
$ sudo hostname nixcraft-centos8
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-centos8
Save and close the file.
A note about hostnamectl command
Systemd based Linux distributions such as CentOS 8 can simply use the hostnamectl command to rename hostname. To see current setting just type:
$ hostnamectl
Sample outputs:
To change hostname from localhost.localdomain to nixcraft-centos8, enter:
$ sudo hostnamectl set-hostname nixcraft-centos8
$ hostnamectl
Set or change CentOS 8 server hostname using the hostnamectl command
- 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 ➔
Conclusion
On CentOS 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 CentOS 8 users.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
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).
Check Hostname in CentOS 7
You can also display a Linux system hostname by inspecting the content of /etc/hostname file using the cat command.
Display CentOS 7 Hostname
In order to change or set a CentOS 7/8 machine hostname, use the hostnamectl command as shown in the below command excerpt.
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.
Set CentOS 7 Hostname
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.
To display machine transient hostname issue the below commands.
Change CentOS 7 Hostname
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.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
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]
- hostnamectl command : Control the system hostname. This is recommended method.
- nmtui command : Control the system hostname using text user interface (TUI).
- nmcli command : Control the system hostname using CLI part of NetworkManager.
Types of hostnames
The hostname can be configured as follows
- Static host name assigned by sysadmin. For example, “server1”, “wwwbox2”, or “server42.cyberciti.biz”.
- Transient/dynamic host name assigned by DHCP or mDNS server at run time.
- 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
Источник
4 Methods to Change Hostname in RHEL/CentOS 7/8
To check the hostname open your terminal and type hostname or hostnamectl command in CentOS 7/8. By default, the hostname is localhost.localdomain. To change hostname you can follow the following methods.
1) By Editing /etc/hostname File
The easiest way to change the hostname is to edit the /etc/hostname file. Open your terminal and with your favorite text editor delete localhost.localdomain and change to whatever name you like.
Save changes and then confirm your hostname by using hostname command. If it has not changed then you need to restart the CentOS 7/8 server.
2) Using Hostnamectl Command
Hostnamectl is a tool that is used control the Linux system hostname. You can also use this tool to change the hostname.
First and foremost confirm the current hostname by using hostnamectl as follows:
To change the hostname type the following command:
It’s necessary to restart the systemd-hostnamed daemon so that as to reflect the change in Static hostname.
Note: Hostnamectl recognizes three types of hostnames: pretty, static and transient. Pretty is stored in /etc/machine-info and is a human-readable format while static hostname is stored in /etc/hostname. Transient hostname is more of a temporary hostname which may take up the static hostname when network connectivity is lost.
3) Using Nmtui Utility
To change the hostname you can use NetworkManager text interface tool (nmtui).
From the command line invoke nmtui:
This will present to you a text user interface like this. Using the arrow keys select Set system hostname and use tab to select OK.
Edit the hostname which by default is localhost.localdomain and change it to whatever you prefer.
After changing the hostname, choose okay, then the following confirmation message will appear. If the hostname is as you desire you can choose OK. You can confirm changes using the hostname command.
4) Using Nmcli Utility
Nmcli is a command line tool for controlling the NetworkManager and can be used to change the hostname.
To check the hostname with nmcli, type in your terminal:
This will print out the hostname to your terminal. By default in CentOS 7/8 it should be localhost.localdomain.
To change hostname use the command:
This will request for your password via a GUI interface. If you are running this command remotely remember to use root or sudo:
You can confirm the hostname by running nmcli general hostname.
Conclusion
In this article, we learn methods to change hostname in CentOS 7 and same command you can apply on CentOS 8. Please let us know your feedback in the below comment section.
More Articles You May Like
9 Comments. add one
I set the hostname but upon reboot the systemd-hostnamed changes it back to the original hostname.
I have used hostname, /etc/hostname , /etc/hosts, AND hostnamectl, but none of them are persistent upon reboot.
Here is /var/log/messages , where it changes the hostname
Oct 17 16:55:26 sim systemd[1]: No hostname configured.
Oct 17 16:55:26 sim systemd[1]: Set hostname to .
Oct 17 16:55:29 sim systemd: Set hostname to .
Oct 17 16:55:30 sim NetworkManager[372]: hostname ‘ip-172-31-18-83’
Oct 17 16:55:32 sim dbus-daemon: dbus[386]: [system] Activating via systemd: service name=’org.freedesktop.hostname1′ unit=’dbus-org.freedesktop.hostname1.service’
Oct 17 16:55:32 sim dbus[386]: [system] Activating via systemd: service name=’org.freedesktop.hostname1′ unit=’dbus-org.freedesktop.hostname1.service’
Oct 17 16:55:32 sim systemd: Starting Hostname Service.
Oct 17 16:55:32 sim dbus-daemon: dbus[386]: [system] Successfully activated service ‘org.freedesktop.hostname1’
Oct 17 16:55:32 sim dbus[386]: [system] Successfully activated service ‘org.freedesktop.hostname1’
Oct 17 16:55:32 sim systemd: Started Hostname Service.
Oct 17 16:55:32 sim systemd-hostnamed: Changed static host name to ‘ip-172-31-18-83.ec2.internal’
Oct 17 16:55:32 sim systemd-hostnamed: Changed host name to ‘ip-172-31-18-83.ec2.internal’
[root@ip-172-31-18-83 ec2-user]# date
Fri Oct 17 16:57:09 EDT 2014
[root@ip-172-31-18-83 ec2-user]#
did you try this ?
#hostnamectl set-hostname —static «yourhostname»
Same issue here, centos 7 static hostname gets set, then changed at boot time —
1583:Jan 21 17:53:13 p-ftp01 systemd-hostnamed: Changed static host name to ‘p-ftp01.predsci.local’
2253:Jan 21 17:54:34 p-ftp01 systemd-hostnamed: Changed static host name to ‘ip-172-31-28-106.ec2.internal’
2254:Jan 21 17:54:34 p-ftp01 systemd-hostnamed: Changed host name to ‘ip-172-31-28-106.ec2.internal’
As I can see that you have Amazaon EC2, you’ll need to do the following to change your hostname.
1. Set the hostname in /etc/hostname.
2. Edit /etc/cloud/cloud.cfg and set preserve_hostname to True. (You can also pass this option in with your user data.)
Thank You
Thank you so much Arun. I’ve been scouring the web for this having tried all the other methods I could find. Each time on reboot I see the name changed in the log. I found mention of the cloud.cfg but nothing regarding the specifics.
For other searchers, this is on the CentOS AWS image kernel 3.10.0-123.8.1.el7.x86_64. I’m going to post a comment and credit you Arun.
I was about to go crazy since the hostname changes I make was not being applied properly after a reboot!
Adding «preserve_hostname:true» to /etc/cloud/cloud.cfg made it permanent!
Any idea if that file is a core component of the OS or is used by something else in essence?
Hi Mustafa,
Welcome to LinOxide,
I am glad to hear that you fixed your problem. That file is of cloud-init package that handles early initialization of a cloud instance which is used in EC2.
Thank You !
Thanks Arun 🙂
Most OS’s cloud image release is packaged with «cloud-init» and it support many data source.
EC2 datasource is the oldest and most widely used datasource that cloud-init supports.
Working on Amazon AWS EC2, Followed the instructions and did what is written here:
1. /etc/cloud/cloud.conf has the line
preserve_hostname: true
2. Did the command
sudo hostnamectl set-hostname «foobar»
Then reboots works like a charm the first time.
Next reboot (without running the hostnamectl command) but the cloud.conf verified to still have the value to true, and the machine is back to it’s Amazon default name of ip_172 something.
Not sure how to solve this besides hacks of manually setting it.
On a bit sarcastic mood, Makes me wonder if in 2015/6 we still need to re-design setting up hostname to a machine or move on to more interesting things.
Источник