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

Setting up AD LDS on Windows Server 2016

What Is Active Directory Lightweight Directory Services? Microsoft Active Directory Lightweight Directory Services (AD LDS) is an independent mode of Active Directory that provides dedicated directory services for applications. AD LDS is a mode of Active Directory that provides directory services for applications. AD LDS provides dedicated directory services for applications. It provides a data store and services for accessing the data store. It uses standard application programming interfaces (APIs) for accessing the application data. The APIs include those of Active Directory, Active Directory Service Interfaces, Lightweight Data Access Protocol, and System.DirectoryServices. AD LDS operates independently of Active Directory and independently of Active Directory domains or forests. It operates either as a standalone data store, or it operates with replication. Its independence enables local control and autonomy of directory services for specific applications. It al...

How to set up Active Directory Domain Service role in Windows server 2016

This post is showing how to set up Active Directory Domain Service role in Windows Server 2016. Consideration Change server name to meaningful name if not changed yet. How to 1. Add role or feature on the top menu "Manage".   2. Next with role-based or feature-based installation. 3. Select Active Directory Domain Services. 4. Add required features for Active Directory Domain Services. 5. Keep going with default until installing. 6. Once it is successfuly installed, then you need to promote AD server.  7. Add new domain. 8. Type in password for Directory Service Restore Mode and click Next. 10. Proceed with default to installation step. 11. Installation. 12. Once it's all configured, server will reboot. 13. check if you can see ad server on your server now. Done!!.