This article is summarized and explained on how to configure/change hostname in Centos7 and RHEL7.
Understadning of 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:
Method 2: Using hostnamectl
Reference
Configure host names, https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Networking_Guide/ch-Configure_Host_Names.html
Understadning of hostname
There are three classes of
hostname
: static, pretty, and transient.- The “static” host name is the traditional
hostname
, which can be chosen by the user, and is stored in the/etc/hostname
file. - 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 byDHCP
ormDNS
at runtime. - 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
The hostnamectl tool will enforce the following: Static and transient host names to consist of 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.a-z
, A-Z
, 0-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
.
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
Post a Comment