Вы находитесь на странице: 1из 25

1. hostnamectl command : Control the system hostname.

This is recommended
method.
2. nmtui command : Control the system hostname using text user interface (TUI).
3. nmcli command : Control the system hostname using CLI part of NetworkManager.

Types of hostnames
The hostname can be configured as follows

1. Static host name assigned by sysadmin. For example, “server1”, “wwwbox2”, or


“server42.cyberciti.biz”.
2. Transient/dynamic host name assigned by DHCP or mDNS server at run time.
3. 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:

Static hostname: centos-7-rc

Icon name: computer

Chassis: n/a

Machine ID: b5470b10ccfd49ed8e4a3b0e953a53c3

Boot ID: f79de79e2dac4670bddfe528e826b61f

Virtualization: oracle

Operating System: CentOS Linux 7 (Core)

CPE OS Name: cpe:/o:centos:centos:7

Kernel: Linux 3.10.0-229.1.2.el7.x86_64


Architecture: x86_64

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:

Static hostname: server1.cyberciti.biz

Pretty hostname: Senator Padmé Amidala's Laptop

Transient hostname: r2-d2

Icon name: computer

Chassis: n/a

Machine ID: b5470b10ccfd49ed8e4a3b0e953a53c3

Boot ID: f79de79e2dac4670bddfe528e826b61f

Virtualization: oracle

Operating System: CentOS Linux 7 (Core)

CPE OS Name: cpe:/o:centos:centos:7

Kernel: Linux 3.10.0-229.1.2.el7.x86_64

Architecture: x86_64
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
Use the Down arrow key > select the “Set system hostname” menu option > Press the
“Ok” button:
Fig.02: Set hostname
You will see the confirmation box as follows:

Fig.03: New hostname


confirmed
Finally, restart hostnamed service by typing the following command
# systemctl restart systemd-hostnamed
To verify changes, enter:
# hostnamectl status
Sample outputs:

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

-------------------------------------------

After installing the CentOS 7 on my server, I tried to change host name by


modifying the /etc/sysconfig/network; it did not take an effect of the modification.
Even after multiple reboots of the server, the host name remains
localhost.localdomain. The procedure to change the host name in CentOS 7 is now
totally different from the previous version, this guide will help you to setup the host
name on CentOS 7 / RHEL 7.

CentOS 7 supports three class of Host Names:

Static – The static host name is traditional host which can be chosen by the user and
is stored in /etc/hostname file.

Transient – The transient host name is maintained by the kernel and can be
changed by DHCP and mDNS.

Pretty – It is a free form UTF -8 host name for the presentation to the user.

HostName can be,

• 64 character in a length
• Recommend to have FQDN
• Consists of a-z,A-Z, 0-9, “-”, “_” and “.” only

How to Change:

Before changing the host name, let’s check the current host name.

[root@localhost ~]# hostname


localhost.localdomain

1. nmtui tool:

NetworkManager tool is used to set the static host name in /etc/hostname file.

nmtui -Select Set HostName


Set the host name.

nmtui – Change HostName


2
restart the hostnamed to force the hostnamectl to notice the change of the static
host name.

[root@localhost ~]# systemctl restart systemd-hostnamed

You can verify the change in host name.

[root@server ~]# hostname


server.itzgeek.com
[root@server ~]# cat /etc/hostname
server.itzgeek.com
[root@server ~]# cat /etc/sysconfig/network
# Created by anaconda
HOSTNAME=server.itzgeek.com

2. hostnamectl:

hostnamectl is used to change the host name, with this tool we can change all the
three class of host name; here we look only static host name.

Check the current host name.

[root@server ~]# hostnamectl status

Static hostname: server.itzgeek.com


Icon name: computer-vm
Chassis: vm
Machine ID: 565ea8b749544aca9d5563308f9e4bc2
Boot ID: 5c979d9b5f754df8b75a4e3aeabf2bad
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-123.el7.x86_64
Architecture: x86_64

Set the hostname.

[root@server ~]# hostnamectl set-hostname client.itzgeek.com

Check the host name again (Close the session and open new session using putty or
console)

[root@client ~]# hostnamectl status

Static hostname: client.itzgeek.com


