Skip to main content

How to change hostname on Centos 7 /RHEL7

This article is summarized and explained on how to configure/change hostname in Centos7 and RHEL7.


Understadning of hostname


There are three classes of hostname: static, pretty, and transient.
  1. The static host name is the traditional hostname, which can be chosen by the user, and is stored in the /etc/hostname file. 
  2. The transient hostname is a dynamic host name maintained by the kernel. It is initialized to the static host name by default, whose value defaults to localhost. It can be changed by DHCP or mDNS at runtime. 
  3. The pretty hostname is a free-form UTF8 host name for presentation to the user.
A host name can be a free-form string up to 64 characters in length. However, Red Hat recommends that both static and transient names match the fully-qualified domain name (FQDN) used for the machine in DNS, such as host.example.com. It is also recommended that the static and transient names consists only of 7 bit ASCII lower-case characters, no spaces or dots, and limits itself to the format allowed for DNS domain name labels, even though this is not a strict requirement. Older specifications do not permit the underscore, and so their use is not recommended.
The hostnamectl tool will enforce the following: Static and transient host names to consist of a-zA-Z0-9-_ and . only, to not begin or end in a dot, and to not have two dots immediately following each other. The size limit of 64 characters is enforced.



Defualt / Current hostname

Host name is set to localhost.localdomain by default in Centos 7 / RHEL 7 and this can be found in /etc/hostname.




Configuring host name

Setting hostname is as simple as changing /etc/hostname file which requires reboot and only affect static hostname. RHEL7 and Centos 7 are introducing more ways to configure hostname as following methods.


Method 1: Using Text User Interface - nmtui

The text user interface tool nmtui can be used to configure a host name in a terminal window. Issue the following command to start the tool:
~]$ nmtui




The NetworkManager text user interface tool nmtui can be used to query and set the static host name in the /etc/hostname file. Note that at time of writing, changing the host name in this way will not be noticed by hostnamectl.
To force hostnamectl to notice the change in the static host name, restart hostnamed as root:
~]# systemctl restart systemd-hostnamed


Method 2: Using hostnamectl



The hostnamectl tool is provided for administering the three separate classes of host names in use on a given system.
1. View All the Host Names
To view all the current host names, enter the following command:
~]$ hostnamectl status

2. Set All the Host Names

To set all the host names on a system, enter the following command as root:
~]# hostnamectl set-hostname name
This will alter the pretty, static, and transient host names alike. The static and transient host names will be simplified forms of the pretty host name. Spaces will be replaced with - and special characters will be removed.
3. Set a Particular Host Name
To set a particular host name, enter the following command as root with the relevant option:
~]# hostnamectl set-hostname name [option...]
Where option is one or more of: --pretty--static, and --transient.
If the --static or --transient options are used together with the --pretty option, the static and transient host names will be simplified forms of the pretty host name. Spaces will be replaced with - and special characters will be removed. If the --pretty option is not given, no simplification takes place.
When setting a pretty host name, remember to use the appropriate quotation marks if the host name contains spaces or a single quotation mark. For example:
~]# hostnamectl set-hostname "Stephen's notebook" --pretty

4. Clear a Particular Host Name

To clear a particular host name and allow it to revert to the default, enter the following command as root with the relevant option:
~]# hostnamectl set-hostname "" [option...]
Where "" is a quoted empty string and where option is one or more of: --pretty--static, and --transient.

5. Changing Host Names Remotely

To execute a hostnamectl command on a remote system, use the -H, --host option as follows:
~]# hostnamectl set-hostname -H [username]@hostname
Where hostname is the remote host you want to configure. The username is optional. The hostnamectl tool will use SSH to connect to the remote system.

Method3: Using NetworkManager tool - nmcli
The NetworkManager tool nmcli can be used to query and set the static host name in the /etc/hostname file. Note that at time of writing, changing the host name in this way will not be noticed by hostnamectl.
To query the static host name, issue the following command:
~]$ nmcli general hostname
To set the static host name to my-server, issue the following command as root:
~]# nmcli general hostname my-server
To force hostnamectl to notice the change in the static host name, restart hostnamed as root:
~]# systemctl restart systemd-hostnamed


Reference

Configure host names, https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Networking_Guide/ch-Configure_Host_Names.html

Comments

Popular posts from this blog

How to configure LDAPS, LDAP over SSL, using CA Certificate Service on Domain Controller in Windows Server 2016

This post is to provide an instruction on how to set up LDAPS (LDAP over SSL) on Domain Controller using single-tier CA hierachy. Reasons for Enabling LDAPS By default, LDAP communications between client and server applications are not encrypted. This means that it would be possible to use a network monitoring device or software and view the communications traveling between LDAP client and server computers. This is especially problematic when an LDAP simple bind is used because credentials (username and password) is passed over the network unencrypted. This could quickly lead to the compromise of credentials.  Reasons for enabling Lightweight Directory Access Protocol (LDAP) over Secure Sockets Layer (SSL) / Transport Layer Security (TLS) also known as LDAPS include: Some applications authenticate with Active Directory Domain Services (AD DS) through simple BIND. As simple BIND exposes the users’ credentials in clear text, use of Kerberos is preferred. If simp...

[Tableau] How to install Tableau Server on Centos 7 / Redhat

Step 1: Install Tableau Server package and start Tableau Services Manager 1. Log on as a user with sudo access to the computer where you want to install Tableau Server. 2. Downalod installer from below Tableau download page. https://www.tableau.com/products/server/download/linux // use WINSCP or any preferred utility to move Tableau Server installation file to Centos. 3. Use the package manager to install the Tableau Server package. sudo yum update sudo yum install tableau-server-<version>.x86_64.rpm -- Non-default location—To install to a non-default location, you must use rpm -i. You will also need to install all dependent packages. See the note below. Run the following command: sudo rpm -i --prefix /preferred/install/path tableau-server.rpm Refer to https://onlinehelp.tableau.com/current/server-linux/en-us/setup.htm -- 4. Navigate to the scripts directory: cd /opt/tableau/tableau_server/packages/scripts.'version'/ 5. Ru...

[AD LDAP] How to install LDAP in AD in Windows Server 2019 in VirtualBox