Five ways to modify hostname in CentOS7

Keywords: network Anaconda CentOS Linux

Reprinted from: https://blog.csdn.net/liumiaocn/article/details/103170673

Narration

This article is used to introduce the five setting methods of hostname under CentOS 7. Although many methods can be used on different distributions of Linux, they are not verified in this article.

Confirm hostname method

Command to confirm the name of the current machine: hostname

Current machine name setting file / etc/hostname

Environment variable related to current machine name: HOSTNAME
[root@devops ~]# hostname
devops
[root@devops ~]# cat /etc/hostname
devops
[root@devops ~]# echo $HOSTNAME
devops
[root@devops ~]#

Method 1: use the hostname command

The current machine name is devops. Use hostname to change it to liumiao

[root@devops ~]# hostname liumiao
[root@devops ~]# echo $HOSTNAME
devops
[root@devops ~]# cat /etc/hostname
devops
[root@devops ~]# hostname
liumiao
[root@devops ~]#

You can see that the hostname command has changed except that the prompt has not changed. After logging in again, this prompt and the hostname environment variable will change

[root@liumiao ~]# echo $HOSTNAME
liumiao
[root@liumiao ~]# hostname
liumiao
[root@liumiao ~]# cat /etc/hostname
devops
[root@liumiao ~]# 

After restart, the status before modification will be restored

[root@devops ~]# cat /etc/hostname
devops
[root@devops ~]# hostname
devops
[root@devops ~]# echo $HOSTNAME
devops
[root@devops ~]#

Method 2: use the hostnamectl command

Command format: hostnamectl set hostname new name

The execution log is shown below, and you can see that all the contents except PS1 have been modified in real time

[root@devops ~]# hostnamectl set-hostname liumiao
[root@devops ~]# cat /etc/hostname
liumiao
[root@devops ~]# echo $HOSTNAME
devops
[root@devops ~]# hostname
liumiao
[root@devops ~]# 

After logging in again, you will find that the information of PS1 has changed

[root@liumiao ~]# cat /etc/hostname
liumiao
[root@liumiao ~]# hostname
liumiao
[root@liumiao ~]# echo $HOSTNAME
liumiao
[root@liumiao ~]#

It will not change after reboot. So hostnamectl is basically a way of changing in real time and being able to persist.

Mode 3: modify / etc/hostname

You can also change the machine name by modifying the / etc/hostname file

[root@liumiao ~]# cat /etc/hostname
liumiao
[root@liumiao ~]# echo $HOSTNAME
liumiao
[root@liumiao ~]# hostname
liumiao
[root@liumiao ~]#

Change it to devops and restart

[root@liumiao ~]# vi /etc/hostname
[root@liumiao ~]# cat /etc/hostname
devops
[root@liumiao ~]# reboot
Shared connection to 127.0.0.1 closed.
liumiaocn:~ liumiao$ 

After logging in again, you can see that they have been modified

[root@devops ~]# hostname
devops
[root@devops ~]# cat /etc/hostname
devops
[root@devops ~]# echo $HOSTNAME
devops
[root@devops ~]#

Mode 4: modify with sysctl

Command format: sysctl kernel.hostname = new name

sysctl can also achieve the same effect by modifying the settings in the kernel. The execution log is as follows

[root@devops ~]# hostname
devops
[root@devops ~]# cat /etc/hostname
devops
[root@devops ~]# echo $HOSTNAME
devops
[root@devops ~]# 
[root@devops ~]# sysctl kernel.hostname
kernel.hostname = devops
[root@devops ~]# 
[root@devops ~]# sysctl kernel.hostname=liumiao
kernel.hostname = liumiao
[root@devops ~]#

The results after execution are as follows

[root@devops ~]# echo $HOSTNAME
devops
[root@devops ~]# cat /etc/hostname
devops
[root@devops ~]# hostname
liumiao
[root@devops ~]#

After logging back in, you can see that in addition to / etc/hostname, the HOSTNAME environment variable has also changed

[root@liumiao ~]# echo $HOSTNAME
liumiao
[root@liumiao ~]# hostname
liumiao
[root@liumiao ~]# cat /etc/hostname
devops
[root@liumiao ~]# 

Because there is no persistent save, reboot will restore the previous state as the first method.

Method 5: modify / etc/sysconfig/network

By modifying / etc/sysconfig/network, you can also modify the return value of hostname. For example:

[root@devops ~]# cat /etc/sysconfig/network
# Created by anaconda
[root@devops ~]# vi /etc/sysconfig/network
[root@devops ~]# 
[root@devops ~]# cat /etc/sysconfig/network
# Created by anaconda
hostname liumiao.com
[root@devops ~]#

After the network service is restarted, it will take effect

[root@devops ~]# systemctl restart network
[root@devops ~]# hostname
liumiao.com
[root@devops ~]# echo $HOSTNAME
devops
[root@devops ~]#

Change after login again

[root@liumiao ~]# echo $HOSTNAME
liumiao.com
[root@liumiao ~]# hostname
liumiao.com
[root@liumiao ~]# cat /etc/hostname
devops
[root@liumiao ~]#

In fact, hostname has two options (- F and - s: -f return FQDN value, while - s return shortname). So far, the return values of other methods are the same, but different values are returned.

[root@liumiao ~]# hostname -f
liumiao.com
[root@liumiao ~]# hostname -s
liumiao
[root@liumiao ~]#

It will not change after restart.

First article in the station
Published 23 original articles, won praise 31, visited 90000+
Private letter follow

Posted by intercampus on Mon, 10 Feb 2020 05:13:27 -0800