Icon name: computer-vm
Chassis: vm
Machine ID: 565ea8b749544aca9d5563308f9e4bc2
Boot ID: 5c979d9b5f754df8b75a4e3aeabf2bad
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-123.el7.x86_64
Architecture: x86_64

If you use this command, you do not require to notify the change of the host name.
Close the current session and re launch the terminal.

3. nmcli tool:

It can be used to query and setup the static host name in /etc/hostname file.

Check the hostname.

[root@client ~]# nmcli general hostname


client.itzgeek.com

Change the host name.

[root@client ~]# nmcli general hostname server.itzgeek.com

restart the hostnamed to force the hostnamectl to notice the change of the static
host name.

[root@client ~]# systemctl restart systemd-hostnamed

4. Edit /etc/hostname

This is the simple but requires a reboot of the server to take an effect.
Note: Use the hostnamectl to change the host name, which is fair better than
other commands and does not require to update the kernel about the change
in the host name.

---------------------------

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 special character.
hostnames can contains 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 contains 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 and RHEL 7 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
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.

# cat /etc/hostname

Display CentOS 7 Hostname

In order to change or set a CentOS 7 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.

# hostnamectl

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
Set CentOS 7 Hostname

A second method to setup a CentOS 7 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.

# vi /etc/hostname

A third method that can be used to change a CentOS 7 machine hostname


is by using 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
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 general available
regardless of the used Linux distribution.
---------------------------

Unlike CentOS/RHEL 6, manually appending “HOSTNAME=xxxxx” into file


/etc/sysconfig/network and restarting system will not work on CentOS/RHEL 7, in
order to change/set the hostname. There 4 ways to change the hostname in
CentOS/RHEL 7 :

You can use either of following methods to change the hostname


1. use hostname control utility: hostnamectl
2. use NetworkManager command line tool: nmcli
3. use NetworkManager text user interface tool : nmtui
4. edit /etc/hostname file directly (a reboot afterwards is required)

hostname types
We can configure 3 hostname types is CentOS/RHEL 7 :
Hostname Type Description

Static Assigned by the system admin

Dynamic Assigned by DHCP or mDNS server at runtime

Pretty Assigned by the system admin. Its can be used as Description like “Oracle

Out of these 3, only static hostname is mandatory. Other 2 are optional.

Method 1 : hostnamectl
To get the current hostname of the system :

# hostnamectl status
Static hostname: localhost.localdomain
Icon name: computer
Chassis: n/a
Machine ID: 55cc1c57c7f24ed0b0d352648024cea6
Boot ID: a12ec8e04e6b4534841d14dc8425e38c
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-123.el7.x86_64
Architecture: x86_64
To set new hostname (geeklab) for the machine :

# hostnamectl set-hostname geeklab ## static


# hostnamectl set-hostname "Geeks LAB" ## pretty
Re-login and verify the new hostname :

# hostnamectl
Static hostname: geekslab
Pretty hostname: Geeks LAB
Icon name: computer
Chassis: n/a
Machine ID: 55cc1c57c7f24ed0b0d352648024cea6
Boot ID: a12ec8e04e6b4534841d14dc8425e38c
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-123.el7.x86_64
Architecture: x86_64

Method 2 : nmcli
To view the current hostname :

# nmcli general hostname


localhost.localdomain
To change the hostname to geeklab :

# nmcli general hostname geeklab


We need to restart the systemd-hostnamed service for the changes to take effect :

# service systemd-hostnamed restart


Re-login and erify the hostname change :

# hostname
geeklab

Method 3 : nmtui
We can also change the hostname using the nmtui tool :

nmtui
Select the option to “set the hostname” and hit enter

Set the hostname


Confirm the hostname change

Restart the systemd-hostnamed service for the changes to take effect.

# service systemd-hostnamed restart


Re-login and verify the hostname change.

# hostnamectl
Static hostname: geeklab
Icon name: computer
Chassis: n/a
Machine ID: 55cc1c57c7f24ed0b0d352648024cea6
Boot ID: a12ec8e04e6b4534841d14dc8425e38c
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-123.el7.x86_64
Architecture: x86_64

Method 4 : Edit /etc/hostname


This method requires a reboot of the system. View the current content of the file
/etc/hostname.

# cat /etc/hostname
localhost.localdomain
To change the hostname to “geeklab”, replace the content of the /etc/hostname file
with “geeklab”
# echo "geeklab" > /etc/hostname
# cat /etc/hostname
geeklab
Restart the system and verify.

# shutdown -r now
# hostname
geeklab
-------------------

5 ways to add or change


hostname in RHEL/CentOS 7
BY SHUSAIN · NOVEMBER 18, 2017
Hostname for a system is like an identity for that system, it not only makes system
management easy but we can also use hostname to access a system over LAN. In
this tutorial, we will learn five different ways by which we can change hostname in
RHEL/CentOS 7.

First way
We have been using this way for most of the Linux versions & it works fine
in CentOS/RHEL 7 as well. In this way, we will manually add hostname by
editing /etc/sysconfig/network,
$ vi /etc/sysconfig/network
and add your hostname,

NETWORKING=yes
HOSTNAME=centos7.linux.com
Restart network services to implement the changes.

$ systemctl restart network


You can now view the new hostname using command ‘hostname’.

Second Way
Second way for changing hostname is by using command ‘hostnamectl’.
Hostnamectl is used to change or query hostname. For changing
hostname, run the following command
$ hostnamectl set-hostname “centos7.linux.com”
To view the changes hostname, only run ‘hostnameclt‘ command,
$ hostnamectl
If you completely want to remove the hostname, use ‘hostnamectl set-
hostname “” ‘. This remove the hostname completely.

Third way
Next command to change hostname is ‘nmtui‘. NMTUI or “Network
Manager Text User Interface” utility can used while using CLI or GUI. With
‘nmtui’, we can edit network interfaces & change hostname also.
Type ‘nmtui’ into your shell & a new interface with some options will launch
$ nmtui
next, select ‘set system hostname’ & click ‘ok and enter your desired hostname
, press ‘ok‘ & its done.

Restart hostnamed service to implement the changes.

$ systemctl restart systemd-hostnamed


verify new hostname by running ‘hostname’ or ‘hostnamectl’.

Fourth Way
Next method of changing hostname is by using ‘nmcli’ command. It is
meant to be used with nmcli, to view the hostname run the following
command,
$ nmcli general hostname
To change the hostname using nmcli, use the command below,

$ nmcli general hostname centos7.linux.com


Restart the hostnamed service to implement changes & set the new
hostname

$ systemctl restart systemd-hostnamed

Fifth way
Last way for changing the hostname is not a preferred way but none the
less it gets the job done. To view the current hostname ,

$ sysctl kernel.hostname
& if you want to change the hostname, run

$ sysctl kernel.hostname=centos7.linux.com
Changes to the hostname will be immediate & you can verify the new
hostname by running any of the above commands to check hostname.

That’ it guys, these were the five ways to change hostname. If you have any
other method that you think i missed, please do share them via the
comment box down below.
-------------------

You have probably needed to change the hostname of your CentOS 7 install for
one reason or the other. Changing the hostname in CentOS 7 is now much simpler
than ever.

If you are ready open up your terminal and follow through this tutorial, and in less
than 10 minutes you will have changed your hostname!

Check Hostname
To check the hostname open your terminal and type the following command:

$ hostname
By default the hostname is localhost.localdomain. To change this you can follow
the following steps

Edit /etc/hostname File


The easiest way to change the hostname is to edit the /etc/hostname file. Open
your terminal and with your favourite text editor delete localhost.localdomain
and change to whatever name you like.

$ sudo vi /etc/hostname
Save changes and then confirm your hostname by using hostname command. If
it has not changed then you need to restart the CentOS 7 server.

Using Hostnamectl
Hostnamectl is a tool that is used control the Linux system hostname. You can
also use this tool to change the hostname in a few easy steps.

First and foremost confirm the hostname by using hostnamectl as follows:

$ hostnamectl status

To change the hostname type the command:

$ hostnamectl set-hostname grace

It may be necessary to restart the systemd-hostnamed daemon so that as to


reflect the change in Static hostname.

$ sudo systemctl restart systemd-hostnamed

Note: Hostnamectl recognizes three types of


hostnames: pretty,
pretty static and transient.
transient Pretty is stored in /etc/machine-
/etc/machine-info and
is a human readable format while static hostname is stored in /etc/hostname.
/etc/hostname
Transient hostname is more of a temporary hostname which may take up the
static hostname when network connectivity is lost.

Using Nmtui
To change the hostname you can also use the NetworkManager text interface tool
(nmtui). This is also another very easy method and fairly straight forward.

From the command line invoke nmtui:


$ sudo 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.
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.
OK You
can confirm changes using
the hostname command.

Using Nmcli
Nmcli is a command line tool for controlling the NetworkManager and can also be
used to change the hostname.

To check the hostname with nmcli, type in your terminal:

$ nmcli general hostname


This will print out the hostname to your terminal. By default in CentOS 7 it should
be localhost.localdomain.

To change this use the command:

$ nmcli general hostname grace


This will request for your password via a GUI interface. If you are running this
command remotely remember to use root or sudo:

$ sudo nmcli general hostname grace


You can then check the hostname with the command hostname or nmcli general
hostname.
Conclusion
See how easy it was? CentOS 7 is now much easier to administer compared to the
previous releases. Its time to upgrade/install systemd version if you haven't done
so.

----------------------

Check Hostname

To check the hostname, type the following command:


$ hostname

The default hostname of the system is localhost.localdomain. We can change it by editing the file,
/etc/hostname. This can be done by opening the file with a text editor and entering in the desired
hostname.
$ vi /etc/hostname

Please note that after the modification the file must be saved. If it has not changed, then try
restarting the server. Then confirm your new hostname in the hostname file.

Using Hostnamectl

With the help of hostnamectl we can easily control the Linux system hostname. You can also use
this tool to change the hostname in a few easy steps. The present hostname can be checked by:
$ hostnamectl status

The sample output will be the following:


Static hostname : centos7.is.cc
Icon name : computer-vm
Chassis : vm
Machine ID : fae4b37f37c05cdec9c7739d27983bbe
Boot ID : 8e5190a4f1c645ce9a7c6ed1f7c94cb3
Virtualization : kvm
Operating System : CentOS Linux 7 (Core)
CPE OS Name : cpe:/o:centos:centos:7
Kernel : Linux 3.10.0-327.22.2.el7.x86_64
Architecture : x86-64
To change the hostname try following command:
$ hostnamectl set-hostname TEST

You can rechek the hostname by using the command:


$ hostname
TEST

It is recommended to restart systemd-hostnamed daemon in order to update the changes.


$ sudo systemctl restart systemd-hostnamed

Using Nmtui

Network Manager text interface tool (NMTUI) is a graphical interface tool which allows to change
the hostname easily. Type the following command in terminal:

$ nmtui

A list of available options will be shown and you can select the required option from there.
Change the hostname from localhost.localdomain to the desired name. Press OK to apply the
modification done. For further verification type hostname in the command line.

Using Nmcli

This command line tool is used to manage the Network Manager and change the
hostname. The following command can be used to check the hostname:
$ nmcli general hostname
The below given command can be used to change the hostname
$ nmcli general hostname TEST

You can recheck it by using the command:


$ nmcli general hostname
TEST

This will request for your password via a GUI interface. If you are running this command remotely
remember to use root or sudo:
$ sudo nmcli general hostname grace
You can then check the hostname with the command hostname or nmcli general hostname.

If you need any further assistance please contact our support department.
-----------------

https://www.server-world.info/en/note?os=CentOS_7&p=hostname

The
example
for
setting
Hostnam
e of your
system.

[1] Change hostname. (but if rebooting, it will be back).

# show current hostname

[root@localhost ~]#
hostname

localhost.localdomain
# change hostname

[root@localhost ~]#
hostname dlp.srv.world

[root@localhost ~]#
hostname

dlp.srv.world
# just changed

[2] Change hostname permanently.

[root@localhost ~]#
hostnamectl set-hostname dlp.srv.world
# show status

[root@localhost ~]#
hostnamectl

Static hostname: dlp.srv.world


Icon name: computer-vm
Chassis: vm
Machine ID: 98a49a78fc9ad91f1b99304c75b94c31
Boot ID: 09b95ce0bc7f4179b1e8a011ed314c6b
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-123.4.2.el7.x86_64
Architecture: x86_64

Вам также может понравиться